Chapter 22 - Arrays

Suppose you have a list of numbers, for instance the number of income tax collectors that have died each month in the current financial year. To store them in a computer you could set up a single variable for each month, but you would find this very awkward. You might decide to call the variables ALAS NO MORE 1, ALAS NO MORE 2, & so on up to ALAS NO MORE 12, but the program to print out these twelve numbers would be rather long & 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 12

20 PRINT ALAS NO MORE N

30 NEXT N

Well, you can't.

However, there is a mechanism by which you can apply this idea, & it uses arrays. An array is a set of variables, or elements, all with the same name, & distinguished only by a number (the subscript) written in brackets after the name. In our example the name would be A (like control variables for FOR-NEXT loops, the name of an array must be a single letter), & the twelve variables would then be A(1), A(2), & so on up to A(12).

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, & you do this using a DIM (for dimension) statement.

DIM A(12)

sets up an array called A with dimension 12 (i.e. there are 12 subscripted variables A(1),...,A(12)), & initializes the 12 values to 0. It also deletes any array called A that existed previously. (But not a simple variable. An array & a simple variable with the same name can coexist, & 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

10 FOR N=1 TO 12

20 PRINT A(N)

30 NEXT N

You can also set up arrays with more than one dimension. In a two- dimensional array you need two numbers to specify one of the elements - rather like the line & column numbers to specify a character position on the television screen - so it has the form of a table. Alternatively, if you imagine the line & column numbers (two dimensional) as referring to a page printed, 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 C as being specified by C (page number, line number, column number).

For example, to set up a two-dimensional array B with dimensions 3 & 6, you use a DIM statement

DIM B(3,6)

This then gives you 3*6 = 18 subscripted variables

B(1,1), B(1,2),..., B(1,6)

B(2,1), B(2,2),..., B(2,6)

B(3,1), B(3,2),..., B(3,6)

The same principal works for any number of dimensions.

Although you can have a number & an array with the same name, you cannot have two arrays with the same name, even if they have different numbers of dimensions.

There are also string arrays. The strings in an array differ from simple strings in that they are of fixed length & assignment to them is always

Procrustean - another way of thinking of them is as arrays (with one extra dimension) of single characters. The name of a string array is a single letter followed by \$, & a string array & a simple string variable cannot have the same name (unlike the case for numbers).

Suppose then, that you want an array A\$ of five 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\$(5,10)  (type this in)

This sets up a 5*10 array of characters, but you can also think of each row as being a string:

A\$(1)=A\$(1,1)  A\$(1,2)  ...  A\$(1,10)

A\$(2)=A\$(2,1)  A\$(2,2)  ...  A\$(2,10)

:         :      :              :          :         :

A\$(5)=A\$(5,1)  A\$(5,2)  ...  A\$(5,10)

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 7th character in the string A\$(2); using the slicing notation, we could also write this as A\$(2)(7). Now type

LET A\$(2)="1234567890"

&

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 slice, 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.

Summary

Arrays (the way the ZX81 handles string arrays is slightly non-standard.)

Statements: DIM

Exercises

1. Set up an array M\$ of twelve strings in which M\$(N) is the name of the Nth 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?

2. You can have string arrays with no dimensions. Type

DIM A\$(10)

& you will find that A\$ behaves just like a string variable, except that it always has length 10, & assignment to it is always Procrustean.

3. READ, DATA & RESTORE; who needs them?

Most BASICs (but not the ZX81 BASIC) have three statements called READ, DATA & RESTORE.

A DATA statement is a list of expressions, & taking all the DATA statements in the program gives one long list of expressions, the DATA list.

READ statements are used to assign these expressions, one by one, to variables:

for instance, assigns the current expression in the DATA list to the variable X, & moves on to the next expression for the next READ statement.

[RESTORE moves back to the beginning of the DATA list.]

In theory, you can always replace READ & DATA statements by LET statements; however, one of their major uses is in initializing arrays, as in this program:

5 REM THIS PROGRAM WILL NOT WORK IN ZX81 BASIC

10 DIM M\$(12,3)

20 FOR N=1 TO 12

40 NEXT N

50 DATA "JAN","FEB","MAR","APR"

60 DATA "MAY","JUN","JUL","AUG"

70 DATA "SEP","OCT","NOV","DEC"

If you only want to run this program once, you might as well replace line 30 with an INPUT statement thus:

10 DIM M\$(12,3)

20 FOR N=1 TO 12

30 INPUT M\$(N)

40 NEXT N

& you will have no extra typing to do. However, if you want to save the program, you will certainly not want to type the months in every time you

run it.

We suggest that you use this method:

(i) Initialize the array using a program like the one above.

(ii) Edit out the initialization program. (Don't use NEW, because you want to preserve the array.)

(iii) Type in the rest of the program, & save it. This will save the variables as well, including the array.

(iv) When you load the program back, you will also load the array.

(v) When you execute the program, do not use RUN, which clears the variables. Use GOTO with a line number instead.

You may alternatively be able to use the LOAD & execute technique of chapter 16, & its exercise 3. Then in stage (iii) above you will use a SAVE statement in the program, & stage (v) will be omitted altogether.

Previous: Chapter 21    Next: Chapter 23