C++ Chapter 7: Arrays
True
(T or F) Array subscript numbering in C++ always starts with a zero.
False
(T or F) Subscript numbers cannot be stored in variables.
(32.50)
What will be the third line of output displayed in the program shown in Checkpoint 7.12 on p. 404?
integer greater than zero ( either a named constant, or a literal integer. The following both work when INDEX is an integer > 0. float meters[INDEX]; string people[100]; )
An array's size declarator must be an ___ .
actual array
Any changes that a function makes to the array are made to the...
prototype ( void showScores(int [], int); // function prototype void showScores(int tests[], int size) // function header )
Array size must also be reflected in the...
[]
Arrays are declared using ___ operator
any data type
Arrays can be defined for which data types?
initialization list ( const int SIZE = 5; int tests[SIZE] = {79, 82, 91, 77, 84}; )
Arrays can be initialized with an ...
integer expression ( int a = 4, b = 7; cout << numbers[a] << endl; average = (numbers[a] + numbers[b]) / 2.0; cout << numbers[a + b]; )
Arrays can use an integer variable or ___ as a subscript
individual elements
Arrays must be accessed via
char letters[1000]; string people[100]; float meters[INDEX];
Assume that INDEX is an integer constant greater than zero. Which of the following are valid array definitions? (Check all that apply.) double miles[0]; array numbers[INDEX]; char letters[1000]; string people[100]; double kilometers(200); float meters[INDEX];
30
Assume that amount is a double and numbers is an array defined as int numbers[ ] = {5, 10, 15, 25, 20, 30, 10}. What will be the value of amount after the following statement executes? amount = numbers[4] * 2 - 10;
Method 2 (The one with the for loop, duh.)
Assume that array1 and array2 are arrays of the same data type and ARRAY_SIZE is equal to the number of elements for each array. Which method would be the correct way to assign the contents of array1 to array2? Method 1 array2 = array1; Method 2 for (int count = 0; count < ARRAY_SIZE; count++) array2[count] = array1[count]; Method 3 array2[ ] = array1[ ];
False
Assume that array1 and array2 are arrays of the same data type. The statement array1 == array2 will return true if the contents of the two arrays are exactly the same.
(Correct, will cause a crash) age[count - 6] = 25; age[50] = 34; age[10 * count + 5] = 58; ( age[9 * count - 38] = 18; age[10 * count - 1] = 22; age[5 * count - 4] = 39; )
Assume that count is an int whose value is 5 and age is an array defined as int age[50];. Which of the following may cause a program to crash? (Check all that apply.) age[count - 6] = 25; age[50] = 34; age[10 * count + 5] = 58; age[9 * count - 38] = 18; age[10 * count - 1] = 22; age[5 * count - 4] = 39;
(Correct) ( age[count * 3.46] = 20; ) age[count] = 45; age[count - 5] = count + 1; age[count - 1] = 35; ( age[count + 2.5] = 30; ) age[2 * count] = 22;
Assume that count is an int whose value is 5 and age is an int array that can hold 100 values. Which of the following are valid assignment statements? (Check all that apply.) age[count * 3.46] = 20; age[count] = 45; age[count - 5] = count + 1; age[count - 1] = 35; age[count + 2.5] = 30; age[2 * count] = 22;
Method 3 (The one with the for loop, duh.)
Assume that numbers is an array defined as int numbers[3] = {23, 44, 56};. Which method would be the correct way to display the contents of numbers? Method 1 cout << numbers << endl; Method 2 cout << numbers[ ] << endl; Method 3 for (int count = 0; count < 3; count++) cout << numbers[count] << endl;
2
Assume that the array numbers is defined as shown below. What value is stored in the array element numbers[1] [0]? int numbers[2] [3] = {{4, 1, 0}, {2, 8, 7}};
sales [2] [3] = 15.25;
Assume that the array sales is defined as shown below. Which statement would assign the value 15.25 to the last column of the last row? double sales [3] [4]; sales [4] [3] = 15.25; sales [2] [3] = 15.25; sales [3] [4] = 15.25; sales [3] [2] = 15.25;
sales[0][1] = 345.55;
Assume that the array sales is defined as shown below. Write a statement that would assign the value 345.55 to the second column of the first row. double sales [3] [4];
26
Assume values is an array defined as int values[ ] = {10, 20, 5, 25, 40, 40};. What will be displayed by the following code segment? values[0] = values[0] * values[2] - values[3]; values[0] += 1; cout << values[0];
1 2 and 4 ( values[3] == values[1] + values[2] values[0] > values[2] values[2] != values[1] - 15 values[4] == values[5] values[5] == values[0] + 20 )
Assume values is an array defined as int values[ ] = {10, 20, 5, 25, 40, 40};. Which of the following relational expressions will evaluate to true? (Check all that apply.) values[3] == values[1] + values[2] values[0] > values[2] values[2] != values[1] - 15 values[4] == values[5] values[5] == values[0] + 20
2 calculate(values, SIZE);
Assume values is an array of any data type and SIZE equals to the number of elements that values contains. To pass values and SIZE to a function named calculate, which statement would be correct? calculate(values[ ], SIZE); calculate(values, SIZE); calculate(values(), SIZE); calculate(values[SIZE], SIZE);
for loop (seriously. this is like the only reason for loops exist at this point.)
Do to basically ANYTHING with arrays you have to use a...
No
Does C++ perform bounds checking? (in other words, does it check to make sure you're not trying to write more than 3 values to a 3-element array?)
4
What will be the size of the numbers array after it is defined as shown below? double numbers[ ] = {6.5, 8.9, 7.4, 7.5};
<
Fill in the blank with the correct operator. for (int index = 0; index ??? SIZE; index++) ----sum = sum + tests[index];
(2 3 and 4)
For Program 7-13 on p. 405, which of the following statements are true? (Check all that apply.) 1. The entire numbers array is passed to the showValue function . 2. The showValue function displays one integer value each time it is called. 3. numbers[index] is passed to the showValue function by value. 4. An array element is passed to the showValue function like any other variable .
(2 3 4 and 5)
For Program 7-14 on p. 406, which of the following statements are true? (Check all that apply.) The variable ARRAY_SIZE is passed to the showValues function by reference. 1. The variable ARRAY_SIZE is passed to the showValues function by value. 2. The entire numbers array is passed to the showValues function. 3. The showValues function can be used to display the contents of any integer array. 4. The numbers array is passed to the showValues function by reference. 5. The numbers array is passed to the showValues function by value.
4 5 and 6 ( 4. The getTotal function cannot change the contents of the testScores array. 5. The getLowest function cannot change the contents of the testScores array. 6. If the variable total is not initialized to zero on line 75, the getTotal function will return an incorrect value. )
For Program 7-17 on p. 411, three of the following statements are true and three are false. Which three statements are true? 1. The getTestScores function cannot change the contents of the testScores array. 2. The testScores array is passed to the getTotal function by value. 3. The getLowest function will test every element in the testScores array to determine if it is less than the value of lowest. 4. The getTotal function cannot change the contents of the testScores array. 5. The getLowest function cannot change the contents of the testScores array. 6. If the variable total is not initialized to zero on line 75, the getTotal function will return an incorrect value.
3 (two corresponding elements in firstArray and secondArray have different values.)
For the code at the bottom of p. 401, while (arraysEqual && count < SIZE) { ----if (firstArray[count] != secondArray[count]) --------arraysEqual = false; ----count++ } the bool variable arraysEqual is set to false when 1. it is determined that the size of firstArray and secondArray are not equal. 2. the variable count becomes equal to SIZE. 3. two corresponding elements in firstArray and secondArray have different values. 4. two corresponding elements in firstArray and secondArray have the same values.
(81)
For the code shown in Checkpoint 7.13 on p. 404, what value will be stored in dist[2] after the code executes?
void getData(double [][COLS], string );
Given the following information, create the appropriate prototype statement for the getData function. // Declare global constants. const int COLS = 3, -----------ROWS = 4; // Declare arrays. double sales[ROWS][COLS], ---------rowTotals[ROWS], ---------columnTotals[COLS]; // Read data from a text file and store in the sales array. getData(sales, "sales_data.txt"); // *************************************************************** // getData. This function reads data from //sales_data.txt and stores it in the // sales array. // **************************************************************** void getData(double sales[][COLS], string file_name) { }
40 (the formula is just the rows * columns)
How many elements are in the following array? double sales [5] [8];
error
If a statement in showValues attempts to modify the numbers array, an ___ will occur at compile time. void showValues(const int numbers[], int size) { for (int index = 0; index < size; index++) cout << numbers[index] << " "; cout << endl; }
0
If an array that is not a string array is only partially initialized, the uninitialized elements will be set to
distance[30] distance[31]
If distance is an array as defined below, which array elements do not exist for this array? (Check all that apply.) double distance[30]; distance[29] distance[0] distance[30] distance[31]
large enough to hold the largest expected number of elements the number of items stored in the array
If it is unknown how much data an array will be holding: •Make the array... and •Use a counter variable to keep track of...
initialization list ( int quizzes[]={12, 17, 15, 11}; the array size will be 4, or 0 through 3 )
Implicit array sizing can determine array size by the size of the
either of these
In Program 7-11 on p. 400: while (count < ARRAY_SIZE && inputFile >> numbers[count]) ----count++ , data is read from numbers.txt until which of the following conditions becomes true? either of these the end of the numbers.txt file is reached count is equal to ARRAY_SIZE
(Employee #3: $481.25)
In Program 7-12 on p. 402, if the user enters the data shown below for hours and payRate, what will be the third line of output displayed within the for loop that begins on line 28? 20 11.75 28 14.85 35 13.75 24 15.75 40 16.80 (you can skip this one if you want)
(275)
In Program 7-17 on p. 411, if the user enters the data shown below, what value will be assigned to total on line 34? 87 95 88 92
12 (the formula is (NUM_DIVS)*(NUM_QTRS) so long as the test operator is < and the accumulator is incremented each iteration, which it is in this case)
In Program 7-18 on p. 417, NUM_DIVS = 3; NUM_QTRS = 4; for (div = 0; div < NUM_DIVS; div++) ----for (qtr = 0; qtr < NUM_QTRS; qtr++) ----{ 26--cin >> sales[div][qtr]; ----} how many times will the statement on line 26 execute? (In other words, how many times will the second loop iterate?)
1 2 and 3
In Program 7-19 on p. 419, which statements are true about the function header on line 37? (Check all that apply.) 1. COLS specifies the number of columns of the array passed to the showArray function. 2. COLS is a global constant. 3. The value of COLS is 4.
(Score average for student 3 is 80.6)
In the code at the bottom of p. 421 and the top of p. 422, what will be the last line of output displayed?
Yes
Initializing the Elements of a Two-Dimensional Array Are the following initialization statements equivalent? int numbers[3][2] = {{5, 4}, {8, 6}, {12, 45}}; int numbers[3][2] = {5, 4, 8, 6, 12, 45};
5 4\n 8 6\n 12 45
Initializing the Elements of a Two-Dimensional Array Write the resulting two-dimensional array values in their proper orientation, using \n to indicate a shift to the lower row. int numbers[3][2] = {{5, 4}, {8, 6}, {12, 45}};
int numbers[3][2] = {{5}, {8, 6}, {12}};
Partial Initialization of the Elements of a Two-Dimensional Array Write an array initialization statement with the integer array numbers, that creates the value table in the image
inputFile >> numbers[count];
Reading Data from a File into an Array: const int ARRAY_SIZE = 10; int numbers[ARRAY_SIZE ]; ifstream inputFile; int count = 0; while (count < ARRAY_SIZE) { // Write the correct code here. -count++; }
ok
Study the image.
zero
Subscript numbering in C++ always starts at
int sum = 0; for (int row = 0; row < ROWS; row++) ----for (int col = 0; col < COLS; col++) --------sum = sum + numbers[row][col];
Summing all Elements of aTwo-Dimensional Arrays Write a nested for loop that sums the values of the array elements together const int ROWS = 3; COLS = 4; int numbers[ROWS][COLS] = {{5, 9, 3, 8}, -----------------------------------{4, 8, 6, 1}, -----------------------------------{0, 2, 9, 5}};
49
The 50th element of an array is assigned which subscript?
const
The __ key word below prevents function showValues from making changes to the numbers array. void showValues(const int numbers[], int size) { -for (int index = 0; index < size; index++) --cout << numbers[index] << " "; -cout << endl; }
True
The const key word in the following statement prevents function showValues from changing the contents of the nums array. void showValues(const int nums[ ], int size); True False
Kenneth, Helen, and Amanda
What will be displayed by the following code segment? const int MAX_NAMES = 7; string names[MAX_NAMES] = {"Tom", "Helen", "Mary", "Stanley", "Amanda", "Jerome", "Kenneth"}; cout << names[6] << ", " << names[1] << ", and " -----<< names[4];
0 1 2 3 4
The definition: int tests[5]; List the elements from the first number to the last
subscripts
The individual elements of an array are assigned unique _______ which are used to access the elements.
size
The initialization list cannot exceed the array...
constant integer expression, zero
The size declarator must be a(n) _________ with a value greater than _________.
size declarator
This appears in an array declaration and specifies the number of elements in the array.
empty [] ( void showScores(int []); // function prototype void showScores(int tests[]) // function header )
To define a function that takes an array parameter, use ___ for array argument:
array name ( showScores(tests); )
To pass an array to a function, just use the
parallel
Two or more arrays that have the same size in which there exists a relationship between the data stored in the arrays are called ________ arrays. similar like parallel two-dimensional compatible
rows
Two-Dimensional Arrays int numbers[3][4]; 3 is the number of...
columns
Two-Dimensional Arrays int numbers[3][4]; 4 is the number of...
invalid subscripts.
Use of ___ ___ can corrupt other memory locations, crash program, or lock up computer, and cause elusive bugs.
200 560 300
Use the image to solve the following problems 1. numbers[2][1] = ? 2. numbers[2][0] + numbers[1][2] = ? 3. int sum = 0; for (int row = 0; row < 4; row++) = ? sum = sum + numbers[row][1];
adjacent
Values are stored in ___ memory locations
finds the minimum (determines the minimum value in the numbers array)
What does the following code segment do? const int SIZE = 6; int numbers[SIZE] = {10, 3, -10, -15, 40, 35}; int x = numbers[0]; for (int count = 1; count < SIZE; count++) ----if (numbers[count] < x) --------x = numbers[count];
finds the maximum (determines the maximum value in the numbers array)
What does the following code segment do? const int SIZE = 6; int numbers[SIZE] = {10, 3, -10, -15, 40, 35}; int x = numbers[0]; for (int count = 1; count < SIZE; count++) ----if (numbers[count] > x) --------x = numbers[count];
(4 52 208)
What will be the fourth line of output displayed in the code shown in Checkpoint 7.13 on p. 404?
110
What will be the value of total after the following code segment executes? int numbers[4] = {20, 10, 30, 50}; int total = 0; for (int count = 0; count < 4; count++) ----total += numbers[count];
Amount = 1.91
What will the following program segment display? double coins[6] = {0.01, 0.05, 0.1, 0.25, 0.5, 1.0}; double amount = 0; for (int count = 0; count <= 5; count++) ----amount = amount + coins[count]; cout << "Amount = " << amount << endl;
number of columns
When a two-dimensional array is passed to a function, the array parameter type in the function header must contain a size declarator for the...
size declarator for the number of columns ( void showArray(const int array[][COLS], int rows) { for (int x = 0; x < rows; x++) { for (int y = 0; y < COLS; y++) cout << setw(4) << array[x][y] << " "; cout << endl; } } // COLS is a global constant. )
When a two-dimensional array is passed to a function, the corresponding parameter must contain a...
the starting address of the array
When an array name is passed to a function, what is actually being passed? only the first element of the array a copy of the entire contents of the array none of these the starting address of the array
reference
When an entire array is passed to a function, it is passed by...
No (You have to provide a size declarator for the columns in the prototype and function header, but not the call. See examples below. // Header void showArray(const int array[][COLS], int rows) //Prototype void showArray(const int [][COLS], int); // Function call showArray(table1, TBL1_ROWS);
When passing a two-dimensional array as an argument in a function call, do you have to provide a size declarator for the number of columns?
array size ( showScores(tests, ARRAY_SIZE); )
When passing an array to a function, it is common to pass the ___ so the function knows how many elements to process:
0 ( for (int index = 0; index < SIZE; index++) ----sum = sum + tests[index]; )
When using for loops to handle arrays, you want the accumulator variable to be initiated to ___
valid
When you use a value as an array subscript, C++ does not check it to make sure it is a ___ subscript.
1 because it provides a size declarator for the number of columns
Which is the correct prototype statement and why? 1. void showArray(const int [][COLS], int); 2. void showArray(const int [][], int);
1 2 and 3
Which of the following statements are true about passing an entire array to a function? (Check all that apply.) 1. the array can be passed only by reference 2. the array name is followed by a pair of empty brackets in the function's parameter list 3. the array's data type is followed by a pair of empty brackets in the function prototype 4. the array can be passed by either reference or value 5. the array can be passed only by value
1 2 3 4 6 ( 1. In the array definition the second size declarator specifies the number of columns. 2. Programs that cycle through each element usually do so with nested loops. 3. In the array definition the first size declarator specifies the number of rows. 4. Each element in the array is referenced by two subscripts. 5. The first subscript of an array element will reference its column. 6. Two size declarators are required to define the array. 7. The second subscript of an array element will reference its row. )
Which of the following statements are true about two-dimensional arrays? (Check all that apply.) 1. In the array definition the second size declarator specifies the number of columns. 2. Programs that cycle through each element usually do so with nested loops. 3. In the array definition the first size declarator specifies the number of rows. 4. Each element in the array is referenced by two subscripts. 5. The first subscript of an array element will reference its column. 6. Two size declarators are required to define the array. 7. The second subscript of an array element will reference its row.
miles[4] = 40;
Which of the following statements will store the number 40 in the fifth element of the array miles? miles[5] = 40; none of these miles[3] = 40; miles[4] = 40;
int grades[30] [10];
Which of the following would be the correct definition to define a two-dimensional array of ints named grades that has 30 rows and 10 columns. int grades[30, 10]; int grades[10, 30]; int grades[30] [10]; int grades[10] [30];
double distance[400];
Which of the following would define a double array named distance that contains 400 elements? double distance[400]; double distance{400}; double distance(400); double distance;
arrays can hold values of different data types
Which statement is false about an array data structure? arrays can hold values of different data types it can hold one or more values of the same data type the size declaration indicates the number of elements or values the array can hold an array's size declarator must be an integer value the values in an array are stored together in consecutive memory locations
If an array is defined locally, all of its elements are initialized to zero by default.
Which statement is false? The expression hours[0] is pronounced "hours sub zero". If an array is defined locally, none of its elements have a default initialization value. If an array is defined locally, all of its elements are initialized to zero by default. If an array is defined globally, all of its elements are initialized to zero by default.
Yes
Will the following array assignment loop copy the contents of the numbers array into the new_numbers array? const int SIZE = 100; int new_numbers[SIZE], numbers[SIZE]; for (index = 0; index < ARRAY_SIZE; index++) ----new_numbers[index] = numbers[index];
No
Will the following array assignment statement copy the contents of the numbers array into the new_numbers array? const int SIZE = 100; int new_numbers[SIZE], numbers[SIZE]; new_numbers = numbers;
Yes
Will the following code segment assign the number 25 to each element of the age array? int age[100]; for (int count = 0; count <= 99; count++) -age[count] = 25;
Named constants
___ are commonly used as size declarators
Global array
all elements initialized to 0 by default
Local array
all elements not initialized by default
if (numbers[index] > maximum) maximum = numbers[index];
int maximum; // Set maximum to the first array element. maximum = numbers[0]; // Find the maximum value in the array. for (int index = 1; index < SIZE; index++) { //Write the correct code here }
for (count = 0; count < ARRAY_SIZE; count++) -outputFile << numbers[count] << endl;
const int ARRAY_SIZE = 10; int numbers[ARRAY_SIZE]; ofstream outputFile; int count; // Store values in the array. for (count = 0; count < ARRAY_SIZE; count++) -numbers[count] = count; //Open a file for output. outputFile.open("SavedNumbers.txt"); // Write the array contents to the file. // write the correct code here. // Close the file. outputFile.close();
for(int index = 0; index < SIZE; index++) sum = sum + tests[index]; average = sum / SIZE;
const int SIZE = 4; double tests[SIZE] = {90, 80, 100, 90}; double average, sum = 0; // Calculate the sum of all tests. // Write the correct code here. // Calculate the average of the tests. // Write the correct code here.
appear
const int SIZE = 5; int tests[SIZE] = {79, 82, 91, 77, 84}; The values are stored in the array in the order in which they ___ in the list.
array data type
int numbers[5]; numbers[0] = 20; numbers[1] = 50; numbers[2] = 3; numbers[3] = 100; numbers[4] = 75;
array element
int numbers[5]; numbers[0] = 20; numbers[1] = 50; numbers[2] = 3; numbers[3] = 100; numbers[4] = 75;
array name
int numbers[5]; numbers[0] = 20; numbers[1] = 50; numbers[2] = 3; numbers[3] = 100; numbers[4] = 75;
array size declarator
int numbers[5]; numbers[0] = 20; numbers[1] = 50; numbers[2] = 3; numbers[3] = 100; numbers[4] = 75;
array subscript
int numbers[5]; numbers[0] = 20; numbers[1] = 50; numbers[2] = 3; numbers[3] = 100; numbers[4] = 75;
Array Declaration
int tests[5];
names sub zero
names[0] is pronounced ...
Parallel arrays
two or more arrays that contain related data.
elements
values an array can hold
Array
variable that can store multiple values of the same type
for
when using arrays, data should be saved to a file with a ___ loop.
while
when using arrays, files should be read with a ___ loop.