` COMPUTE! ISSUE 79 / DECEMBER 1986 / PAGE 91`

# The Beginner's Page

## C. Regena

### Arrays

Sometimes you need to tag a whole list of information with names that the computer can manipulate. Usually, you give things names by assigning variable names to them. For example, you can write A\$ = "APPLES" and B\$ = "MILK" so that whenever you PRINT B\$, you'll see the word MILK appear.

However, there's an even more effective way to manipulate related information: arrays. Let's assume you are making up a shopping list in the example above. When you're ready to print out the list, you'll need to PRINT A\$; PRINT B\$; and so on down the whole list. But using the array A\$(l)="AP-PLES": A\$(2)="MILK" instead will let you print out the whole list easily:

10 FOR I = 1 TO 35

20 PRINT A\$(I)

30 NEXT I

Even if there are 35 items in this list (as in the example program above) you'll still only need these three lines to print out the whole list. Likewise, if you're reading a list from DATA statements into an array, this is a more efficient way to program than to assign individual variables to each item:

10 FOR I = 1 TO 35

30 NEXT I

#### Varieties Of Arrays

Arrays can be used with either numeric or string variables on most computers. An exception is that many versions of BASIC on the Atari do not allow string arrays.

Let's look at another example of how arrays can be used. Suppose you're describing four boys. We can assign names in BASIC with these statements:

NAME\$(1) = "RICHARD"

NAME\$(2) = "ROBERT"

NAME\$(3) = "RANDY"

NAME\$(4) = "BRETT"

More arrays can be used to list traits about each of the boys:

AGE(1) = 15

AGE(2) = 10

AGE(3) = 6

AGE(4) = 1

COLOR\$(1) = "BLACK"

COLOR\$(2) = "RED"

COLOR\$(3) = "BLUE"

COLOR\$(4) = "PURPLE"

SPORT\$(1) = "BASEBALL"

SPORT\$(2) = "FOOTBALL"

SPORT\$(4) = "BASEBALL"

With the information set up in this way, your program can provide a list of the boys by using a single loop and a variable subscript as we did in the shopping list example. Printing information using different variable names would take quite a few statements, but using arrays can make programming more efficient:

200 FOR J = l TO 4

210 PRINT NAME\$(J);AGE(J),COLOR\$(J), SPORT\$(J)

220 NEXT J

#### Avoiding Repetitive Programming

Also, searching and sorting data becomes easier when you use arrays. If you want to know about a particular boy, you can print only his information by searching the arrays for his particular subscript. To find out Randy's favorite color:

300 N=3

310 PRINT NAME\$(N),COLOR\$(N)

Although this example uses only four boys, you can see how much arrays can reduce repetitive programming. In a larger list of people, for example, you might want to find all the ten-year-old boys. Let T be the total number of boys in the list. In the following loop, each age in the AGE array is checked. If the age is 10, then line 420 is executed and the name is printed.

400 FOR C = l TO T

410 IF AGE(C)<>10 THEN 430

420 PRINT NAME\$(C)

430 NEXT C

One final point before we go on to more complicated arrays. Most BASICs require that you DIMension an array if it's going to be larger than ten elements:

10 DIM AGE(60)

This would set aside enough memory to hold 60 different items in the AGE array. And you must DIM early in your program (before you actually make use of the array).

#### Many Dimensions

If you want to get really fancy, you can create an array of two or more dimensions. (In practice, however, few programmers ever need to get this fancy.) In any case, you use two numbers separated by a comma to indicate two dimensions. For example, instead of using 12 different variable names for related items, you can use a two-dimensional array:

C(1,1) C(l,2) C(1,3)

C(2,1) C(2,2) C(2,3)

C(3,1) C(3,2) C(3,3)

C(4,1) C(4,2) C(4,3)

Each element names its own memory location and contains its own value. In a two-dimensional array, you can visualize the elements by thinking the first number indicates the row and the second number indicates the column.