Lecture 17: Arrays
|Var name: array[startindex .. endindex] of type;|
is the identifier of the array, just like a name for other variables.
startindex and endindex define the limits of the indices of the array. Note that the start index is not necessarily 0, instead we can let the array start at any index we want. That is nice, because humans are more used to working with indices that start with 1 rather than 0.
type is any variable type, for instance real or integer, but it can even be another array as we will see later.
Var account: array[1..100] of
This might be used to store the information of 100 bankaccounts.
Var prime: array[1..10] of
This might be used to store the first 10 prime numbers..
This might be used to store some boolean information of the status of the students with numbers between 1000 and 2000, 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:
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 := 1 to 20 do
|Var name: array[startindex1 .. endindex1, startindex2 .. endindex2] of type;|
Technically speaking we can also do it in the way
Var name: array[startindex1..endindex1] of array[startindex2..endindex2] of type;
which nicely shows what we are dealing with, namely an array of arrays. This is how the computer organizes our array. The first form of declaring a double array is preferred though, because it is more logical from a human point of view.
The use of a double array is similar to that of a single array. We
the indices with a comma, or by putting them in separate square
|As an example: to write the matrix on the left we might do
in a complete program. Note that the array consists of 9 (3x3) elements
of type integer.
Var matrix: array[1..3, 1..3] of integer;
|When we are using an array we also have to be careful not to
index that is 'out of bounds'. This means that we always have to use an
index that is inbetween the lower index and the higher index limit. If
we use a too large or too low index, the results of our program canl be
very odd. This is best illustrated in an example. The following program
defines an array of 4 integers r[1..4] and a normal integer a.
The figure on the left shows how they might be placed in memory. What
happen when our program assigns a value to r? If r
had existed, it would have occupied the place that is now taken by a,
and an assignment to r would be putting a value in the box
of what is now occupied by a. Most computer languages don't
and put the value for r there anyway, thereby overwriting
the value of a.
Var r: array[1..4] of integer;
The output of the program will probably be
Some programming languages will check for this at run rime. With our Turbo Pascal compiler we can select the option to verify for such an index-out-of-bounds event at run time. This is called range-checking and when the program tries to use a wrong index, a 'range-check error' will be generated. The disadvantage of doing this is that the program becomes slower and the compiled program will occupy more space in memory and on disk.