CH 7 review
7.1 Define the following arrays: A) empNums , a 100-element array of int s
array of 100 integers
7.1 Define the following arrays: E) lightYears , a 1,000-element array of double s
array of 1000 doubles
7.1 Define the following arrays: C) miles , a 14-element array of long s
array of 14 long integers
7.1 Define the following arrays: B) payRates , a 25-element array of float s
array of 25 floats
7.1 Define the following arrays: D) cityName , a 26-element array of string objects
array of 26 characters
31. When initializing a two-dimensional array, it helps to enclose each row's initialization list in _________.
braces
7.8 Define the following arrays: . C) alpha , an 8-element array of char s initialized with the values 'J', 'B', 'L', 'A', '*', '$', 'H', and 'M'.
char alpha[8]= ('J','B','L','A','*','$','H','M');
32. When a two-dimensional array is passed to a function the _________ size must be specified.
column
42. The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2
const int size = 100; int numberArray[size],numberArray2[size]; for (int i=0;) number Array 2[i] = numberArray 1[i];
7.22 Write a statement that displays the contents of the last column of the last row of the array defined in Question 7.20.
cout << sales[5] [3];
7.2 What's wrong with the following array definitions? int size; string names[size];
declartor must be a constant
7.10 Given the following array definition: int values[] = {2, 6, 10, 14}; What does each of the following display? A) cout << values[2];
displays 10
7.10 Given the following array definition: int values[] = {2, 6, 10, 14}; What does each of the following display? D) x = 2; cout << values[++x];
displays 14
7.10 Given the following array definition: int values[] = {2, 6, 10, 14}; What does each of the following display? B) cout << ++values[0];
displays 3
7.10 Given the following array definition: int values[] = {2, 6, 10, 14}; What does each of the following display? C) cout << values[1]++;
displays 6
7.21 Write a statement that assigns the value 56893.12 to the first column of the first row of the array defined in Question 7.20.
double sales [0] [0] = 56893.12;
7.20 How many elements are in the following array? double sales[6][4];
double sales[6] [4];
18. Subscript numbering in C++ always starts at _________.
zero
7.16 When used as function arguments, are arrays passed by value?
passed by reference
7.5 What is "array bounds checking"? Does C++ perform it?
prevents a program from using a subscript. c++ does not do array bonds checking.
29. It's best to think of a two-dimensional array as having _________ and _________.
rows, columns
7.4 What is the difference between an array's size declarator and a subscript?
size declarator : specifies number of elements in an array. subscript: used to access an individual element in an array.
7.2 What's wrong with the following array definitions? int readings[-1];
size declarator cant be negative
7.2 What's wrong with the following array definitions? float measurements[4.5];
size declarator must be an integer
17. Each element of an array is accessed and indexed by a number known as a(n) _________.
subscript
7.15 When an array name is passed to a function, what is actually being passed?
the address of the array
30. To define a two-dimensional array, _________ size declarators are required.
two
7.3 What would the valid subscript values be in a four-element array of doubles?
valid values are 0,1,2, and 3
12. Look at the following array definition. double sales[8][10]; How many rows does the array have? How many columns does the array have? How many elements does the array have? Write a statement that stores a number in the last column of the last row in the array.
8 rows 10 columns 80 elements
7.6 What is the output of the following code? int values[5], count; for (count = 0; count < 5; count++) values[count] = count + 1; for (count = 0; count < 5; count++) cout << values[count] << endl;
1 2 3 4 5
2. Look at the following array definition. int values[10]; How many elements does the array have? What is the subscript of the first element in the array? What is the subscript of the last element in the array? Assuming that an int uses four bytes of memory, how much memory does the array use?
10 zero 9 40 bytes
7.12 What is the output of the following code? (You may need to use a calculator.) double balance[5] = {100.0, 250.0, 325.0, 500.0, 1100.0}; const double INTRATE = 0.1; cout << fixed << showpoint << setprecision(2); for (int count = 0; count < 5; count++) cout << (balance[count] * INTRATE) << endl;
10.00 25.00 32.50 50.00 110.00
4. Consider the following array definition: int values[5] = { 4, 7, 6, 8, 2 }; What does each of the following statements display? cout << values[4] << endl; __________ cout << (values[2] + values[3]) << endl; __________ cout << ++values[1] << endl; __________
2 14 8
6. Look at the following array definition. int numbers[5] = { 1, 2, 3 }; What value is stored in numbers[2] ? What value is stored in numbers[4] ?
3 0
7.18 The following program skeleton, when completed, will ask the user to enter 10 integers, which are stored in an array. The function avgArray , which you must write, is to calculate and return the average of the numbers entered. #include <iostream> using namespace std; // Write your function prototype here int main() { const int SIZE = 10; int userNums[SIZE]; cout << "Enter 10 numbers: "; for (int count = 0; count < SIZE; count++) { cout << "#" << (count + 1) << " "; cin >> userNums[count]; } cout << "The average of those numbers is "; cout << avgArray(userNums, SIZE) << endl; return 0; } // // Write the function avgArray here. //
float avgArray(int[],int n) { float total= 0.0, average; for (int count = 0; count <n; count++) total += array[count]; average=total/10; return Average; }
7.8 Define the following arrays: B) temps , a 7-element array of float s initialized with the values 14.7, 16.3, 18.43, 21.09, 17.9, 18.76, and 26.7.
float temps[7]=(14.7,16.3,18.43,21.09,17.9,18.76,26.7)
41. names is an integer array with 20 elements. Write a regular for loop, as well as a range-based for loop that prints each element of the array.
for (int index=0; index<20; index++) cout << names[index] << endl;
7.8 Define the following arrays: A) ages , a 10-element array of int s initialized with the values 5, 7, 9, 14, 15, 17, 18, 19, 21, and 23.
int ages[10]=(5,7,9,14,15,17,18,19,21,23);
7.7 The following program skeleton contains a 20-element array of int s called fish . When completed, the program should ask how many fish were caught by fishermen 1 through 20, and store this data in the array. Complete the program. #include <iostream> using namespace std; int main() { const int NUM_FISH = 20; int fish[NUM_FISH]; // You must finish this program. It should ask how // many fish were caught by fishermen 1-20, and // store this data in the array fish. return 0;
int fish [num_fish], count; cout << "Enter amount of fish caught:"; for (count = 0; count<20;count++) { cout << "fisherman"<< [count+1]; cin >> fish [count]; } return 0; }
7.19 Define a two-dimensional array of int s named grades . It should have 30 rows and 10 columns.
int grades [30] [10];
7.26 A video rental store keeps DVDs on 50 racks with 10 shelves each. Each shelf holds 25 DVDs. Define a three-dimensional array large enough to represent the store's storage system.
int vidnum[50][10][25];