LP4 - Unit 5 - C++ Arrays
LP4 - Unit 5 - C++ Arrays
UNIT 5
Arrays
5.1 Introduction
Suppose we wish to write a program that reads in five test scores and performs
some manipulations on these scores. For instance, the program might compute the
highest test score and then output the amount by which each score falls short of the
highest. The highest score is not known until all five scores are read in. Hence, all five
scores must be retained in storage so that after the highest score is computed each
score can be compared to it. To retain the five scores, we will need something
equivalent to five variables of type int. We could use five individual variables of type
int, but five variables are hard to keep track of, and we may later want to change our
program to handle 100 scores; certainly, 100 variables are impractical.
An array is the perfect solution. An array behaves like a list of variables with a
uniform naming mechanism that can be declared in a single line of simple code. For
example, the names for the five individual variables we need might be score[0],
score[1], score[2], score[3], and score[4].
The part that does not change, in this case score, is the name of the array. The
part that can change is the integer in the square brackets, [].
5.2 Arrays
An array is used to process a collection of data all of which is of the same type,
such as a list of temperatures or a list of names. This chapter introduces the basics of
defining and using arrays in C++ and presents many of the basic techniques used
when designing algorithms and programs that use arrays.
4 | Computer Programming 1
In C++, an array consisting of five variables of type int can be declared as follows:
int score[5];
This declaration is like declaring the following five variables to all be of type
int: score[0], score[1], score[2], score[3], score[4].
The individual variables that together make up the array are referred to in a
variety of different ways. We will call them indexed variables, though they are also
sometimes called subscripted variables or elements of the array. The number in
square brackets is called an index or a subscript. In C++, indexes are numbered
starting with 0, not starting with 1 or any other number except 0. The number of
indexed variables in an array is called the declared size of the array, or sometimes
simply the size of the array. When an array is declared, the size of the array is given
in square brackets after the array name. The indexed variables are then numbered
(also using square brackets), starting with 0 and ending with the integer that is one
less than the size of the array.
In our example, the indexed variables were of type int, but an array can have
indexed variables of any type. For example, to declare an array with indexed variables
of type double, simply use the type name double instead of int in the declaration of
the array. All the indexed variables for one array are, however, of the same
type. This type is called the base type of the array. Thus, in our example of the array
score, the base type is int.
You can declare arrays and regular variables together. For example, the following
declares the two int variables next and max in addition to the array score:
An indexed variable like score[3] can be used anyplace that an ordinary vari-
able of type int can be used.
Do not confuse the two ways to use the square brackets [] with an array name.
When used in a declaration, such as
int score[5];
the number enclosed in the square brackets specifies how many indexed variables the
array has. When used anywhere else, the number enclosed in the square brackets tells
which indexed variable is meant. For example, score[0] through score[4] are indexed
variables.
4 | Computer Programming 1
The index inside the square brackets need not be given as an integer constant. You
can use any expression in the square brackets as long as the expression evaluates
to one of the integers 0 through the integer that is one less than the size of the array.
For example, the following will set the value of score[3] equal to 99:
int n = 2;
score[n + 1] = 99;
Although they may look different, score[n + 1] and score[3] are the same
indexed variable in the above code. That is because n + 1 evaluates to 3. The identity
of an indexed variable, such as score[i], is determined by the value of its index, which
in this instance is i. Thus, you can write programs that say things such as “do such
and such to the i th indexed variable,” where the value of i is computed by the
program. For example, the program in Display 10.1 reads in scores and processes them
in the way we described at the start of this chapter
Output
Here, we have passed array parameters to the display() function in the same way we
pass variables to a function.
We can see this in the function definition, where the function parameters are
individual variables:
4 | Computer Programming 1
5.3 References
5.4 Acknowledgment
The syntax, table or images, and information contained in this module were
taken from the references cited above.