EasyManuals Logo

Sinclair ZX81 User Manual

Sinclair ZX81
92 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #62 background imageLoading...
Page #62 background image
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 lements 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

Other manuals for Sinclair ZX81

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Sinclair ZX81 and is the answer not in the manual?

Sinclair ZX81 Specifications

General IconGeneral
ProcessorZilog Z80A
Processor Speed3.25 MHz
ROM8 KB
InputMembrane keyboard
StorageCassette tape
Power Supply9V DC
Dimensions167 mm x 40 mm x 175 mm
Release Year1981
ManufacturerSinclair Research
Weight350 g
Operating SystemSinclair BASIC
RAM1 KB
PortsTV out, expansion port
DisplayTelevision (RF modulator output)

Related product manuals