Chapter 7
393 Declaring and Processing Two Dimensional Arrays
EXERCISES 7.3
1. (Practice) Write specification statements for the following:
a. An array of integers with 6 rows and 10 columns b. An array of
integers with 2 rows and 5 columns c. An array of characters with 7
rows and 12 columns d. An array of characters with 15 rows and 7 columns e.
An array of double-precision numbers with 10 rows and 25
columns
f. An array of double-precision numbers with 16 rows and 8 columns
2. (Desk Check) Determine the output produced by the following program:
#include <iostream> using namespace std;
int main()
int i, j, val[3] [4] = {8,16,9,52,3,15,27,6,14,25,2,10};
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
cout << " " «< val[i][j];
return 0;
3. (Practice) a. Write a C++ program that adds the values of all
elements in the val array
used in Exercise 2 and displays the total.
b. Modify the program written for Exercise 3a to display the total of each row
separately.
4. (Practice) Write a C++ program that adds equivalent elements of the
two-dimensional
arrays named first and second. Both arrays should have two rows and three
columns. For example, element [1] [2] of the resulting array should be the sum of
first[1] [2] and second[1] [2]. The first and second arrays should be initialized
as f ollows:
16 54
first
18 91
23 11
second 24 52 16 19
77 59
5. (Data Processing) a. Write a C++ program that finds and displays the
maximum value
in a two-dimensional array of integers. The array should be declared as a 4-by-5
array of integers and initialized with the data 16, 22, 99, 4, 18, -258, 4, 101, 5, 98,
105, 6, 15, 2, 45, 33, 88, 72, 16, and 3. b. Modify the program written in
Exercise 5a so that it also displays the maximum val
ue's row and column subscript numbers.
394
Arrays
ata Processing) Write a C++ program that selects the values in a 4-by-5 array of
6. (D
posi
tive integers in increasing order and stores the selected values in the
one-dimensional array named sort. Use the data statement in Exercise 5a to
initialize the two dimensional array.
7. (Electrical Eng.) a. An engineer has constructed a two-dimensional array
of real num
bers with three rows and five columns. This array currently contains test voltages of an
amplifier. Write a C++ program that interactively inputs 15 array values, and then deter
mines the total number of voltages in these ranges: less than 60, greater than or equal
to 60 and less than 70, greater than or equal to 70 and less than 80, greater than
or equal to 80 and less than 90, and greater than or equal to 90. b
. Entering 15
voltages each time the program written for Exercise 7a runs is cumbersome.
What method could be used for initializing the array during the testing phase? c. How
might the program you wrote for Exercise 7a be modified to include the case of
no voltage being present? That is, what voltage could be used to indicate an invalid voltage,
and how would your program have to be modified to exclude counting such a voltage?
7.4 Arrays as Arguments
Array elements are passed to a called function in the same manner as scalar
variables; they are simply included as subscripted variables when the function call is
made. For example, the following function call passes the values of the elements
volts [2] and volts [6] to the f unction findMin():
findMin(volts[2], volts [6]);
Passing a complete array of values to a function is, in many respects, easier than
passing e ach element. The called function receives access to the actual array
rather than a copy of values in the array. For example, if volts is an array, the
function call findMax (volts); makes the complete volts array available to the findMax
() function. This function call is different from passing a single variable to a function.
Recall that when a single scalar argument is passed to a function (see Section 6.1), the
called function receives only a copy of the passed value, which is stored in one of the
function's parameters. If arrays were passed in this manner, a copy of the
complete array would have to be created. For large arrays, making copies for each
function call would waste computer storage and frustrate the effort to return
multiple-element changes made by the called program. (Remember that a function
returns at most one direct value.)
To avoid these problems, the called function is given direct access to the original array,3 In
this way, any changes the called function makes are made directly to the array. For
the following specific examples of function calls, the arrays nums, keys, volts, and
current are declared as shown:
int nums [5]; char keys [256]; double volts (500), current[500);
// a n array of five integers 1 / an array of 256 characters // t wo arrays of 500
doubles
3The called function has access to the original array because the array's starting address is actually passed
as an argument. The formal parameter receiving this address argument is a pointer. Chapter 12 explains the
intimate relationship between array names and pointers.