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 pnsoners.
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.
•
•
west(5)
west(4)west(3)
It
m
II
west(2)west(l)
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
west
(1)
west(2) west(3)
west(4) wes\(5)
The
complete program
is
shown below:
100 REMark
Prisoners
110
DIM
west(5)
120
FOR
cell
=1
TO
5 :
READ
west(cell)
130
FOR
cell
=1
TO
5:
PRINT
cell
I
west(celLJ
140
DATA
50,
37,
86,
41,
32
The
output from the program
is:
1 50
237
3
86
4
41
5 32
The numbers 1
to
5
are
called subscripts of the array
name,
west The
array,
west,
is
a numeric array consisling
of
five
numeric array elements.
You
can replace line
130
by:
130
PRINT
west
This
will
output the values only:
o
50
37
86
41
32
The
zero
at
the top
of
the
list
appears because subscripts range from zero
to
the declared
number.
We
will
show later how useful the zero elements
in
arrays can
be.
Note also that when a numeric array
is
DIMensioned
its
elements
are
all
given the value
zero.
•
•
74 12/84