Initializing Arrays - Answer-Can initialize arrays in the same line as declaration
Format: type name[size] = { list of elements };
Ex: int list[5] = {1, 3, 5, 9, 10};
The list of elements goes in { } and is separated by commas
may leave size box empty when initializing on the declaration line - compiler sets size.
Can also initialize with for loops (good with regular patterns)
Initializing Arrays (Special case) - Answer-strings: null-terminated character arrays
can initialize on the declaration with a string literal
Ex: char name[7] = "Marvin";
size must leave room for null-character '\0'
, Using Arrays - Answer-valid indices are 0 through size-1.
may use any of these index numbers to access a single array element:
may use any positive integer r-value to index arrays (i.e. variables, expressions, etc)
it is the programmer's job to check for out-of-bounds index!
Copying Arrays - Answer-Assignment between array names does not copy one array to
another
If you want to copy one array to another, do it element by element (easy with a loop)
Using c-strings - Answer-A c-string can be used like a normal array (of characters)
cout and cin objects also work with c-strings (for output and input of words)
>> operator for input stops at white space (space, tab, newline, etc.)
only good for one word at a time
get and getline for reading strings from input
get, getline read up to specified delimiter -- can read entire sentences
Arrays as function parameters - Answer-Know how to pass an array into a function
Usually a good idea to pass in a size as well
Function always has access to the array contents -- only the address is sent in
-There's no pass-by-value vs. pass-by-reference with arrays
Use const on the array parameter when the function shouldn't change the array
Array Usage and Algorithms - Answer-Understand how to handle arrays that are
declared to a certain size, but are not always "full" to their capacity
Understand common array algorithms and patterns, including (but not limited to):
-iterating through an array elements with a loop
-Printing array contents
-adding or counting array elements
-finding largest/smallest element of an array
-initializing array contents, with either formulas, user entry, or initializer list
-Using parallel arrays
-Swapping or moving around array elements
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller lectknancy. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $12.49. You're not tied to anything after your purchase.