Lecture 13: Arrays |
scanf("%d", &n);
switch (n)
{
case 1: average
= a1;
break;
case 2: average
= (a1 + a2) / 2.0;
break;
case 3: average
= (a1 + a2 + a3) / 3.0;
break;
|
case 10: average
= (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) / 10.0;
}
For these purposes exist the arrays. An array lets us define a set
of variables of the same type with an easy way of access, namely with
an
index. Just like in mathematics, ai is the i-th
element of vector or series a, a[i] gives the i-th
element
of array a.
|
type name[numelement] ; |
name
is the identifier of the array, just like a name for other variables.
numelement
defines the number of elements in the array. The elements run from 0 to
numelement-1. This is a little confusing for beginning programmers, who
are used to indices from 1 to n.
type
is any variable type, for instance float or int.
Examples:
float account[100];
This might be used to store the information of 100 bankaccounts.
long int prime[10];
This might be used to store the first 10 prime numbers..
int propinas[2000];
This might be used to store some simple information of the status of
the students with numbers between 0 and 1999, for instance if they paid
their tuition fees or not.
Use of an array
name[index] will return the value element number index of the array name. This is then a value of the type as described in the declaration of the array. Examples of the arrays declared in the previous section: account[20]
prime[8]
propinas[1055]
We can also use a variable for the index in addressing a single element of an array. Naturally, this variable needs to be of any integer type, because the index is something countable; index 3.4981 does not make sense. Index 3 does, it will address the third element of the array. The following code will show the entire array of 20 accounts: for (i=0; i<20; i++)
|
type name[numindex1][numindex2]; |
The use of a double array is similar to that of a single array. We
separate
the indices with a comma, or by putting them in separate square
parenthesis:
name[index1][index2] |
As an example: to write the matrix of the figure on the left
we might
do the following in a complete program. Note that the array consists of
9 (3x3) elements of type integer.
void main()
matrix[0][0] = 1;
|
Caution |
When we are using an array we also have to be careful not to
use an
index that is 'out of bounds'. This means that we always have to use an
index that is less than the total number of elements in the array. If
we
use a too large index, the results of our program can be very odd. This
is best illustrated in an example. The following program defines an
array
r
of 4 integers running from r[0] to r[3], and a
normal
integer a. The figure on the left shows how they might be
placed
in memory. What will happen when our program assigns a value to r[4]?
If r[4] had existed, it would have occupied the place that is
now taken by a, and an assignment to r[4] would be
putting
a value in the box of what is now occupied by a. Most
computer
languages don't care and put the value for r[4] there anyway,
thereby overwriting the value of a.
main()
a = 0;
The output of the program will probably be a=0
Some programming languages can check for this at run-rime. This is called range-checking and when the program tries to use a wrong index, a message 'range-check error' or 'array index out of bounds' will be displayed. The disadvantage of doing this is that the program becomes slower and the compiled program will occupy more space in memory and on disk. |
Note: it depends on the exact implementation of the
language/compiler.
With some languages, it will overwrite the variable declared before
the array as in the example above, while in other languages it will
overwrite
the variable after the array. You can find out by declaring the
variables
int a;
int r[4];
int b;
and see if r[4] overwrites a
or b.