Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
78 Cards in this Set
- Front
- Back
What do arrays allow you to do? |
store and work with a group values of the same data type. creates consecutive space in memory |
|
How do you define an array? |
int days[6]; data type name [size declarator]; size declarator indicates the number of elements the array can hold |
|
What are the guidelines for choosing the size declarator of an array? |
must be a constant integer expression with a value greater than zero. can be an integer literal or an integer named constant starts at 0. 6 elements: element 0, element 1, element 2, element 3, element 4, element 5 |
|
How do you calculate the size/memory requirements of an array?
|
multiply the size of the individual elements by the number of elements in the array |
|
The individual elements of an array are assigned unique ________ that are used to access the elements |
subscripts |
|
How do you assign the first element of the array hours to 20? |
hours[0]=20; read, "hours sub zero is assigned twenty" |
|
Do local arrays have default initialization values? What about global arrays?
|
local: no default global: default to 0 |
|
What is the difference between the array size declarator and subscript? |
[#] in definition: size declarator
[#] of assignment statement or any statement that works with the array: subscript size declarator: int days[6]; // defines an array with 6 elements subscript: days[5] = 20; // sets the 6th element to 20 cout << days[2]; // displays 3rd element of days |
|
The size declarator of an array definition must be a _________ or _______. The subscript may be a ____________.
|
declarator: literal or constant subscript: variable --> can use loops to cycle through arrays |
|
Can any integer expression be used as an array subscript?
|
yes cin >> hours[count-1]; // valid expression |
|
How do you need to input and output elements of an array? |
one at a time. Cannot just display or fill the whole array at once |
|
How do you read data from a file into an array? |
open the file and use a loop to read each item from the file, storing each item in an array element. loop should iterate until the array is filled or the end of the file is reached while (count < ARRAY_SIZE && inputFile >> numbers[count]) count++; inputFile >> numbers[count]; <-- It actually does this action and then tests the "truth" of whether it was able to |
|
How do you write the contents of an array to a file? |
use a loop to step through each element of the array, writing its contents to a file
for (count=0; countoutputFile << numbers[count]=count << endl; saves 0-9 in the file |
|
Does C++ prevent you from overwriting an array's bounds?
|
no |
|
What is a common mistake when working with arrays? |
off-by-one error easy to make because subscripts start at 0, not 1 |
|
How do you initialize an array?
|
put the initialization list inside braces and separate elements with commas int days[7] = {7, 6, 5, 4, 3, 2, 1}; or int days[7]={7,6, 5, 4,3,2,1} // can spread initialization list across multiple lines |
|
How can you force C++ to display the ASCII value of characters stored in an array named letters? |
cout << static_cast(letters[count]);
|
|
How do you partially initialize an array? What happens to the uninitialized elements?
|
assign values to the first n elements of the array the rest of the uninitialized elements are set to 0 |
|
Does C++ provide a way to skip elements in the initialization list? |
No. If you leave an element uninitialized, all elements that follow it must be uninitialized |
|
What is implicit array sizing? |
Defining an array without directly specifying the size. you must provide an initialization list int num[]={1,2,3,4} C++ will count the number of elements in the initialization list |
|
What is a range-based for loop?
|
loop that automatically iterates once for each element in an array. Each time the loop iterates, it copies an element from the array to a variable |
|
What are the benefits of using a range-based for-loop?
|
- don't need to use a counter variable to control the iterations because it automatically knows the number of elements in an array - don't need to worry about stepping outside the bounds of an array |
|
What is the general format of a range-based for loop? |
for (dataType rangeVariable : array) statement; dataType: data type of range variable must be the same as data type of array elements, or a type that they can be automatically converted to rangeVariable: name of the variable that receives the value of the array elements during each iteration array: name of the array for which you wish the loop to operate. statement: statement that executes during a loop iteration |
|
What does auto mean when it is used in a range-based for loop? |
key word that automatically specifies the range variable's data type
int numbers[] = {1, 2, 3} for (auto val : numbers) cout << val; |
|
How can you make a range-based for loop to modify the contents of an array? |
declare the range variable as a reference variable by including & in front of its name in the loop header for (int &val : numbers){ cout << "Enter an integer value: "; cin >> val;} |
|
When can you use a range-based for loop and when do you need to use a regular for loop? |
range-based: use in any situation where you need to step through the elements of an array and you do not need to use the element subscripts regular for: use if you need the element subscript for some reason |
|
How are individual array elements processed? |
processed like any other type of variable e.g., pay = hours[3]*rate; ++hours[2]; hours[2]++; |
|
What do you need to be careful of when using increment/decrement operators with arrays? |
don't confuse the subscript with the array element amount[count--]; vs. amount[count]--; |
|
How do you assign one array to another? |
assign the individual elements in the arrays, usually best done with a loop for (int count=0; count newValues[count]=oldValues[count]; |
|
You have an array named numbers. Why is this wrong? cout << numbers; What do you do instead? |
it interprets the name of the array as its address in memory, not its contents
Instead, use a loop to display the contents of the array's elements for (int count=0; count < SIZE; count++) cout << numbers[count]; or range-based for loop for (int val : numbers) cout << val; |
|
How do you sum the values in an array? |
Use a loop with an accumulator variable for (int val : units) total +=val; for (int count=0; count total +=units[counts] |
|
How do you find the average of the values in an array? |
const int NUM_SCORES=10; double scores[NUM_SCORES]; double total=0, average; for (int val : scores) total += val; average = total/NUM_SCORES; |
|
How do you find the highest and lowest values in a numeric array? |
copy the first element to a very use a loop to go through all elements if the new element is > or < that one, assign it to be the new highest or lowest |
|
When you process a partially filled array, you must only process the elements that contain _____ ______ _______ |
valid data items
|
|
A partially filled array is normally used with an accompanying _________ variable that holds the number of items to be stored in the arrray |
integer |
|
How do you determine whether two arrays are equal? |
Compare the individual elements. If you do array1[]==array2[], it will test whether their memory locations are the same e.g., use a while loop to compare the element. use a bool variable to flag whether they are still equal. use a counter to step through each element |
|
What does it mean to use parallel arrays? |
using the same subscripts to build relationships between data stored in two or more arrays |
|
When is an example of a time that it's useful to store data in two or more arrays? |
different types of data
|
|
How do you pass a single element of an array to a function? The full array? |
Single element: handled like any other variable function (array[0]); // don't need to do anything special w/ function parameters Full array: pass the name of the array function(array); |
|
When an entire array is passed to a function, it is passed by _______, not by _________
|
reference, not value (copying arrays would be very resource intensive) |
|
How do you set up a function to expect an array as a parameter? |
set up the function parameters to expect an arrayint function (int []); <-- function prototype that expects an array |
|
How do you prevent a function from making changes to an array argument? |
use const in the parameter declaration void showValues (const int[]); |
|
As a precaution, you should always use _______ array parameters in any function that is not intended to modify its array argument |
const That way, the code will fail to compile if you inadvertently write code that modifies the array |
|
When are two-dimensional arrays useful? |
storing multiple sets of data (think rows and columns) |
|
How do you define a two dimensional array? |
use two size declarators double scores [3][4]; first one: rows second one: columns |
|
How do you refer to a single element in a two-dimensional array? |
use both subscripts scores [0][1] row 0, column 1 |
|
How do programs typically cycle through all elements of two-dimensional arrays?
|
Nested loops (go through all iterations of columns for every row) |
|
How do you initialize a two-dimensional array? |
Enclose each row's initialization list in a set of braces int hours [3][2] = {{8,5}, {7,9}, {6,3}} //hours [0][0] = 8 // hours [0][1] = 5 // hours [1][0] = 7 // hours [1][1]=9 |
|
Are the extra set of braces in the initializations of two-dimensional arrays mandatory? |
No, but they help visually separate it |
|
How do you set up a function to accept a two-dimensional array? |
Must specify the number of columns in the function prototype and header void showArray (int [][4], int); |
|
How do you sum all elements of a two-dimensional array? |
use nested loops to add the contents of each element to an accumulator |
|
How do you sum the rows of a two-dimensional array? |
use a single loop to add all elements in one row |
|
Does C++ limit the number of dimensions an array may have? |
No. It is possible to create array with multiple dimensions to model data that occur in multiple sets |
|
It is helpful to think of three-dimensional arrays as _______ of two-dimensional arrays |
pages |
|
When writing functions that accept multi-dimensional arrays as arguments, ___ ___ ___ ___ ___ must be explicitly stated in the parameter list |
all but the first dimension must be explicitly stated in the parameter list |
|
What is the STL?
|
collection of programmer-defined data types and algorithms |
|
The data types in STL are called ______ because ________
|
containers: store and organize data |
|
What are the two types of containers in STL?
|
associative container: organizes data with keys, allowing for rapid, random access |
|
Describe the vector data type in STL. What kind of container is it? How is it similar to an array? What are the advantages over an array?
|
Similarities: stores a sequence of values in contiguous memory; can use array subscript [ ] to read individual elements Advantages: don't have to declare the number of elements, automatically increases size to accommodate new values, can report the number of elements they contain |
|
What header file do you include in order to use vectors? What namespace?
|
using namespace std; |
|
How do you define a vector?
|
vector < int >numbers;
//vector < data type > variableName;
Don't have to define starting size because it increases automatically, but you can if you feel like it vector numbers (10); //size is in (), not [] |
|
How do you copy an initialization value to all elements of a vector?
|
// vector with 10 elements. Each element is assigned to 2 |
|
How do you initialize a new vector with values from a vector that is already defined?
|
// set2 will be a copy of set1 |
|
How do you initialize a vector with a list of values?
|
// do not use = to assign values // automatically creates a vector with 4 elements |
|
How do you store and retrieve a value in an element that already exists in a vector?
|
|
|
Can you use a range-based for loop with vectors? |
vector numbers { 10, 20 , 30}; for (int val : numbers) cout << val << endl; |
|
Can you use reference variables with vectors? |
vector numbers(5); // vector w/ 5 elements for (int &val : numbers){ |
|
How do you store a value in a vector that does not have a starting size or that is already full? |
(pushes the value onto the back of the vector) e.g., numbers.push_back(25); |
|
How do you determine the size of a vector? |
size member function numValues = vectorName.size(); useful when writing functions that accept vectors as arguments; continue while count |
|
How do you remove the last element from a vector? |
pop_back vectorName.pop_back(); // removes last element from vectorName |
|
How do you clear a vector? |
vectorName.clear(); |
|
How do you detect an empty function? |
vectorName.empty(); |
|
How can you break out of a loop when a user enters a sentinel value?
|
break for loop{ // stuff if (value == sentinel #) break; // will break out of loop |
|
How do you return the value located at a specific element in the vector? |
at(element) x = vectorName.at(5); // returns value stored in 5th element |
|
How do you return the maximum number of elements that can be stored in a vector without additional memory being allocated? |
capacity() x = vectorName.capacity(); |
|
How do you reverse the order of elements in a vector? |
reverse() vectorName.reverse(); |
|
How do you resize a vector by a certain number of elements? |
resize (elements, value) vectorName.resize (5,1); //increase the size of the vector by 5 and initializes all 5 new elements to 1 |
|
How do you swap the contents of two vectors? |
swap vector1.swap(vector2); |