BY DAVID PLOIKIN, ANTIC CONTRIBUTING EDITOR
New Owners Column
lesson 9: Subscripted Variables
This series, which started in the March, 1986 Antic,
teaches beginners how to program in BASIC on all Atari 8-bit computers
such as the 800XL and the 130XE. Contributing Editor David Plotkin is a
chemical engineer and longtime Atari programmer.
Subscripted variables are very useful for storing and manipulating information. As you gain more programming experience, you'll learn that sometimes it can be limiting to hold values in standard variables (as explained in Antic, May 1986, page 107). Each variable must have a unique name, which must be kept track of. Also, Atari BASIC limits you to 128 different variables. Normally this is enough, but it's possible to run out.
An example of this might be a starship simulation program. Imagine that your universe is broken up into 120 sectors, 10 across and 12 down. Keeping track of all 120 sectors, each with a different variable name, would be cumbersome. Such situations cry out for subscripted variables, which make your job much easier.
Subscripted variables can hold a whole series of values at the same
time because of their subscripts, which provide an index to each value.
Each element of the subscripted variable has a different subscript
and can hold a different value. An example of a subscripted variable is:
EXAMPLE(2) = 3.5, where EXAMPLE is the name of the variable, 2 is the subscript,
and the value 3.5 is the second element of EXAMPLE.
VARIABLES IS LIMITED.
SUBSCRIPTS PICK UP THE SLACK
Subscripted variables are given names just like regular variables. However, they are different from regular variables in two important ways. First, you must tell your Atari how much room to save for the values in each set of subscripted variables. This is done with the DIM statement. Second, values stored in the subscripted variable are accessed by use of the appropriate subscript.
DIM STATEMENT
After you decide on a name for a subscripted variable, you must allocate
memory to hold its values. The DIM statement DIMensions the subscripted
variable. For example, if the variable UNIVERSE is to hold 100 values,
then you would use the following:
10 DIM UNIVERSE(100)
You can DIMension more than one subscripted variable on each line:
10 DIM UNIVERSE(100),STAR(50),HIT(10)
Subscripted variables may also have two subscripts, in which case the DIM statement might look like this:
20 DIM DOUBLE(20, 20),DOUBLE2(10,20)
Note that you can DIM more than one doubly-subscripted variable on a line, just as with singly-subscripted variables. You may also DIM both singly-subscripted and doubly-subscripted variables in the same DIM statement. You may not use variables with more than two subscripts. And it is important to know that DIMensioning a subscripted variable more than once in a program generates an error.
SINGLE SUBSCRIPTS
Singly-subscripted variables have one subscript that must be an integer,
and each element can hold a different value:
10 DIM EXAMPLE(10)
20 FOR J=0 TO 10:EXAMPLE(J)=J*2.2
30 PRINT "EXAMPLE(" ;J; ")= ";EXAMPLE (J)
40 NEXT J
Thus, the subscripted variable EXAMPLE can hold 11 different values, one for each value of the integer subscript which was DIMensioned to 10. Although the subscript must be an integer, the value held by EXAMPLE(J) can be any number, including a calculated number. You can also simply assign a value to a subscripted variable:
50 EXAMPLE(0)=100.345
Another oddity about subscripted variables is that the subscripts start from zero. Thus, for example, when you DIMension a variable subscript for 10, you will actually have space for 11 values.
Singly-subscripted variables have many uses. This month's program Listing 1 demonstrates how they can be used to record test scores for a full classroom of students. Subscripted variables are quite commonly used with READ/DATA statements. Notice that you cannot READ the value contained in the DATA statement directly into the subscripted variable. The following will not work:
10 READ SCORE(J)
Instead, you must READ the value into a regular variable, then transfer it into the subscripted variable:
10 DIM SCORE(1)
20 READ SCORE:SCORE(0)=SCORE
The above is perfectly valid, because your Atari sees SCORE and the subscripted variable SCORE(J) as two different variables. The same principle applies when you use INPUT and GET to obtain a value for a subscripted variable-you must INPUT or GET a regular variable, then equate the subscripted variable to the regular variable:
10 DIM SCORE(1)
20 PRINT "What Score";:INPUT SCORE
30 SCORE(0) = SCORE
OVER 5,000 VALUES-
A STANDARD VARIABLE, ONLY ONE
DOUBLE-SUBSCRIPT ARRAYS
Each subscript of a doubly-subscripted variable must be DIMensioned:
10 DIM UNIVERSE(10,12)
Doubly-subscripted variables are often thought of as being represented by rows and columns. Thus, UNIVERSE would have 10 rows and 12 columns, holding 10 x 12, or 120 values. Because of the analogy to rows and columns, doubly-subscripted variables are often referred to as arrays. To access the values held in an array, you must specify both subscripts:
10 DIM UNIVERSE(5,6):UNIVERSE(1,4) =2.1
20 PRINT UNIVERSE(1,4)
Again, the subscripts must be integers. The same rules for singly-subscripted variables apply to arrays.
As you can see, it is now quite easy to solve the problem of keeping track of 120 sectors in your starship simulation. Just DIMension a 10 x 12 array and store a number into each element of the array. This month's Listing 2 is an example of how you might carry this out. Notice how the nested FOR/NEXT loops are used to access each element of the array UNIVERSE by stepping through the subscripts. Using the RND (random number) function assures that there will be a different number of enemy ships in the sectors each time we play the simulation.
CALCULATED SUBSCRIPTS
I have stated that the subscripts must be integers. In fact, if you
do specify a subscript which is not an integer, your Atari will round it
to the nearest integer anyway.
You can also calculate a particular subscript by using the mathematical rules explained in Antic's July, 1986 New Owners Column. This option gives you considerable flexibility. For example, while you are limited to having two subscripts in an array, you can represent threedimensional space with a singly-subscripted variable by doing some calculations on the chosen 3-D coordinates:
10 DIM SPACE(1000)
20 FORJ = 0 TO 999:SPACE(J) RND(0):REM PUT SOME VALUES IN SPACE
30 PRINT "INPUT X,Y,Z COORDINATES (0-9)":INPUT X,Y,Z
40 IF (X<0 OR X>9 OR Y<0 OR Y>9 OR Z<0 OR Z>9) THEN PRINT
"COORDINATE OUT OF RANGE!":GOTO 30
50 PRINT "VALUE IS ";SPACE(X+Y* 10+Z* 100):REM CALC. THE SUBSCRIPT.
60 GOTO 30:REM AROUND AGAIN
Both singly-subscripted and doubly-subscripted variables can be used like regular variables in mathematical equations and in GOTO/GOSUB statements. In fact, subscripted variables can be used just about anywhere that regular variables are used-except, as stated above, in READ, INPUT, and GET statements.
FRACTAL LISTING
Listing 3 makes extensive use of arrays to store values for later use.
The program generates a shape on your screen which looks like a different
3-D landscape every time. The program uses a form of fractal arithmetic,
which generates shapes that imitate nature. Memory requirements for Listing
3 are 32K disk and 24K cassette.
When the program starts, it will ask you how many levels you want. The higher the number of levels, the finer the resolution of the picture and the more lifelike the result. But the higher-numbered levels also take longer to draw on the screen. Level 6, the highest resolution, takes several hours, so be sure you want your computer tied up that long.
Listing 1: NEWOWN8A.BAS Download
Listing 2: NEWOWN8B.BAS Download
Listing 3: NEWOWN8C.BAS Download