Next: Initializations of arrays
Up: Arrays in C++
Previous: Declaration of Arrays
Given the declaration above of a 100 element array the compiler
reserves space for 100 consecutive floating point values and accesses
these values using an index/subscript that takes values from 0 to 99.
The first element in an array in C++ always has the
index 0, and if the array has n
elements the last
element will have the index n-1
.
An array element is accessed by writing the identifier of the array followed by the subscript in square brackets. Thus to set the 15th element of the array above to 1.5 the following assignment is used:
annual_temp[14] = 1.5;Note that since the first element is at index 0, then the
i
th
element is at index i-1
. Hence in the above the 15th element
has index 14.
An array element can be used anywhere an identifier may be used. Here are some examples assuming the following declarations:
const int NE = 100, N = 50; int i, j, count[N]; float annual_temp[NE]; float sum, av1, av2;A value can be read into an array element directly, using
cin
cin >> count[i];
The element can be increased by 5,
count[i] = count[i] + 5;
or, using the shorthand form of the assignment
count[i] += 5;
Array elements can form part of the condition for an if
statement, or indeed, for any other logical expression:
if (annual_temp[j] < 10.0) cout << "It was cold this year " << endl;
for
statements are the usual means of accessing every element
in an array. Here, the first NE
elements of the array
annual_temp
are given values from the input stream
cin
.for (i = 0; i < NE; i++) cin >> annual_temp[i];
The following code finds the average temperature recorded in the first ten elements of the array.
sum = 0.0; for (i = 0; i <10; i++) sum += annual_temp[i]; av1 = sum / 10;Notice that it is good practice to use named constants, rather than literal numbers such as 10. If the program is changed to take the average of the first 20 entries, then it all too easy to forget to change a 10 to 20. If a
const
is used consistently, then
changing its value will be all that is necessary.
For example, the following example finds the average of the
last k
entries in the array. k
could either be a
variable, or a declared constant. Observe that a change in the value
of k
will still calculate the correct average (provided
k<=NE
).
sum = 0.0; for (i = NE - k; i < NE; i++) sum += annual_temp[i]; av2 = sum / k;Important - C++ does not check that the subscript that is used to reference an array element actually lies in the subscript range of the array. Thus C++ will allow the assignment of a value to
annual_temp[200]
, however the effect of this assignment is
unpredictable. For example it could lead to the program attempting to
assign a value to a memory element that is outside the program's
allocated memory space. This would lead to the program being
terminated by the operating system. Alternatively it might actually
access a memory location that is within the allocated memory space of
the program and assign a value to that location, changing the
value of the variable in your program which is actually associated
with that memory location, or overwriting the machine code of your
program. Similarly reading a value from
annual_temp[200]
might access a value that has not been set by
the program or might be the value of another variable. It is the
programmer's responsibility to ensure that if an array is declared
with n
elements then no attempt is made to reference any
element with a subscript outside the range 0 to n-1
. Using an
index, or subscript, that is out of range is called
Subscript Overflow.
Subscript overflow is one of the commonest causes of erroneous results and
can frequently cause very strange and hard to spot errors in programs.