Chapter 11

ONE-DIMENSIONAL ARRAYS

QUESTIONS

True/False

1. The float type is an example of a structured data type.

2. A one-dimensional array is an example of a structured data type.

3. In C++, the component type of a one-dimensional array cannot be float, double, or long double.

4. In C++, the index type of a one-dimensional array can be any integral type.

5. An individual array component can be passed as a parameter to a function.

6. The components of an array are all of the same data type.

7. The size of an array is established at compile time rather than at execution time.

8. In C++, an array can be passed as a parameter either by value or by reference.

9. An array is an atomic data type.

10. Given the declaration

int beta[20];

the expression beta[3] accesses the third component of the array.

11. Given the declaration

int beta[20];

the statement

cout << beta;

cannot be used to output all 20 elements of the array.

12. Given the declaration

int beta[20];

the statement

beta = beta + 1;

adds 1 to all 20 elements of the array.

13. If a program has the declarations

enum WeatherType {SUNNY, CLOUDY, FOGGY, WINDY};

int frequency[4];

then the statement

cout << frequency[CLOUDY];

is syntactically valid.

14. C++ does not check for out-of-bounds array indices while a program is running.

15. The function heading

void SomeFunc( float x[] )

 

causes a compile-time error because the size of the array is missing.

16. If the word const precedes the declaration of an array in a function heading, the function is prevented from modifying the array.

17. The statement

if (score[studentID] >= 90)

letterGrade[studentID] = 'A';

is an example of the use of parallel arrays.

18. The statement

frequency['G']++;

is an example of the use of an array index with semantic content.

Multiple Choice

19. Which of the following statements about C++ arrays is true?

a. Array components cannot be of floating point types.

b. The index type of an array can be any data type.

c. An array component can be treated the same as a simple variable of its component type.

d. a and b above

e. a, b, and c above

20. Given the declaration

float alpha[75];

the valid range of index values for alpha is:

a. 0 through 75

b. 0 through 74

c. 1 through 75

d. 1 through 74

e. 1 through 76

21. What is the output of the following program fragment?

int gamma[3] = {5, 10, 15};

int i;

for (i = 0; i < 3; i++)

cout << gamma[i] << ' ';

a. 5 10 15

b. 5 10

c. 0 1 2

d. 0 1

e. It cannot be answered from the information given.

22. What is the output of the following program fragment?

int gamma[3] = {5, 10, 15};

int i;

for (i = 0; i <= 3; i++)

cout << gamma[i] << ' ';

a. 5 10 15

b. 5 10

c. 0 1 2

d. 0 1

e. It cannot be answered from the information given.

23. Which of the following could be used to declare an array alpha and initialize its components to 10, 20, and 30?

a. int alpha[3] = {10, 20, 30};

b. int alpha[] = {10, 20, 30};

c. int alpha[3] = {10 20 30};

d. a and b above

e. a, b, and c above

24. Given the declarations

int status[10];

int i;

which of the following loops correctly zeros out the status array?

a. for (i = 0; i <= 10; i++)

status[i] = 0;

b. for (i = 0; i < 10; i++)

status[i] = 0;

c. for (i = 1; i <= 10; i++)

status[i] = 0;

d. for (i = 1; i < 10; i++)

status[i] = 0;

e. for (i = 1; i <= 11; i++)

status[i] = 0;

25. After execution of the code fragment

int arr[5];

int i;

for (i = 0; i < 5; i++)

{

arr[i] = i + 2;

if (i >= 3)

arr[i-1] = arr[i] + 3;

}

what is contained in arr[1]?

a. 2

b. 3

c. 7

d. 8

e. none of the above

26. After execution of the code fragment

int arr[5];

int i;

for (i = 0; i < 5; i++)

{

arr[i] = i + 2;

if (i >= 3)

arr[i-1] = arr[i] + 3;

}

what is contained in arr[3]?

a. 5

b. 3

c. 8

d. 9

e. none of the above

27. What is the output of the following program fragment?

int alpha[5] = {100, 200, 300, 400, 500};

int i;

for (i = 4; i > 0; i--)

cout << alpha[i] << ' ';

a. 400 300 200 100

b. 500 400 300 200 100

c. 500 400 300 200

d. 4 3 2 1

e. It cannot be answered from the information given.

28. What is the output of the following program fragment?

int alpha[5] = {100, 200, 300, 400, 500};

int i;

for (i = 4; i >= 0; i--)

cout << alpha[i] << ' ';

a. 400 300 200 100 0

b. 500 400 300 200 100

c. 500 400 300 200

d. 4 3 2 1 0

e. It cannot be answered from the information given.

29. Given a 5000-element array beta, which of the code fragments below could be used to print out the values of beta[0], beta[2], beta[4], and so forth? (All variables are of type int.)

a. for (i = 0; i < 5000; i = i + 2)

cout << beta[i] << endl;

b. for (i = 0; i < 2500; i++)

cout << beta[2*i] << endl;

c. for (i = 0; i < 2500; i++)

cout << beta[i]*2 << endl;

d. a and b above

e. a, b, and c above

30. Which of the following cannot be used to input values into a 3-element int array named alpha?

a. cin >> alpha[0] >> alpha[1] >> alpha[2];

b. cin >> alpha;

c. for (i = 0; i < 3; i++)

cin >> alpha[i];

d. cin >> alpha[0];

cin >> alpha[1];

cin >> alpha[2];

31. Which of the following statements about passing C++ arrays as parameters is false?

a. It is impossible to pass an array by value.

b. When declaring an array as a formal parameter, you do not attach an ampersand (&) to the name of the component type.

c. When declaring an array as a formal parameter, you must include its size within square brackets.

d. At run time, the base address of the actual parameter is passed to the function.

32. Given the program fragment

char alpha[200];

char beta[200];

...

Copy(alpha, beta, 200); // Copy all components of beta into alpha

which of the following is the best function heading for the Copy function?

a. void Copy( /* out */ char arr1[],

/* in */ char arr2[],

/* in */ int length )

b. void Copy( /* out */ const char arr1[],

/* in */ char arr2[],

/* in */ int length )

c. void Copy( /* out */ char arr1[],

/* in */ const char arr2[],

/* in */ int length )

d. void Copy( /* out */ const char arr1[],

/* in */ const char arr2[],

/* in */ int length )

33. You are writing a program to count the frequencies of characters that are read from a data file. (The computer uses the ASCII character set, which defines 128 different characters.) Which of the following array declarations is appropriate, given that input characters will be used to index into the freqCount array?

a. int freqCount[128];

b. int freqCount[char];

c. char freqCount[128];

d. char freqCount[int];

e. none of the above

34. Given the declarations

const int NUM_STUDENTS = 300;

enum Colors {RED, BLACK, BROWN, BLOND, GRAY};

 

 

which set of declarations below creates two parallel arrays, one that holds integer ID numbers and one that holds student hair colors?

a. int studentID[NUM_STUDENTS];

int hairColor[Colors];

b. int studentID[NUM_STUDENTS];

int hairColor[NUM_STUDENTS];

c. int studentID[NUM_STUDENTS];

Colors hairColor[NUM_STUDENTS];

d. int studentID[Colors];

int hairColor[Colors];

Fill-In

35. A(n) ____________________ data type is a collection of components whose organization is characterized by the method used to access individual components.

36. An individual component of an array is accessed by using a(n) ____________________ in square brackets to specify the component's position within the array.

37. A(n) ____________________ is a structured collection of components, all of the same data type, that are accessed by relative position within the collection.

38. Array components often are referred to as array ____________________.

39. A(n) ____________________ array index is an index value that, in C++, is either less than zero or greater than the array size minus one.

40. The ____________________ of an array is the memory address of the first element of the array.

41. The ____________________ of an array (the actual number of values in the array) may be less than the array's declared size.

42. ____________________ processing refers to working with only that portion of an array that contains meaningful data values.

43. ____________________ arrays are a structure in which several arrays are accessed by the same index.

44. An array index is said to have ____________________ if it has meaning beyond simple position.

45. Write the declaration statement for a one-dimensional array named alpha whose index values range from 0 through 99 and whose component type is float: ____________________

 

46. Write the declaration statement for a 25-element one-dimensional array named letterGrade whose component type is char: ____________________

47. Given the enumeration type

enum FlagColors {RED, WHITE, BLUE};

write the declaration statement for a one-dimensional array named flagArray whose index values range from 0 through 50 and whose component type is FlagColors: ____________________

48. Given the enumeration type

enum Grades {A, B, C, D, F, AU, W};

write the declaration statement for a one-dimensional array named gradeCount whose index values are of type Grades and whose component type is int: ____________________

49. Given the declaration

char charArray[15];

write an assignment statement that stores the value 'X' into the sixth component of the array: ____________________

50. Given the declarations

enum Colors {RED, ORANGE, YELLOW, GREEN, BLUE};

float waveLength[5]; // To be indexed by values of type Colors

write a statement to print the third component of the waveLength array: ____________________