Self-Review Exercises

  1. 7.1 (Fill in the Blanks) Answer each of the following:

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

    2. An array’s elements 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 eliminates magic numbers.

    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.

  2. 7.2 (True or False) State whether the following are true or false. If the answer is false, explain why.

    1. A given 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’s an error if an initializer list has more initializers than there are elements in the array.

  3. 7.3 (Write C++ Statements) Write one or more statements that perform the following tasks for an array called fractions:

    1. Define a constant variable arraySize to represent the size of an array and initialize it 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. Refer to array element 4.

    5. Assign the value 1.667 to array element 9.

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

    7. Display array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that’s actually displayed on the screen.

    8. Display all the array elements using a counter-controlled for statement. Define the integer variable i as a control variable for the loop. Show the output.

    9. Display all the array elements separated by spaces using a range-based for statement.

  4. 7.4 (Two-Dimensional array Questions) Answer the following questions regarding a two-dimensional array called table:

    1. Declare the array to store int values 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 counter-controlled for statement to initialize each element of the array to the sum of its subscripts.

    4. Write a nested for statement that displays the values of each element of array table in tabular format with 3 rows and 3 columns. Each row and column should be labeled with the row or column number. Assume that the array was initialized with an initializer list containing the values from 1 through 9 in order. Show the output.

  5. 7.5 (Find the Error) Find and correct the error in each of the following program segments:

    1. #include <iostream>;

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

    3. Assume that array<int, 10> b{};

      
      for (size_t i{0}; i <= b.size(); ++i) {
          b[i] = 1;
      }
      
    4. Assume that a is a two-dimensional array of int values with two rows and two columns:

      
      a[1, 1] = 5;
      
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset