Professional Documents
Culture Documents
For example, although the variables key, count, and grade declared in the statements
char key;
int count;
double grade;
are of different data types, each variable can store only one value of the declared data type.
These types of variables are called atomic variables (also referred to as scalar variables),
which means their
values can’t be further subdivided or separated into a legitimate data type.
Often one need to store a set of values, all the same data type, that form a logical group. For
example, the following lists show three groups of items: 1) a list of five double-precision
temperatures, 2) a list of four character codes, and 3) a list of six integer voltages:
A simple list containing items of the same data type is called a one-dimensional array.
All the temperatures in the list are double-precision numbers and must be declared as
such, the items in the list can be declared as a single unit and stored under a common variable
name called the array name. For example, temp is used as the name for this list, and the
declaration statement
double temp[5]; specifies that temp is to store five double-precision values. Notice that
this declaration statement gives the array (or list) name, the data type of items in the array,
and the number of items in the array. It’s a specific example of the general syntax of an array
declaration statement:
dataType arrayName[number-of-items]
The following are other examples of array declarations using this two-line syntax:
const int NUMELS = 6;
int volts[NUMELS];
Each item in an array is called an element or a component of the array. The elements in
the arrays shown in Figure 7.1 are stored sequentially, with the first element stored in the
first reserved location, the second element stored in the second reserved location, and so on
until the last element is stored in the last reserved location.
Because elements in the array are stored sequentially, any single element can be accessed
by giving the array’s name and the element’s position. This position is called the element’s
index or subscript value.
For example, the declaration double temp[5]; creates five elements, with the
following correspondences:
Finally, 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, 92, 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};
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.
Although the commas in the initialization braces are always required, the inner braces can
be omitted. Without them, the initialization for val can be written as follows:
int val[3][4] = {8,16,9,52,
3,15,27,6,
14,25,2,10};
Nested loops are especially useful when dealing with two-dimensional arrays because they
allow the programmer to designate and cycle through each element easily. In the example
given the variable i controls the outer loop, and the variable j controls the inner loop. Each
pass through the outer loop corresponds to a single row, with the inner loop supplying the
column elements.
Assignment
Write a program that specifies three one-dimensional arrays named current, resistance, and
volts. Each array should be capable of holding 10 elements.
Using a for loop, input values for the current and resistance arrays. The entries in
the 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
column heading:
Current Resistance Volts