7.1 (Fill in the Blanks) Answer each of the following:
Lists and tables of values can be stored in or .
An array
’s elements are related by the fact that they have the same and .
The number used to refer to a particular element of an array
is called its .
A(n) should be used to declare the size of an array
, because it eliminates magic numbers.
The process of placing the elements of an array
in order is called the array
.
The process of determining if an array
contains a particular key value is called the array
.
An array
that uses two subscripts is referred to as a(n) array
.
7.2 (True or False) State whether the following are true or false. If the answer is false, explain why.
A given array
can store many different types of values.
An array
subscript should normally be of data type float
.
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.
It’s an error if an initializer list has more initializers than there are elements in the array
.
7.3 (Write C++ Statements) Write one or more statements that perform the following tasks for an array
called fractions
:
Define a constant variable arraySize
to represent the size of an array
and initialize it to 10
.
Declare an array
with arraySize
elements of type double
, and initialize the elements to 0
.
Name the fourth element of the array
.
Refer to array
element 4.
Assign the value 1.667
to array
element 9.
Assign the value 3.333
to the seventh element of the array
.
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.
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.
Display all the array
elements separated by spaces using a range-based for
statement.
7.4 (Two-Dimensional array
Questions) Answer the following questions regarding a two-dimensional array
called table
:
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.
How many elements does the array
contain?
Use a counter-controlled for
statement to initialize each element of the array
to the sum of its subscripts.
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.
7.5 (Find the Error) Find and correct the error in each of the following program segments:
#include <iostream>;
arraySize = 10; // arraySize was declared const
Assume that array<int, 10> b{};
for (size_t i{0}; i <= b.size(); ++i) {
b[i] = 1;
}
Assume that a
is a two-dimensional array
of int
values with two rows and two columns:
a[1, 1] = 5;