Unit 3
Unit 3
ARRAYS IN C++
3.0 INTRODUCTION
Society today generates massive amounts of data, including records of digital transactions, social
media posts, and information from sensors and devices spread everywhere. In order to handle that
large amount of data, we need a way to store it. And we need to be able to store not just a few
variables, but dozens, or hundreds, or thousands. Fortunately, there's a solution—in fact, more than
one solution—for handling as much data as you have. Two methods for storing
data are arrays, which were part of C, and vectors, which were an improvement of C++.
An array is a sequence of objects all of which have the same type. The objects are called the
elements of the array and are numbered consecutively 0, 1, 2, 3, ... . These numbers are called
index values or subscripts of the array. The term “subscript” is used because as a mathematical
sequence, an array would be written with subscripts: a0, a1, a2, …. The subscripts locate the
element’s position within the array, thereby giving direct access into the array.
If the name of the array is a, then a[0] is the name of the element that is in position 0, a[1]
is the name of the element that is in position 1, etc. In general, the ith element is in position i–1.
So if the array has n elements, their names are a[0], a[1], a[2], …, a[n-1].
Generally, the whole array is referred to as the variable. In other words, even though the array is
made up of many different "boxes", which you could think of as many different individual
variables, you will generally be able to refer to the entire array—the entire collection of boxes—
as one thing, so it makes sense to call it a variable. The individual boxes are called the elements,
2|Page
which are the component variables of the overall array variable. The number identifying an
element is called the index. To refer to an element of the array, the common phrase used is sub
(which can be thought of as short for subscript) and then the index number.
3|Page
equivalent to:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
You can see that the data is in a table format. The table has 30 entries, and every entry is an
integer. Because the table entries are all of the same type, you can declare a one-dimensional
array of 30 components of type int. The first five components of the one-dimensional array can
store the data of the first row of the table, the next five components of the one-dimensional array
can store the data of the second row of the table, and so on. In other
words, you can simulate the data given in a table format in a one-dimensional array.
If you do so, the algorithms to manipulate the data in the one-dimensional array will be somewhat
complicated, because you must know where one row ends and another begins.
You must also correctly compute the index of a particular element. C++ simplifies the
processing of manipulating data in a table form with the use of two-dimensional arrays.
This section first discusses how to declare two-dimensional arrays and then looks at ways
to manipulate data in a two-dimensional array.
Two-dimensional array: A collection of a fixed number of components arranged in rows
and columns (that is, in two dimensions), wherein all components are of the same type.
The syntax for declaring a two-dimensional array is:
4|Page
wherein intExp1 and intExp2 are constant expressions yielding positive integer values.
The two expressions, intExp1 and intExp2, specify the number of rows and the
number of columns, respectively, in the array.
The statement:
double sales[10][5];
wherein indexExp1 and indexExp2 are expressions yielding nonnegative integer values.
indexExp1 specifies the row position; indexExp2 specifies the column position.
The statement:
sales[5][3] = 25.75;
stores 25.75 into row number 5 and column number 3 (that is, the sixth row and the fourth column)
of the array sales.
5|Page
Suppose that:
int i = 5;
int j = 3;
Then, the previous statement:
sales[5][3] = 25.75;
is equivalent to:
sales[i][j] = 25.75;
So the indices can also be variables.
This statement declares board to be a two-dimensional array of four rows and three columns. The
components of the first row are 2, 3, and 1; the components of the second row are 15, 25, and 13;
the components of the third row are 20, 4, and 7; and the components of the fourth row are 11,
18, and 14, respectively.
6|Page
are initialized to 0. In this case, at least one of the values must be given to initialize all the
components of a row.
7|Page
where intExp1, intExp2, . . . , and intExpn are constant expressions yielding
positive integer values.
For example, the statement:
declares carDealers to be a three-dimensional array. The size of the first dimension is 10, the size
of the second dimension is 5, and the size of the third
dimension is 7. The first-dimension ranges from 0 to 9, the second dimension
ranges from 0 to 4, and the third-dimension ranges from 0 to 6. The base address
of the array carDealers is the address of the first array component that is, the
address of carDealers [0][0][0]. The total number of components in the array
carDealers is 10 * 5 * 7 = 350
The statement:
SELF ASSESMENT
1.Consider the following declaration: double salary[10];
In this declaration, identify the following:
a. The array name.
b. The array size.
c. The data type of each array component.
d. The range of values for the index of the array.
2. What would be a valid range for the index of an array of size 50?
8|Page
3. Write C++ statements to do the following:
a. Declare an array alpha of 15 components of type int.
b. Output the value of the tenth component of the array alpha.
c. Set the value of the fifth component of the array alpha to 35.
d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteenth
components of the array alpha.
e. Set the value of the fourth component of the array alpha to three times the value of the eighth
component minus 57.
f. Output alpha so that five components per line are printed.
4. Write C++ statements to define and initialize the following arrays.
a. Array heights of 10 components of type double. Initialize this array
to the following values: 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0.
b. Array weights of 7 components of type int. Initialize this array to the
following values: 120, 125, 137, 140, 150, 180, 210.
9|Page