Chapter 7
383
One-Dimensional Arrays
c. for (j = 3; j <= 10; j++)
cout << b[j] << " "; d. for (k = 3; k <= 12; k = k +
3)
cout << b[k] << " "; e. for (i = 2; i < 11; i = i + 2)
cout << c[i] << " ";
6. (Practice) a. Write a program to input the following values in
an array named volts:
11.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59. After the
data has been entered, have your program display the values. b. Repeat
Exercise 6a, but after the data has been entered, have your
program display it
in the following form:
11.95
8.22 13.54
16.32 15.98 6.45
9
12.15 26.22 17.59
7. (Practice) Write a program to input eight integer numbers in an array
named temp. As
each number is input, add the numbers to a total. After all numbers are
input, display the numbers and their average.
8. (Data Processing) a. Write a program to input 10 integer
numbers in an array named
fmax and determine the maximum value entered. Your program should contain
only one loop, and the maximum should be determined as array element values
et the maximum equal to the first array element,
are being input. (Hint: S
which should be input before the loop used to input the remaining
array values.) b. Repeat Exercise 8a, keeping track of both the maximum
element in the array and the
index number for the maximum. After displaying the numbers,
print these two mes sages (replacing the underlines with the
correct values):
The maximum value is:
This is element number in the list of numbers c. Repeat Exercise
8b, but have your program locate the minimum of the data entered.
9. (Data Processing) a. Write a program to input the following integer
numbers in an
array named grades: 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82,
and 73. As each number is input, add the numbers to a total. After all
numbers are input and the total is obtained, calculate the average of the
numbers, and use the average to determine the deviation of each value from the
average. Store each deviation in an array named deviation. Each
deviation is obtained as the element value less the average of all the
data. Have your program display each deviation with its corresponding
element from the grades array. b. Calculate the variance of the data
used in Exercise 9a. The variance is obtained by
squaring each deviation and dividing the sum of the squared deviations by
the num ber of deviations.
10. (Electrical Eng.) Write a program that specifies three
one-dimensional arrays named cur
rent, resistance, and volts. Each array should be capable of holding 10
elements.
384
Arrays
Using a for loop, input values for the current and resistance arrays. The entries
in t he volts array should be the product of the corresponding values in the
current and resistance arrays (so volts[i] = current [i] *
resistance[i]). After all the data has been entered, display the following
output, with the appropriate value under each c olumn heading:
Current
Resistance
Volts
7.2
Array Initialization
Array elements can be initialized in their declaration statements in the same
manner as scalar variables, except the initializing elements must be included in
braces, as shown in these examples:
int temp [ 5] = { 98, 87 ,
9
2, 79, 85}; char codes [6] = {'s', 'a', 'm',
'p', 'l', ' e'); double slopes [7] = {11.96, 6.43, 2.58, .86, 5.89,
7.56, 8.22};
Initializers are applied in the order they are written, with the first value used to
initialize element 0, the second value used to initialize element 1, and so on, until
all values have been used. For example, in the declaration
int temp [5] = {98, 87, 92, 79, 85};
temp [0] is initialized to 98, temp [1] is initialized to 87, temp [2] is initialized to
92, temp [3] is initialized to 79, and temp [4] is initialized to 85.
Because white space is ignored in C++, initializations can be continued across
multiple lines. For example, the following declaration uses four lines to initialize
all the array elements:
int gallons [20] = {19, 16, 1
4, 19, 20, 18,
12, 10, 22, 15, 18, 17, 16 , 14, 23, 19, 15, 18, 21, 5};
NNH
1/ i nitializing values 1/ can extend across 1/ m
ultiple lines
If the number of initializers is less than the declared number of elements
listed in square brackets, the initializers are applied starting with array element
0. Therefore, in the declaration
double length [7] = {7.8, 6.4, 4.9, 11.2};
only length[0], length[1], length[2], and length[3] are initialized with the
listed values. The other array elements are initialized to 0.
Unfortunately, there's no method of indicating repetition of an initialization value
or of initializing later array elements without first specifying values for earlier
elements.
A unique feature of initializers is that the array size can be omitted when
initializing values are included in the declaration statement. For example,
the following declaration reserves enough storage room for five elements:
int gallons[] = {16, 12, 10, 14, 11};