Dimensioning Arrays - Commodore 128 System Manual

Table of Contents

Advertisement

Notice there are not many changes. Line 5 is the only new
statement. It tells the computer to set aside five boxes in memory
for the X array. Line 30 has been changed so that each time the
computer executes the loop, it assigns a value from the DATA
statement to the position in the X array that corresponds to loop
counter (J). Line 40 calculates the total, just as it did before, but
you must use a subscripted variable to do it.
After you run the program, if you want to recall the third number,
type ?X(3)<RETURN>. The computer remembers every number
in the array X. You can create string arrays to store the
characters in string variables the same way. Try updating the
COMMODORE 128 COMPUTER READ-DATA program so the
computer will remember the elements in the A$ array.
5 DIM A $(3)
10 FOR J=1 TO 3
20 READ A$(J)
30 ? A$(J)
40 NEXT
50 END
60 DATA COMMODORE,C128,COMPUTER
TIP: You do not need the DIM statement in your program unless
the array you use has more than 10 elements. See

DIMENSIONING ARRAYS.

Dimensioning Arrays
Arrays can be used with nested loops, so the computer can
handle data in a more advanced way. What if you had a large
chart with 10 rows and 5 numbers in each row. Suppose you
wanted to find the average of the five numbers in each row. You
could create 10 arrays and have the computer calculate the
average of the five numbers in each one. This is not necessary,
because you can put all the numbers in a two-dimensional array.
This array would have the same dimensions as the chart of
numbers you want to work with— 10 rows by 5 columns. The DIM
statement for this array (we will call it array X) should be:
10 DIM X(10,5)
4-14

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

128d

Table of Contents