Answers to Self-Review Exercises

  1. 7.1

    1. arrays, vectors.

    2. array name, type.

    3. subscript or index.

    4. constant variable.

    5. sorting.

    6. searching.

    7. two-dimensional.

  2. 7.2

    1. False. An array can store only values of the same type.

    2. False. An array subscript should be an integer or an integer expression.

    3. False. The remaining elements are initialized to zero.

    4. True.

  3. 7.3

    1. const size_t arraySize{10};

    2. array<double, arraySize> fractions{0.0};

    3. fractions[3]

    4. fractions[4]

    5. fractions[9] = 1.667;

    6. fractions[6] = 3.333;

    7.  

      
      cout << fixed << setprecision(2);
      cout << fractions[6] << ' ' << fractions[9] << endl;
      Output: 3.33 1.67
      
    8.  

      
      for (size_t i{0}; i < fractions.size(); ++i) {
         cout << "fractions[" << i << "] = " << fractions[i] << endl;
      }
      Output:
      fractions[0] = 0.0
      fractions[1] = 0.0
      fractions[2] = 0.0
      fractions[3] = 0.0
      fractions[4] = 0.0
      fractions[5] = 0.0
      fractions[6] = 3.333
      fractions[7] = 0.0
      fractions[8] = 0.0
      fractions[9] = 1.667
      
    9.  

      
      for (double element : fractions)
         cout << element << ' ';
      
  4. 7.4

    1. array<array<int, arraySize>, arraySize> table;

    2. Nine.

    3.  

      
      for (size_t row{0}; row < table.size(); ++row) {
         for (size_t column{0}; column < table[row].size(); ++column) {
            table[row][column] = row + column;
         }
      }
      
    4.  

      
      cout << "     [0]  [1]   [2]" << endl;
      for (size_t i{0}; i < arraySize; ++i) {
         cout << '[' << i << "] ";
         for (size_t j{0}; j < arraySize; ++j) {
            cout << setw(3) << table[i][j] << " ";
         }
         cout << endl;
      }
      Output:
         [0] [1] [2]
      [0] 1   2   3
      [1] 4   5   6
      [2] 7   8   9
      
  5. 7.5

    1. Error: Semicolon at end of #include preprocessing directive.

      Correction: Eliminate semicolon.

    2. Error: Assigning a value to a constant variable using an assignment statement.

      Correction: Initialize the constant variable in a const size_t arraySize declaration.

    3. Error: Referencing an array element outside the bounds of the array (b[10]).

      Correction: Change the loop-continuation condition to use < rather than <=.

    4. Error: array subscripting done incorrectly.

      Correction: Change the statement to 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