Pages 67–70 · Markdown
Suppose you have a list of numbers, for instance the marks of ten people in a class. To store them in the computer you could set up a single variable for each person, but you would find them very awkward. You might decide to call the variable Bloggs 1, Bloggs 2, and so on up to Bloggs 10, but the program to set up these ten numbers would be rather long and boring to type in.
How much nicer it would be if you could type this:
5 REM this program will not
work
10 FOR n=1 TO 10
20 READ Bloggs n
30 NEXT n
40 DATA 10,2,5,9,16,3,11,1,0,6
Well, you can't!
However, there is a mechanism by which you can apply this idea, and it uses arrays. An array is a set of variables, its elements, all with the same name, and distinguished only by a number (the subscript) written in parentheses after the name. In our example the name could be b and the ten variables would then be b(1), b(2), and so on up to b(10).
The elements of an array are called subscripted variables, as opposed to the simple variables that you are already familiar with.
Before you can use an array, you must reserve some space for it inside the computer, and you do this using a DIM (for dimension) statement:
DIM b(10)
sets up an array called b with dimension 10 (i.e. there are 10 subscripted variables b(1),...,b(10)) and initialises the 10 values to 0. It also deletes any array called b that existed previously. (But not a simple variable. An array and a simple numerical variable with the same name can coexist, and there shouldn't be any confusion between them because the array variable always has a subscript). The subscript can be an arbitrary numerical expression, so now you can write:
5 DIM b(10)
10 FOR n=1 TO 10
20 READ b(n)
30 NEXT n
40 DATA 10,2,5,9,16,3,11,1,0,6
to read in the elements from a DATA list, or:
10 FOR %n=1 TO 10
20 INPUT %m(n)
30 NEXT %n
to INPUT the elements' values by hand. Note, that in the second example there is no DIM statement. That's because as discussed in Chapter 1, the second array is an integer array. Integer arrays come predimensioned to a fixed 64 elements numbered 0 to 63. Attempting
to enter a DIM statement for %m will produce an audible tone and entering the statement will not be successful.
If we need to use an integer array with more than 64 elements, it is possible although what changes is the way we have to address them. Whereas in a normal integer array the subscript is written inside parentheses () for integer arrays larger-than-64-elements, the subscript is written within brackets []. Furthermore, larger-than-64-elements integer arrays reduce the number of available integer arrays in the system as they take the entire array that follows sequentially from the one we're using and attach it to the current one. What this means is that if we want to use a 128 element integer array %a[], this will take the space from integer array %b(). If we want to use an 192 element integer array %c[], this will use space from integer arrays %d() and %e() and so on.
The maximum integer array usable is 26 x 64 =1664 if using integer array %a[] with no other arrays available. Note that subsequent arrays don't disappear; they're still accessible carrying data from the integer array that reserved them. Modifying them however may have unexpected consequences. To illustrate this point, let's assume an integer array %a[] with a desired 128 elements. Write the following little program:
10 %a[65] = 43
20 PRINT %a[65]
30 PRINT %b(1): REM the 65th
element of array a[] is
b(1)
It's now obvious how this works!
You can also set up arrays with more than one dimension. This does also apply to Integer Arrays, although they're normally predefined to have a single dimension; you'll see how below. In a two-dimensional array you need two numbers to specify one of the elements – rather like the line and column numbers to specify a character position on the television screen – so it has the form of a table or matrix.
Alternatively, if you imagine the line and column numbers (two dimensions) as referring to a printed page, you could have an extra dimension for the page numbers. Of course, we are talking about numeric arrays; so the elements would not be printed characters as in a book, but numbers. Think of the elements of a three-dimensional array v as being specified by v (page number, line number, column number).
For example, to set up a two-dimensional array c with dimensions 3 and 6, you use a DIM statement:
DIM c(3,6)
This then gives you 3 x 6=18 subscripted variables:
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 1 | c(1,1) | c(1,2) | c(1,3) | c(1,4) | c(1,5) | c(1,6) |
| 2 | c(2,1) | c(2,2) | c(2,3) | c(2,4) | c(2,5) | c(2,6) |
| 3 | c(3,1) | c(3,2) | c(3,3) | c(3,4) | c(3,5) | c(3,6) |
Table 3 – Representation of a two-dimensional array
The same principle works for any number of dimensions.
Although you can have a number and an array with the same name, you cannot have two arrays with the same name, even if they have different numbers of dimensions except in the case of normal numerical and integer arrays.
As we mentioned above integer arrays can have a second dimension as well. This follows the discussion of extending integer arrays to larger than 64 elements. The technique is similar; If a two-dimensional integer array is required, we enclose subscripts within brackets []. The difference here is that subscripts need to be individually enclosed: For example whereas we would address regular array c() defined with DIM c(4,64) with c(x,y) in the case of its integer counterpart we would address it as %c[x][y]. Each x dimension takes one entire array that follows the base array name. For example using %c [x][y] with x=0 to 5 and y= 0 to 63 will use arrays %C(),%D(),%E(),%F(),%G() and %H()
There are also string arrays. The strings in an array differ from simple strings in that they are of fixed length and assignment to them is always Procrustean – chopped off or padded with spaces. Another way of thinking of them is as arrays (with one extra dimension) of single characters. The name of a string array is a standard variable name followed by $, and a string array and a simple string variable cannot have the same name (unlike the case for numbers).
Suppose then, that you want an array a$ of three strings. You must decide how long these strings are to be – let us suppose that 10 characters each is long enough. You then say:
DIM a$(3,10)
(type this in)
This sets up a 3*10 array of characters, but you can also think of each row as being a string:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | a$(1) | a$(1,1) | a$(1,2) | a$(1,3) | a$(1,4) | a$(1,5) | a$(1,6) | a$(1,7) | a$(1,8) | a$(1,9) | a$(1,10) |
| 2 | a$(2) | a$(2,1) | a$(2,2) | a$(2,3) | a$(2,4) | a$(2,5) | a$(2,6) | a$(2,7) | a$(2,8) | a$(2,9) | a$(2,10) |
| 3 | a$(3) | a$(3,1) | a$(3,2) | a$(3,3) | a$(3,4) | a$(3,5) | a$(3,6) | a$(3,7) | a$(3,8) | a$(3,9) | a$(3,10) |
Table 4 – Representation of a string array
If you give the same number of subscripts (two in this case) as there were dimensions in the DIM statement, then you get a single character; but if you miss the last one out, then you get a fixed length string. So, for instance, a$(2,7) is the 7ᵗʰ character in the string a$(2); using the slicing notation, we could also write this as a$(2)(7). Now type:
a$(2)="1234567890"
and:
PRINT a$(2),a$(2,7)
You get:
1234567890 7
For the last subscript (the one you can miss out), you can also have a slicer, so that for instance:
a$(2,4 TO 8) = a$(2)(4 TO 8) = "45678"
Remember: in a string array, all the strings have the same –fixed– length. The DIM statement has an extra number (the last one) to specify this length. When you write down a subscripted variable for a string array, you can put in an extra number, or a slicer, to correspond with the extra number in the DIM statement. You can have string arrays with no dimensions. Type:
DIM a$(10)
and you will find that a$ behaves just like a string variable, except that it always has length 10, and assignment to it is always Procrustean. Whatever part of the value doesn't fit gets left out.
Apart from the DIM array declaration command, there's also a function by the same name that returns information regarding any declared array. It's syntax is:
DIM (arrayname$ [, dimension]) |numeric and it returns the number of elements in the specified dimension of the array arrayname (dimension defaults to 0 if not specified).
If dimension equals 0, DIM will simply return the number of dimensions in the array.
Simple strings are treated as single-dimension character arrays, returning 1 as the number of dimensions and the current string length as the number of elements in dimension 1.
Let's write a little program to illustrate:
10 DIM a(100,10,5)
20 PRINT DIM (a())
30 PRINT DIM(a(),1)
40 PRINT DIM(a(),2)
50 PRINT DIM(a(),3)
which will return:
3
100
10
5
Use READ and DATA statements to set up an array m$ of twelve strings in which m$(n) is the name of the nᵗʰ month. (Hint: the DIM statement will be DIM m$(12,9). Test it by printing out all the m$(n) (use a loop)).
Type:
PRINT "now is the month of
";m$(5);"ing"; " when
merry lads
are playing"
What can you do about all those spaces?
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.