Problem list 7
1. Define the following arrays:
(a) empNums, a 100-element array of ints
(b) payRates, a 25-element array of floats
(c) cityName, a 26-element array of string objects
(d) lightYears, a 1,000-element array of doubles
2. What’s wrong with the following array definitions?
int r e a d i n g s [ − 1 ] ;
f l o a t measurements [ 4 . 5 ] ;
int s i z e ;
s t r i n g names [ s i z e ] ;
3. What would the valid subscript values be in a 4-element array of doubles?
4. What is the difference between an array’s size declarator and a subscript?
5. What is the output of the following code?
int v a l u e s [ 5 ] , count ;
for ( count = 0 ; count < 5 ; count++)
v a l u e s [ count ] = count + 1 ;
for ( count = 0 ; count < 5 ; count++)
cout << v a l u e s [ count ] << e n d l ;
6. The following program skeleton contains a 20-element array of ints called fish. When
completed, the program should ask how many fish were caught by fishermen 1
through 20, and store this data in the array. Complete the program.
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
1
const int NUM_FISH = 2 0 ;
int f i s h [NUM_FISH ] ;
// You must f i n i s h t h i s program . I t s h o u l d a s k how
// many f i s h were c a u g h t by f i s h e r m e n 1−20, and
// s t o r e t h i s d a t a i n t h e a r r a y f i s h .
return 0 ;
}
7. Define the following arrays:
(a) ages, a 10-element array of ints initialized with the values 5, 7, 9, 14, 15, 17,
18, 19, 21, and 23.
(b) temps, a 7-element array of floats initialized with the values 14.7, 16.3, 18.43,
21.09, 17.9, 18.76, and 26.7.
(c) alpha, an 8-element array of chars initialized with the values ‘J’, ‘B’, ‘L’, ‘A’,
‘*’, ‘$’, ‘H’, and ‘M’.
8. Is each of the following a valid or invalid array definition? (If a definition is invalid,
explain why.)
(a) int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1};
(b) int matrix[5] = {1, 2, 3, 4, 5, 6, 7};
(c) double radii[10] = {3.2, 4.7};
(d) int table[7] = {2, , , 27, , 45, 39};
(e) char codes[] = {’A’, ’X’, ’1’, ’2’, ’s’};
(f) int blanks[];
9. Given the following array definition:
int values[] = {2, 6, 10, 14};
What does each of the following display?
(a) cout << values[2];
(b) cout << ++values[0];
(c) cout << values[1]++;
(d) x = 2;
cout << values[++x];
10. Given the following array definition:
2
int nums [ 5 ] = { 1 , 2 , 3 } ;
What will the following statement display?
c o u t << nums [ 3 ] ;
11. Define a two-dimensional array of ints named grades. It should have 30 rows and
10 columns.
12. How many elements are in the following array?
double s a l e s [ 6 ] [ 4 ] ;
13. Write a statement that assigns the value 56893.12 to the first column of the first
row of the array defined in Question 2.
14. Write a statement that displays the contents of the last column of the last row of
the array defined in Question 2.
15. Define a two-dimensional array named settings large enough to hold the table of
data below. Initialize the array with the values in the table.
12 24 32 21 42
14 67 87 65 90
19 1 24 12 8
16. Look at the following array definition:
int numbers [ 5 ] = { 1 , 2 , 3 } ;
What value is stored in numbers[2]?
What value is stored in numbers[4]?
17. Assuming that numbers is an array of doubles, will the following statement display
the contents of the array?
c o u t << numbers << e n d l ;
18. Look at the following array definition:
double s a l e s [ 8 ] [ 1 0 ] ;
How many rows does the array have?
How many columns does the array have?
How many elements does the array have?
Write a statement that stores a number in the last column of the last row in the
array.
3
19. Define a floating point array myvalues with 20 elements. Write a for loop that
assigns the first 20 odd numbers to the array elements.
20. Define a two-dimensional array of integers named Matrix, where both the number
of rows and the number of columns are equal to 50.
21. Look at the following array definition:
int a r r a y [ 1 0 ] [ 5 ] ;
Write a statement that assigns 100 to the last column of the first row of this array.
Write a for loop that assigns 10 to all the elements of the first column.
22. Write a program that lets the user enter the total rainfall for each of 12 months into
an array of doubles. The program should calculate and display the total rainfall for
the year, the average monthly rainfall, and the months with the highest and lowest
amounts.
Input Validation: Do not accept negative numbers for monthly rainfall figures.