Sinclair QL Beginner's Manual page 89

Hide thumbs Also See for QL:
Table of Contents

Advertisement

CHAPTER 13 – ARRAYS
Suppose you are a prison governor and you have a new prison block which is called the West Block.
It is ready to receive 50 new prisoners. You need to know which prisoner (known by his number) is in
which cell. You could give each cell a name but it is simpler to give them numbers 1 to 50.
In a computing simulation we will imagine just 5 prisoners with numbers which we can put in a DATA
statement:
Data 50, 37, 86, 41, 32
We set up an array of variables which share the name, west, and are distinguished by a number
appended in brackets.
It is necessary to declare an array and give its dimensions with a DIM statement:
DIM west(5)
This enables SuperBASIC to allocate space, which might be a large amount. After the DIM statement
has been executed the five variables can be used.
The convicts can be READ from the DATA statement into the five array variables:
FOR cell = 1 TO 5 : READ west (cell)
We can add another FOR loop with a PRINT statement to prove that the convicts are in the cells.
The complete program is shown below:
100 REMark Prisoners
110 DIM west(5)
120 FOR cell 1 = 1 TO 5 : READ west(cell)
130 FOR cell = 1 TO 5 : PRINT cell ! west(cell)
140 DATA 50, 37, 86, 41, 32
The output from the program is:
1 50
2 37
3 86
4 41
5 32
The numbers 1 to 5 are called subscripts if the array name, west. The array west, is a numeric array
consisting of five numeric array elements.
You can replace line 130 by:
130 PRINT west

Advertisement

Table of Contents
loading

Table of Contents