` COMPUTE! ISSUE 40 / SEPTEMBER 1983 / PAGE 221`

PROGRAMMING THE TI

C. Regena

Subscripted Variables

TI BASIC allows variable names to be subscripted, or used in arrays of up to three dimensions. Examples of subscripted variables are A(1), ING\$(2,6), and N(7,2,8).
Both numeric and string variables may use subscripts, which are written as numbers in parentheses after the variable name. The subscript itself may be a numeric variable or numeric expression. One constraint is that you cannot use the same variable name both with and without a subscript; that is, you cannot use the variable N and the variable N(3).

Just Like Mailboxes
I often think of variables as a mailbox system in memory:

 A B

Here are two variables, named A and B. Initially, they each have the value of zero. As your program runs, you may assign values to these boxes. Suppose you have the statements:

100 B=7
150 A=A+1

The computer will put the value 7 in B's mailbox, then any later statement using B will simply use 7 in the formula instead of B. Line 150 says to add 1 to the value that is currently in A, then place the new value in A.
Some mailboxes are larger than others, and I compare these to subscripted variables. You might think of it as a big box for the Smith family - the first part of the box for John, the second part for James, and the third part for Jeremy. Here is our mailbox again:

 A B C1 C2

The C box actually holds two values, which are written in TI BASIC as C(1) and C(2).
Boxes can be even larger - representing 1, 2, or 3 "dimensions," or using 1, 2, or 3 numbers in the subscripts. C(2) is the second element in the one-dimensional array of C above. N(2,4) would be an element in a two-dimensional array. X(3,4,2) would be an element in a three-dimensional array.

Arrays Are Workhorses
Arrays or subscripted variables can make a computer program more efficient in many cases. If you use a process several times, it may be worth using a variable with a subscript rather than several variables.
For example, suppose you are using your computer to sort a list of 25 students with their scores on a particular test. You could use the following method:

200 INPUT A\$,A (FIRST STUDENT, SCORE)
210 INPUT B\$,B (SECOND STUDENT, SCORE)
220 INPUT C\$,C (THIRD STUDENT, SCORE)

ETC., FOR 25 STUDENTS

.
.
.
(SORT ROUTINE USING 25 VARIABLES)

.
.
.
600 PRINT A,A\$
610 PRINT B,B\$
620 PRINT C,C\$

ETC., FOR 25 SORTED SCORES AND STUDENTS.

Using arrays or subscripted variables, you could INPUT the names as the N\$ array and the corresponding scores in the SC array, sort, and then print using this method:

200 FOR C=1 TO 25
210 INPUT N\$(C),SC(C)
220 NEXT C
(SORT ROUTINE)
600 FOR C=1 TO 25
610 PRINT SC(C),N\$(C)
620 NEXT C
Here is another example program that would be considerably longer if you did not use subscripted variables. Lines 110-130 READ from DATA a subject, a verb, and a phrase and put them in the S\$, V\$, and P\$ arrays. Lines 140-190 contain the data (you could combine data lines if you wish). For the first time through the loop, S\$(1) would be "I", V\$(1) would be "RAN", and P\$(1) would be "TO OUR HOUSE." S\$(2) is "HE", V\$(2) is "WALKED", and P\$(2) is "TO THE STORE."; and so forth.
Line 200 uses the DEF function to define R6 as a random integer from 1 to 6. Each time R6 is used in the program, the computer will choose a random number from 1 to 6.
Line 210 clears the screen, and line 220 prints a title. Lines 230-240 choose a random S\$, a random V\$, and a random P\$ to make up a sentence and print it. Line 250 returns to line 230 to repeat the process until you press CLEAR.
100 REM RANDOM SENTENCES
110 FOR C=1 TO 6
120 READ S\$(C),V\$(C),P\$(C)
130 NEXT C
140 DATA I,RAN,TO OUR HOUSE.
150 DATA HE,WALKED,TO THE STORE.
160 DATA SHE,HOPPED,AROUND THE ROOM.
170 DATA IT,SPED,UP THE HILL.
180 DATA WE,ZOOMED,ACROSS THE GRASS.
190 DATA YOU,JUMPED,ALONG THE PATH.
200 DEF R6=INT(6*RND)+1
210 CALL CLEAR
220 PRINT "** RANDOM SENTENCES **":::
230 RANDOMIZE
240 PRINT :S\$(R6);" ";V\$(R6);" ";P\$(R6)
250 GOTO 230
260 END

Memory Reserved
As soon as you specify a variable name with a subscript, the computer automatically reserves memory for an array with that name. If you use a variable D(3), the computer will automatically reserve elements up to D(10). In two-dimensional arrays, the computer will reserve up to N(10,10); and in three-dimensional arrays, the computer will reserve up to X(10,10,10).
If you need more than ten elements, use a DIMension statement to clear enough space. For example, for our 25 students and 25 scores in the program discussed previously, we would need a DIMension statement:

100 DIM N\$(25),SC(25)

If your program is running nearly full memory and you do not need all the elements automatically reserved, you may save memory by dimensioning the array for the exact number you need:

100 DIM N(6)

The DIMension statement must appear before any reference to the array. I usually put my DIMension statements near the beginning of the program. You may specify several variables in one DIMension statement.
The computer actually starts all subscripts with the zero element, N(0). Thus, the automatic dimensioning includes 11 elements in arrays. If you prefer to use only elements numbered 1 and above, you may use the OPTION BASE statement to avoid reserving space for the zero elements:

100 OPTION BASE 1
110 DIM D(25,6)

Note: The OPTION BASE 1 statement must precede the DIM statement.

Combining The Ingredients
Following is an educational program which illustrates the use of subscripted variables. The program prints a recipe conversion problem for a math competency test. First, one of three recipes is printed. A random ingredient is chosen, and a random multiplication factor is chosen to print the problem. The student must choose from four possible answers.
Line 140 DIMensions the R\$ array and the R array so the first subscript may go up to 3 and the second subscript may go up to 6. The first subscript will actually be 1, 2, or 3, which will correspond to the first, second, or third recipe. R\$(C,0) will contain the title of the recipe for each of the three recipes. R(C,0) will be the number of servings each of the three recipes will make. R(C,I) and R\$(C,I) contain the amount and the ingredient, where C is the recipe number and I is from 1 to 6. The values are read in as DATA in lines 150-230.
Lines 410-440 define values for the elements of the J array. These elements are multiplication factors for the conversion problem. These variables are used first to choose a factor for the problem, then to calculate the multiple-choice answers.

Program Structure

 Lines 100-130 Print title screen. 140 DIMension arrays for recipe elements. 150-200 READ from DATA the values for the R\$ and R arrays. 210-230 DATA for recipes (please be careful while copying these lines-watch the commas and decimals). 240 Branch around subroutines. 250-390 Subroutines to convert decimals to fractions for printing the recipes and the multiple-choice answers. 400 Clear screen for problem. 410-440 Define multiplication factors. 450-460 Randomly choose Recipe 1, Recipe 2, or Recipe 3. The variable C refers to the recipe number. 470-480 Print title of recipe and number of servings. 490-530 Print amount, measure, and ingredient six times. One of the recipes contains only five ingredients, so line 500 checks for a zero value. Line 510 converts the amount from a decimal to a fraction if necessary. 540-560 Randomly choose a multiplication factor for the problem. If F =1 then J(1) =1 which indicates no recipe conversion, and another number is chosen. 570-590 Draw a horizontal line of a random color under the given recipe. 600-640 Print the question, where A is the randomly chosen ingredient. 650 Calculate correct answer as N1. 660-750 Randomly print multiple-choice answers. 760-780 Sound a "beep" then wait for answer. 790-820 If answer is incorrect, play "uh-oh" and return for another answer. 830-870 Indicate correct answer and play arpeggio. 880-910 Print option to try another problem and branch appropriately. 920-930 Clear screen and END.

Math Competency Recipe Conversion
100 CALL CLEAR
110 PRINT TAB(6);"MATH COMPETENCY"
120 PRINT :::TAB(5);"RECIPE CONVERS
ION"
130 PRINT
140 DIM R\$(3,6),R(3,6)
150 FOR C=1 TO 3
160 READ R\$(C,0),R(C,0)
170 FOR 1=1 TO 6
180 READ R(C,I),R\$(C,I)
190 NEXT I
200 NEXT C
210 DATA CHEESE SOUFFLE,2,2,TBSP BU
TTER,2,TBSP FLOUR,1,C. MILK,.75
,C. GRATED CHEESE,2,EGGS,.5,TSP
SALT
220 DATA DUMPLINGS,4,1,C. FLOUR,2.T
SP BAKING POWDER,.5,TSP SALT,.5
,C. MILK,2,TBSP SALAD OIL,0,""
230 DATA PRONTO PUPS,6,2,EGGS,.5,C.
MILK,.75,C. FLOUR,I,TSP BAKING
POWDER,1,TSP SALT,.5,C. CORN M
EAL
240 GOTO 400
250 N=R(C,I)
260 IF N<1 THEN 290
270 N\$=STR\$(N)
280 RETURN
290 IF N<>.75 THEN 320
300 N\$="3/4"
310 RETURN
320 IF N<>.5 THEN 350
330 N\$="1/2"
340 RETURN
350 IF N<>.375 THEN 380
360 N\$="3/8"
370 RETURN
380 N\$="1/4"
390 RETURN
400 CALL CLEAR
410 J(0)=.5
420 J(1)=1
430 J(2)=2
440 J(3)=4
450 RANDOMIZE
460 C=INT(RND*3)+1
470 PRINT TAB(7);R\$(C,0)
480 PRINT :"SERVES";R(C,0)::
490 FOR I=1 TO 6
500 IF R(C,I)=0 THEN 530
510 GOSUB 250
520 PRINT N\$;TAB(5);R\$(C,I)
530 NEXT I
540 F=INT(RND*4)
550 IF F=1 THEN 540
560 F=J(F)
570 H=INT(RND*12)+5
580 CALL COLOR(13,H,H)
590 CALL HCHAR(24,1,128,32)
600 PRINT :::"IF YOU WANTED TO MAKE
"
610 PRINT R\$(C,0);" TO SERVE";F*R(C
,0)
620 A=INT(RND*5)+1
630 PRINT "HOW MANY ";R\$(C,A)
640 PRINT "WOULD YOU NEED?"::
650 N1=F*R(C,A)
660 FOR CH=1 TO 4
670 X=INT(RND*4)
680 IF J(X)=-1 THEN 670
690 N=J(X)*R(C,A)
700 IF N1<>N THEN 720
710 ANS=CH
720 GOSUB 260
730 PRINT TAB(6);CHR\$(64+CH);" "&N\$
740 J(X)=-1
750 NEXT CH
760 CALL SOUND(150,1497,2)
770 CALL KEY (0, K, S)
780 IF S<1 THEN 770
790 IF K=ANS+64 THEN 830
800 CALL SOUND(100,330,2)
810 CALL SOUND(100,262,2)
820 GOTO 770
830 CALL HCHAR(19+ANS,7,42)
840 CALL SOUND(100,262,2)
850 CALL SOUND(100,330,2)
860 CALL SOUND(100,392,2)
870 CALL SOUND(200,523,2)
880 PRINT :"ANOTHER PROBLEM? (Y/N)"
;
890 CALL KEY(0,K,S)
900 IF K=89 THEN 400
910 IF K<>78 THEN 890
920 CALL CLEAR
930 END