You are on page 1of 4

ARRAYS 13/11/2018

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:

Temperatures Codes Voltages


95.75 Z 98
83.0 C 87
97.625 K 92
72.5 L 79
86.25 A 85
79 B 80

A simple list containing items of the same data type is called a one-dimensional array.

One dimension Array

One-dimensional array, also referred to as a single-dimensional array, is a list of related


values,all having the same data type, that’s stored with a single group name. In C++, as in
other computer languages, the group name is referred to as the array name. For example,
consider this list of temperatures:
95.75
83.0
97.625
72.5
86.25

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]

Good programming practice requires defining number-of-items in the array as a


constant before declaring the array. So in practice, the previous array declaration for temp
would be declared with two statements, as in these examples:

const int NUMELS = 5; // define a constant for the number of items


double temp[NUMELS]; // declare the array

The following are other examples of array declarations using this two-line syntax:
const int NUMELS = 6;
int volts[NUMELS];

const int ARRAYSIZE = 4;


char code[ARRAYSIZE];

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:

temp[0] refers to the first temperature stored in the temp array


temp[1] refers to the second temperature stored in the temp array
temp[2] refers to the third temperature stored in the temp array
temp[3] refers to the fourth temperature stored in the temp array
temp[4] refers to the fifth temperature stored in the temp array

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.

Declaring and Processing Two-Dimensional Arrays


A two-dimensional array, sometimes referred to as a table, consists of both rows and
columns of elements. For example, the following array of numbers is called a two-
dimensional array of integers:
8 16 9 52
3 15 27 6
14 25 2 10
This array consists of three rows and four columns. To reserve storage for this array, both
the number of rows and the number of columns must be included in the array’s declaration.
Calling the array val, the following is the correct specification for this two-dimensional array:
int val[3][4]; Similarly, the declarations
double volts [10][5];
char code [6][26];
specify that the array volts consists of 10 rows and 5 columns of floating-point numbers,
and the array code consists of 6 rows and 26 columns, with each element capable of holding
one character.
To locate each element in a two-dimensional array, you use its position in the array. As
shown in Figure below the term val[1][3] uniquely identifies the element in row 1, column
3. As with one-dimensional array variables, two-dimensional array variables can be used
anywhere that scalar variables are valid, as shown in these examples using elements of the
val array:
watts = val[2][3];
val[0][0] = 62;
newnum = 4 * (val[1][0] - 5);
sumRow0 = val[0][0] + val[0][1] + val[0][2] + val[0][3];
As with one-dimensional arrays, two-dimensional arrays can be initialized in their
declaration statements by listing the initial values inside braces and separating them with
commas. Additionally, braces can be used to separate rows. For example, the declaration
int val[3][4] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };

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

You might also like