Using
Arrays
to
Handle
Groups
of
Data
181
The
first
element
in
an
array
is
numbered
0,
not
1.
This
means
that
an
array
dimensioned
as
(5,3)
actually
has
6
rows
and
4
columns,
or
24
elements.
When
you
figure
the
number
of
elements
in
an
array,
add
1
to
each
dimension,
then
multiply
the
results
of
the
additions.
For
example,
if
the
array
is
dimensioned
DIM
K(2,4),
the
array
contains
(2
+
1)
* (4
+
1)
=
15
elements.
The
following
example
uses
a
two-dimensional
array
to
input
and
print
data.
The
printed
data
in
line
80
shows
how
array
elements
are
assigned
in
a
two-
dimensional
array.
10
DIMA(S,3)
20
F0RY=0T02
30
F0RX=0T03
40
INPUT"NEXT
ELEMENT"
;A(Y,X)
50
NEXTX.Y
60
FORY=0TO2
70
F0RX=0T03
80
PRIlTT"ROW";Y;"COLUM]Sr";X;//EQUALS";A(Y^:)
90
NEXTX.Y
The
following
example
uses
a
one-dimensional
array
and
a
two-dimensional
array
to
keep
track
of
data.
The
program
creates
a
4-by-4
letter
grid
like
the
one
used
in
the
game
Boggle.
Array
B
contains
numbers
for
the
current
16
letters
to
be
used
in
the
grid.
Array
N$
contains
the
50
letters
that
can
become
part
of
the
grid.
10
DIMB(3,3):DIMN$(49)
15
DATA
A,B,C,D,E^1,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,XT,V,W)X,Y,Z
20
DATA
A,E,I,O,UA,E,I,U,OrA,E)I,O,R,S,T,]Sr)M)R)S,T,W,M
25
REM
ASSIGN
NUMBERS
TO
LETTERS
30
FORY=0TO49
40
READ
N$(Y)
50
NEXTY
52
X
=
RND(0):REM
SEED
RANDOM
NUMBER
GENERATOR
53
PRINT
55
REM
RANDOMLY
ASSIGN
NUMBERS
TO
SQUARES
60
F0RX=0T03
70
F0RW=0T03
80
B(X,W)=INT(RND(l)*50)
100
PRINTN$(B(X,W));
110
NEXTW:PRINT:NEXT
X
The
FOR
. . .
NEXT
loop
in
lines
30-50
assigns
a
letter
value
to
the
elements
of
array
N$
so
that
array
element
N$(0)
equals
A,
N$(l)
equals
B,
and
so
on.
We
use
50
elements
instead
of
just
26,
so
we
can
assign
common
letters
to
more
than