C++ Chapter 7
initialization list
can be initialized using this; values are stored in array in order in which they appear in the list { }; initialization list cannot exceed array size
two dimensional array
can define one array for multiple sets of data; like a table in a spreadsheet; use two size declarators in definition: const int Rows = 4, Cols = 3; int exams [Rows] [Cols];
off-by-one errors
An ___-____-____ error happens when you use array subscripts that are off by one; can happen when you start subscripts at 1 rather than 0
printing char contents of an array
can display contests of character array by sending its name to cout: char fName[ ] = "Henry"; cout << fName << endl;
recognize array size
common to pass array size so that function knows how many elements to process: showScores(tests, ARRAY_SIZE); array size must also be reflected in prototype, header: void showScores (int [], int); void showScores(int tests[ ], int size)
array assignment
for (i = 0; i< array_size; i++) newTests[i] = tests [i];
printing contests of an array
for other types of arrays, you must print element-by-element: for (i=0; i < ARRAY_SIZE; i++) cout << tests[i] << endl;
partial array initialization
if array is initialized with fewer values than the size declarator, the remaining elements will be set to 0
partially-filled arrays
if it is unknown how much data an array will be holding: -make the array large enough to hold largest expected number of elements -use a counter variable to keep track of the number of items stored in the array
tests[i++]
increment i, no effect on tests
vector<int> numbers {1, 2, 3, 4};
initialize vector with list of values 1, 2, 3, 4
declaring vectors
must #include<vector>
size declarators
named constants are commonly used as size declarators; eases program maintenance when size of array needs to be changed
showScores(tests);
pass array to a function using showScores example
implicit array sizing
Can determine array size by the size of the initialization list: Must use either array size declarator or initialization list at array definition
STL vector
a data type defined in the Standard Template Library; can hold values of any type; automatically adds space as more is needed - no need to determine size at definition; can use [ ] to access elements
scores.push_back(75);
add element 75 to full array/array that had no size definition to score array
resize(elts, val)
add elements to a vector, optionally initialized them
tests[i]++
adds 1 to tests[i]
global array
all elements initialized to 0 by default
local array
all elements uninitialized by default
modifying arrays in function
array names in functions are like reference variables - changes made to array in a function are reflected in actual array in calling function
vector<int> scores(30,0)
declare a vector and initialize all elements to 0
vector<int> finals(scores)
declare a vector initialized to size and contents of another vector
vector<int> scores;
declare a vector to hold int element
vector<int> scores 30
declare a vector with initial size 30
int tests[test];
declare array example
scores.size();
determine size of scores array
swap(vec2)
exchange the contents of two vectors
scores.clear();
remove all contents of vector scores
scores.pop_back();
remove last element from vector scores
capacity()
returns max number of elements a vector can store without allocating more memory
at(elt)
returns the value of the element at position elt in the vector
reverse()
reverse the order of the elements in a vector
comparing arrays
to _________ two arrays, you must compare element-by-element
[ ]
to define a function that takes an array parameter, use empty ___ for array argument
while (!scores.empty())
to determine if vector scores is empty
size of array
total number of bytes allocated for it ; #elements*#bytes each element
parallel arrays
two or more arrays that contain related data; a subscript is used to relate arrays: elements at same subscript are related; may be of different types
2D array initialization
two-dimensional arrays are initialized row-by-row; can omit inner { }, some initial values in a row - array elements without initial value will be set to 0 or null
summing and averaging array elements
use simple loop to add together array elements: int tnum; double average, sum=0; for(tnum=0; tnum<SIZE; tnum++) sum += tests[tnum]; once summed, can compute average: average = sum/size
array
variable that can store multiple values of the same type; values stored in adjacent memory locations; declared using [] operator