Chapter 13
MULTIDIMENSIONAL ARRAYS
QUESTIONS
True/False
1. The array declared as
float angle[10][25];
has 10 rows and 25 columns.
2. The array declared as
int bowlingScore[6][12];
contains 72
int components.3. The array declared as
float age[4][5][6][9];
has four dimensions.
4. The statement
int scaleFactor[2][4] =
{
{5, 2, 8, -2},
{6, 0, 1, 7}
};
is a valid statement for declaring and initializing the
scaleFactor array.5. If a program contains the declaration
int salePrice[100][100];
then the statement
cout << salePrice[3];
outputs all the values in row 3 of the array.
6. In the computer's memory, C++ stores two-dimensional arrays in row order.
7. When a two-dimensional array is declared as a formal parameter, the C++ compiler ignores the sizes of both dimensions.
8. When you declare a two-dimensional array as a formal parameter, you can omit the size of the first dimension but not the second.
9. When you declare an N-dimensional array as a formal parameter, you can omit the sizes of all but the last dimension.
10. When a two-dimensional array is passed as a parameter, the number of columns in the formal parameter must be identical to the number of columns in the actual parameter.
11. When a two-dimensional array is passed as a parameter, the number of rows in the formal parameter must be identical to the number of rows in the actual parameter.
12. A two-dimensional array can be viewed as a one-dimensional array of one-dimensional arrays.
13. If a program contains the declarations
typedef char NameString[31];
NameString studentName[20];
then the statement
cin >> studentName[2];
is a valid statement.
14. If a program contains the declarations
typedef char NameString[31];
NameString studentName[20];
then the statement
cin >> studentName[2][5];
is a valid statement.
Multiple Choice
15. Given the declaration
char alpha[25][3];
how many
char components does alpha have?a. 25
b. 28
c. 75
d. 3
e. 100
16. Given the declaration
float costOfGoods[8][2][7];
how many
float components does costOfGoods have?a. 8
b. 70
c. 72
d. 112
e. 17
17. Given the declarations
float x[300];
float y[75][4];
float z[79];
which of the following statements is true?
a.
b.
y has more components than x.c.
y and z have the same number of components.d.
x and y have the same number of components.e. a and c above
18. If
a. the fourth number in the third row of the array
b. the third number in the fourth row of the array
c. the fourth number in the third column of the array
d. the third number in the fourth column of the array
e. a and d above
19. Given the declaration
char table[7][9];
which of the following stores the character 'B' into the fifth row and second column of the array?
a.
table[4][1] = 'B';b.
table[1][4] = 'B';c.
table[5][2] = 'B';d.
table[2][5] = 'B';e.
table[5] = 'B';20. A weather office uses a program with the declarations
enum Cities {SAN_DIEGO, CHICAGO, BOSTON};
Boolean sunny[3][31];
The first dimension of the array is indexed by a value of type
Cities, and the second is indexed by a day of the month. Which of the following could be used to record the fact that it was sunny in Boston on the twentieth day of the month?a.
sunny[19][TRUE] = BOSTON;b.
sunny[TRUE][BOSTON] = 19;c.
sunny[BOSTON][19] = TRUE;d.
sunny[19][BOSTON] = TRUE;e.
sunny[BOSTON][TRUE] = 19;21. The following program fragment is intended to zero out a two-dimensional array:
int table[10][20];
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 20; j++)
// Statement is missing here
What is the missing statement?
a.
table[i][j] = 0;b.
table[j][i] = 0;c.
table[i+1][j+1] = 0;d.
table[j+1][i+1] = 0;e.
table[i-1][j-1] = 0;22. The following program fragment is intended to zero out a two-dimensional array:
int table[10][20];
int i, j;
for (j = 0; j < 20; j++)
for (i = 0; i < 10; i++)
// Statement is missing here
What is the missing statement?
a.
table[i][j] = 0;b.
table[j][i] = 0;c.
table[i+1][j+1] = 0;d.
table[j+1][i+1] = 0;e.
table[i-1][j-1] = 0;23. Given the nested For loops
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
cout << table[i][j];
what is the appropriate declaration for
table?a.
int table[M][N];
b.
int table[N][M];c.
int table[M+N];d.
int table[M+1][N+1];e.
int table[N+1][M+1];24. Given the declarations
float alpha[5][50];
float sum = 0.0;
which of the following computes the sum of the elements in row 2 of
alpha?a.
for (i = 0; i < 5; i++)sum = sum + alpha[i][2];
b.
for (i = 0; i < 50; i++)sum = sum + alpha[i][2];
c.
for (i = 0; i < 5; i++)sum = sum + alpha[2][i];
d.
for (i = 0; i < 50; i++)sum = sum + alpha[2][i];
25. Given the declaration
int score[5][8];
which of the following outputs the array components in row order?
a.
for (j = 0; j < 8; j++)for (i = 0; i < 5; i++)
cout << score[i][j];
b.
for (i = 0; i < 8; i++)for (j = 0; j < 5; j++)
cout << score[i][j];
c.
for (i = 0; i < 5; i++)for (j = 0; j < 8; j++)
cout << score[i][j];
d.
for (j = 0; j < 5; j++)for (i = 0; i < 8; i++)
cout << score[i][j];
e.
for (i = 0; i < 8; i++)for (j = 0; j < 5; j++)
cout << score[j][i];
26. After execution of the program fragment
int table[3][3];
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
table[i][j] = i + 2*j;
what are the contents of the
table array?a.
0 2 41 3 5
2 4 6
b.
0 1 22 3 4
4 5 6
c.
0 2 41 3 5
0 0 0
d.
0 1 02 3 0
4 5 0
27. After execution of the program fragment
int table[3][3];
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
table[j][i] = i + 2*j;
what are the contents of the
table array?a.
0 2 41 3 5
2 4 6
b.
0 1 22 3 4
4 5 6
c.
0 2 41 3 5
0 0 0
d.
0 1 02 3 0
4 5 0
28. What does the following code fragment do? (All variables are of type
position1 = -1;
position2 = -1;
for (j = 0; j < 50; j++)
for (i = 0; i < 50; i++)
if (table[i][j] == searchValue)
{
position1 = i;
position2 = j;
}
a. It searches the table in row order for the first occurrence of
b. It searches the table in row order for the last occurrence of
searchValue.c. It searches the table in column order for the first occurrence of
searchValue.d. It searches the table in column order for the last occurrence of
searchValue.29. The following code fragment invokes a function named
InitToZero:int alpha[10][20];
InitToZero(alpha);
Which of the following is a valid function heading for
InitToZero?a.
void InitToZero( int beta[][] )b.
void InitToZero( int beta[10][20] )c.
void InitToZero( int beta[10][] )d.
void InitToZero( int beta[][20] )e. b and d above
30. A rental car company needs to keep track of the number of cars that are being rented at any given time by make and year. Which of the following data structures is most appropriate for this problem?
a. a one-dimensional array
b. a two-dimensional array
c. a three-dimensional array
d. parallel one-dimensional arrays
31. A builder wants to keep track of the names of the different models of homes that she builds, their prices, and the number of bedrooms that each has. Which of the following data structures is most appropriate for this problem?
a. a one-dimensional array
b. a two-dimensional array
c. a three-dimensional array
d. parallel one-dimensional arrays
32. A teacher would like to keep track of attendance in the courses he teaches by course name and day of the week. Which of the following data structures is most appropriate for this problem?
a. a one-dimensional array
b. a two-dimensional array
c. a three-dimensional array
d. parallel one-dimensional arrays
33. A video rental store wants to keep track of its inventory of tapes by type of movie (for example, comedy), manufacturer, and date of copyright. Which of the following data structures is most appropriate for this problem?
a. a one-dimensional array
b. a two-dimensional array
c. a three-dimensional array
d. parallel one-dimensional arrays
Fill-In
34. A(n) ____________________ is a collection of components, all of the same type, structured in two dimensions. Each component is accessed by a pair of index values that represent the component's position in each dimension.
35. Accessing array elements in an arbitrary, rather than sequential, order is called ____________________ access.
36. A(n) ____________________ is a collection of components, all of the same type, structured in N dimensions (N >= 1). Each component is accessed by N index values, each of which represents the component's position within that dimension.
37. The array declared as
float height[16][4];
contains ____________________ (how many)
float components?38. The array declared as
int equipmentCount[4][7][2];
contains ____________________ (how many)
int components?39. Given the declarations
typedef int RoomNumber[100];
RoomNumber capacity[20];
the
capacity array contains ____________________ (how many) int components?40. Write the declaration of a two-dimensional array named
twoDim that has 10 rows and 6 columns and whose components are of type float: ____________________41. Given the declaration
enum Breeds {COLLIE, BASSET, POODLE, DACHSHUND, CHOW};
write the declaration of a two-dimensional array named
twoDim whose rows are indexed by values of type Breeds, columns are indexed by 0 through 9, and component type is int: ____________________42. Given the declaration
enum Breeds {COLLIE, BASSET, POODLE, DACHSHUND, CHOW};
write the declaration of a two-dimensional array named
twoDim whose rows are indexed by 0 through 49, columns are indexed by 0 through 75, and component type is Breeds: ____________________43. Given the declarations
enum Names {BILL, ANNE, PEG};
enum Tests {EX1, EX2, EX3, FINAL};
write the declaration of a two-dimensional array named
gradeBook whose rows are indexed by values of type Names, columns are indexed by values of type Tests, and component type is int: ____________________44. Write the declaration of a three-dimensional array named
classScore that keeps track of a teacher's grades (integer). There can be as many as 7 classes, 35 students in a class, and 50 assignments in a term. ____________________45. Given the declaration
char letter[3][3] =
{
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'}
};
which character is stored in
letter[1][2]? ____________________46. Given the declaration
float table[30][20];
write a statement to store the value 16.5 into the twentieth row and fourth column of the array: ____________________
47. If a program contains the declarations
enum GarmentType {SOCKS, SLACKS, SHIRT, HAT, SWEATER};
enum StatusType {CLEAN, DIRTY, NEEDS_REPAIR};
int EddiesClothes[5][3];
// To be indexed by values of type GarmentType and StatusType
then ____________________ (row or column) processing would be needed to total the number of Eddie's garments that need repair.
48. If a program contains the declarations
enum Genders {MALE, FEMALE};
enum Instruments {TRUMPET, CLARINET, DRUM, VIOLIN, GUITAR};
int bandArray[2][5];
// To be indexed by values of type Genders and Instruments
then ____________________ (row or column) processing would be needed to total the number of females who are in the school band.
49. Given the declarations
typedef float List[23];
List payRate[30];
rewrite the declaration of
payRate without referring to type List: ____________________50. Given the declarations
typedef int List[100];
typedef List Table[5];
Table groupCost[8];
rewrite the declaration of
groupCost without referring to types List and Table: ____________________