ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 22.2 - Arrays

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:

READ X

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

30 READ M$(N)

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.

Sinclair ZX Spectrum

  Previous Page Back Next Page