Chmter
61
Arrays
This is a 2-dimensional array. Each element is referred
to
by 2
subscripts:
A(row,column)
For example, A(3,2) points
to
the third week's gas expense.
To
make
a
2-dimensional array from the earlier program, add
the following lines:
25 FOR R
=
1 TO 4:RESTORE
75 NEXT R
105 FOR
R
=
1
TO 4:RESTORE
150 NEXT
R
and change these lines:
20 DIM
A(4,3):
W
=
1
50 PRINT "ENTER THE ";NAMES$;" EXPENSES
IN
DOL-
60 INPUT
A(R,C)
70
NEXT
C:
W
=
W
+
1
100 RESTORE:
W
=
1
130 PR1NT:PRINT NAMES$;" EXPENSE FOR WEEK
140 NEXT
C:W
=
W
+
1
LARS FOR WEEK NO:
"W
NO:
";W;"
=
";ACR,C)
Run this program and see how it works. We simply added an-
other subscript
to
the original array. Now instead of referring
to
an element by a row number only, we refer
to
it by both a row
and column number.
You can add yet another dimension, or subscript, to the array by
adding these lines:
22 FOR P
=
1 TO 2: RESTORE
78
W
=
1:
NEXT P
102 FOR P
=
1 TO 2: RESTORE
160
W
=
1 :NEXT P
and changing these lines:
20
DIM
A<2,4,3):W
=
1:
M
=
1
50 PRINT "ENTER THE ";NAMES$;" EXPENSE IN DOL-
60 INPUT A(P,R,C)
75 NEXT
R:
M
=
M
+
1
100 RESTORE:
W
=
1:
M
=
1
130 PR1NT:PRINT NAMES$;" EXPENSE FOR WEEK
150 NEXT
R:
M
=
M
+
1
LARS FOR WEEK NO:
";W;"
OF MONTH NO:
";M
NO:
";W;"
OF
MONTH
NO:
";M;"
=
";ACP,R,C)
Run the program
to
see how
it
works.
61