Contents / Part 3 / Previous chapter / Next chapter / Index
Quite often we use the computer to store and manipulate sets of data rather than just a single value. For example, we might want to calculate wages for a group of people or sort a group of 20 numbers into order. The 20 numbers might well be associated with 20 names. Arrays make it a lot easier to deal with groups of names and numbers. To get to a more manageable example let's consider working with five names and their associated year of birth. We could store the five names in five variables like this:
N1$ = "SARDESON"
N2$ = "MATTINSON"
N3$ = "MOIR"
N4$ = "ALLEN"
N5$ = "MOUNT"
That is quite reasonable and it works. If you say
PRINT N2$
the computer will then print out MATTINSON.
However, you cannot tell it to print out the 5th entry or the fourth entry. The computer doesn't have any way of knowing that N5$ is the 5th entry. Using arrays, though, we can pick out the 5th entry in a long list and that is very useful.
The first thing we have to do is to tell the computer how large an array we are going to use. This is done with a DIM statement - e.g.
DIM N$(5)
this creates an array (a table) and we can then say
N$(1) = "SARDESON"
N$(2) = "MATTINSON"
N$(3) = "MOIR"
N$(4) = "ALLEN"
N$(5) = "MOUNT"
If we follow that with
X = 1
and then say
PRINT N$(X)
the computer will print "SARDESON".
Note that the X was a variable which, in this case, had the value of 1.
Here is a complete program - as far as we have got.
10 DIM N$(5)
20 N$(1)="SARDESON"
30 N$(2)="MATTINSON"
40 N$(3)="MOIR"
50 N$(4)="ALLEN"
60 N$(5)="MOUNT"
70 PRINT "WHICH ENTRY DO YOU WANT"
80 INPUT X
90 PRINT N$(X)
100 GOTO 70
We could also define an array to contain the five years of birth
200 DIM Y(5)
210 Y(1)=1964
220 Y(2)=1960
230 Y(3)=1950
240 Y(4)=1959
250 Y(5)=1962
It would be easy to add lines to this program to make the computer search for various things. Of course with only 5 entries it would undoubtedly be quickest to do the whole thing manually - but with a hundred, a thousand or a million entries the computer would be faster - and certainly more accurate. A few examples of extra lines will make the use of these arrays clearer.
To print out everyone born before 1963
300 FOR X=1 TO 5
310 IF Y(X)<1963 THEN PRINT N$(X)
320 NEXT X
or to print out everyone whose name contains more than 5 letters
400 FOR X=1 TO 5
410 J$=N$(X)
420 IF LEN (J$)>5 THEN PRINT J$
430 NEXT
or print out every one whose name begins with M,
500 FOR X=1 TO 5
510 J$=N$(X)
520 IF LEFT$(J$,1)="M" THEN PRINT J$
530 NEXT
All these things can only be done if the computer is able to select a position in a list and it can only do this with arrays.
Note: for an explanation of how the last examples worked, see section 22.
You will have noticed that we used the array N$(X) to store strings (the names of the people), and array Y(X) to store numbers (the year of birth). Each element of the array N$(X) can store as long a name as you want (up to 255 characters) and you can DIMension N$ to have as many entries as you want. For example, DIM N$(1000) would create a string array with space for 1000 different names. N$(X) is called a "string array" since it is used to store strings.
The array Y(X) is called a "numeric array" and again it can have as many elements (entries) as you need - e.g. DIM
Y(2000). You can also have "integer numeric arrays" like DIM J%(100).
As usual on the BBC computer the story doesn't finish there! There is another whole group of arrays which we haven't met yet. The arrays we have met (both string and numeric) are all "single dimension arrays" and could be illustrated by this diagram.
Y(1) Y(2) Y(3) Y(4) Y(5)
1964 1960 1950 1959 1962
Now suppose we wanted to store the day and month of the birthday as well as the year. We need more boxes.
21 12 4 24 19
2 2 2 10 12
1964 1960 1950 1959 1962
A set of data like that is called a "5 by 3 array" and the (empty) boxes can be set up by the statement
10 DIM Y(5,3)
The array could then be filled with the statements
20 Y(1,1)=21
30 Y(1,2)=2
40 Y(1,3)=1964
50 Y(2,1)=12
60 Y(2,2)=2
70 Y(2,3)=1960 etc.
In practice it would involve a lot less typing, and make the program shorter, if all the figures were held in DATA statements. You may well need to skip this section at first and return to it when you have understood section 22 which deals with the keywords READ, DATA and RESTORE
If you use READ and DATA to fill the above 5x3 array the program could look like this
10 DIM Y(5,3)
20 FOR COLUMN=1 TO 5
30 FOR ROW=1 TO 3
40 READ Y(COLUMN,ROW)
50 NEXT ROW
60 NEXT COLUMN
500 DATA 21,2,1964
510 DATA 12,2,1960
520 DATA 4,2,1950
530 DATA 24,10,1959
540 DATA 19,12,1962
The program above takes successive numbers from the DATA statements and inserts them into the array. Once this program has been run the array will be set up - full of the figures - and other sections of the program (not shown above), could search the array as required. The array above is a "two-dimensional array" used to store numbers. The phrase "two dimensional" refers to the fact that there are 5 entries in one dimension and 3 entries in another dimension - a total of 15 entries. A three dimensional array could be defined with the statement
DIM W(4,5,6)
and for a four dimensional array with
DIM T(2,2,5,3)
This last array would have 2x2x5x3 (60) individual entries. Actually, array elements can be numbered from zero instead of, so an array declared with
DIM V(3)
has, in fact, got 4 elements which are V(0), V(1), V(2), and V(3). Similarly the array T(2,2,5,3) has 3x3x6x4 (216) elements and will take up over 1000 bytes of memory. Multi-dimension arrays are voracious memory eaters - only use them when needed and, if at all possible, use every element that you set up. There is no limit, other than lack of memory, on the number of dimensions in an array.
At the start of this section we set up a string array with the statement DIM N$(5).
This contains 6 elements N$(0) to N$(5). The length of each string element is limited to the usual 255 characters but you can have as many elements as you wish and as many dimensions - just as for numeric arrays. String arrays are even more ravenous for memory than numeric arrays - use them sparingly!
Just to make sure that the various possibilities are clear, here is a program to set up a string array with first names as well as last names. The program reads names and dates into two arrays:
10 DIM Y(4,2)
20 DIM N$(4,2)
30 FOR COLUMN=0 TO 4
40 FOR ROW=0 TO 2
50 READ Y(COLUMN, ROW)
60 NEXT ROW
70 FOR ROW=0 TO 2
80 READ N$(COLUMN, ROW)
90 NEXT ROW
100 NEXT COLUMN
500 DATA 21,2,1964, JAMES,C,SARDESON
510 DATA 12,2,1960, A, MICHAEL, MATTINSON
520 DATA 4,12,1960, CHARLES,C,MOIR
530 DATA 24,10,1959, STEPHEN, R, ALLEN
540 DATA 19,12,1962, GAVIN,,MOUNT