www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 388 (continued)]

Self-Review Exercises

7.1

Answer each of the following:

  1. Lists and tables of values can be stored in __________ or __________.

  2. The elements of an array are related by the fact that they have the same ________ and ___________.

  3. The number used to refer to a particular element of an array is called its ________.

  4. A(n) __________ should be used to declare the size of an array, because it makes the program more scalable.

  5. The process of placing the elements of an array in order is called ________ the array.

  6. The process of determining if an array contains a particular key value is called _________ the array.

  7. An array that uses two subscripts is referred to as a(n) _________ array.

7.2

State whether the following are true or false. If the answer is false, explain why.

  1. An array can store many different types of values.

  2. An array subscript should normally be of data type float.

  3. If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are initialized to the last value in the initializer list.

  4. It is an error if an initializer list contains more initializers than there are elements in the array.

  5. An individual array element that is passed to a function and modified in that function will contain the modified value when the called function completes execution.

7.3

Write one or more statements that perform the following tasks for and array called fractions:

  1. Define a constant variable arraySize initialized to 10.

  2. Declare an array with arraySize elements of type double, and initialize the elements to 0.

  3. Name the fourth element of the array.


  4. [Page 389]
  5. Refer to array element 4.

  6. Assign the value 1.667 to array element 9.

  7. Assign the value 3.333 to the seventh element of the array.

  8. Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that is actually displayed on the screen.

  9. Print all the array elements using a for statement. Define the integer variable i as a control variable for the loop. Show the output.

7.4

Answer the following questions regarding an array called table:

  1. Declare the array to be an integer array and to have 3 rows and 3 columns. Assume that the constant variable arraySize has been defined to be 3.

  2. How many elements does the array contain?

  3. Use a for repetition statement to initialize each element of the array to the sum of its subscripts. Assume that the integer variables i and j are declared as control variables.

  4. Write a program segment to print the values of each element of array table in tabular format with 3 rows and 3 columns. Assume that the array was initialized with the declaration

    int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } };
    

    and the integer variables i and j are declared as control variables. Show the output.

7.5

Find the error in each of the following program segments and correct the error:

  1. #include <iostream>;

  2. arraySize = 10; // arraySize was declared const

  3. Assume that int b[ 10 ] = { 0 };

    for ( int i = 0; <= 10; i++ )
        b[ i ] = 1;
    
  4. Assume that int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

    a[ 1, 1 ] = 5;
    


Previous Page
Next Page