array
s, vector
s.
array
name, type.
subscript or index.
constant variable.
sorting.
searching.
two-dimensional.
False. An array
can store only values of the same type.
False. An array
subscript should be an integer or an integer expression.
False. The remaining elements are initialized to zero.
True.
const size_t arraySize{10};
array<double, arraySize> fractions{0.0};
fractions[3]
fractions[4]
fractions[9] = 1.667;
fractions[6] = 3.333;
cout << fixed << setprecision(2);
cout << fractions[6] << ' ' << fractions[9] << endl;
Output: 3.33 1.67
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
for (double element : fractions)
cout << element << ' ';
array<array<int, arraySize>, arraySize> table;
Nine.
for (size_t row{0}; row < table.size(); ++row) {
for (size_t column{0}; column < table[row].size(); ++column) {
table[row][column] = row + column;
}
}
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
Error: Semicolon at end of #include
preprocessing directive.
Correction: Eliminate semicolon.
Error: Assigning a value to a constant variable using an assignment statement.
Correction: Initialize the constant variable in a const
size_t
arraySize
declaration.
Error: Referencing an array
element outside the bounds of the array
(b[10]
).
Correction: Change the loop-continuation condition to use <
rather than <=
.
Error: array
subscripting done incorrectly.
Correction: Change the statement to a[1][1] = 5;