` COMPUTE! ISSUE 30 / NOVEMBER 1982 / PAGE 132`

 The Beginner's Page Arrays Richard MansfieldSenior Editor One way to make your computer useful around the house is to have it take over some chores. It could easily do the work of your alarm clock, appointment book, address book, or yearly calendar. What's more, it could combine two jobs into one. To see how one program could do two things at once, let's construct a program which combines the functions of a calendar with an address book. We can also look at how arrays work. They can be a most valuable programming tool when you are working with lists. Because Atari BASIC follows nonstandard rules in the construction of arrays, it will not be covered here. The discussion below applies to Microsoft BASIC, which includes Apple, VIC, PET, and many other computers. When two lists are related to each other, you can put them into a multi-dimensional array and they can work together to provide information. Arrays have dimensions, usually one or two. A one-dimensional array is just another word for a common list. In our example program, there is a list of first names (Mary, Bob, Joe, Alice, Mindy) which could be a one-dimensional array. Arrays give the same variable name to a group or list of items. You might think of an array as a massive variable. The DIM statement creates an array. Line 100 shows how you can DIM an array called A\$ to contain "N" by eight items. This is a two-dimensional array. Since we want to allow for expanding the list of people (and their addresses and birthdays), we use the variable N at the start of the program which can easily be changed to show the total. So, we presently have five people, each listed in a separate DATA statement. The way we are setting up this array, we allow eight pieces of information about each person. These categories are listed in the REM statement in line 5. Imagine a honeycomb of little boxes like those on walls in the post office. Think of each row being used by just a single person. In our DIM, the computer is instructed to set aside enough memory to build five rows. And each row has to be wide enough to hold eight boxes. This is an N × 8 array. If we didn't have arrays to work with, we would; have to give a different variable name to each item of data. That would mean 40 variable names in this small example program. If you put your own address book into the DATA statements here (and change N in line 5 to equal the total number of people), you might end up with 400 or more pieces of information. Clearly, it is more practical to call Bob Riley's last name A\$(2,2), than to give it a unique variable name. What's more, we can now easily use this array, this expanded A\$, as part of a loop. (Keep in mind that A\$() is not the same as A\$. You can use both of them in a program and they won't interact; they are two entirely separate variables.) Notice how lines 100-120 quickly and easily fill up the honey­comb of the array, putting each item into its slot. Then, depending on what kind of information we are requesting – addresses or birthdays – X will point to the second or seventh column in the array. X will let us search through the last names or through the months to find the information desired. Arrays can make it very easy to solve certain kinds of programming problems. How fast could we change our program to tell us all the people who lived in a particular state? Just change line 300 to: ```300 ?" WHAT STATE": X = 5 ``` and the array will be searched for matches in the "states boxes," column five. How would you get information on matching zip codes? You can quickly change the entire function of this program by changing the value of X in line 300. To make all this even more useful, program it to let the user decide which column should be searched. Have your computer put on screen a list of the categories available (change line 5 to PRINT instead of REM) and then INPUT the user's selection. Then use the number that's INPUT as your X. This would be a short step away from a complete, cross-referenced catalog of your library, stamp collection, or whatever. The programming is easy with arrays. The harder part is the time it would take to type in the name and six items of descriptive information about each record in your collection. ```2 N = 5 : REM THE TOTAL NUMBER OF PEOPLE 5 REM 1ST NAME--LAST--STREET--CITY--STATE--ZIP--MONTH--DAY 10 DATA MARY, JONES, 15 ALHAMBRA CT., SLOMO, ARIZONA, 95221, OCT, 10 20 DATA BOB, RILEY, 37 REVELO DR., BIXBY, CA, 81000, DEC, 23 30 DATA JOE, CARGILE, 188 S. TATE ST., NEW YORK, NY, 10022, APR,11 40 DATA ALICE, SOMMERVILLE, 222 DEVLIN, MAXAPAXA, KY, 78215, JUNE, 15 50 DATA MINDY, CLOROXEUSE, 84 MARKMELLO AVE., SANDY RIDGE, PA, 16864, DEC, 1 100 DIMA\$ (N,8): FORI = 1 TON: FORJ = 1 TO 8 110 READA\$(I,J) 120 NEXT J: NEXT I 130 PRINT "WOULD YOU LIKE TO SEE: 1. ADDRESSES OR 2. BIRTHDAYS" 140 INPUT K\$ 150 K = VAL(K\$) 160 ON K GOTO 200, 300 200 PRINT "WHAT IS THE PERSON'S LAST NAME":X = 2 210 GOTO 400 300 PRINT "WHAT MONTH":X = 7 400 INPUT K\$ 410 FOR I = 1 TO N 420 IF A\$(I,X) = K\$ THEN Q = 1: GOSUB 500 430 NEXT I 450 IF Q <> 1 THEN PRINT K\$ "NOT FOUND. PLEASE CHECK SPELLING" 460 Q = 0: GOTO 130 500 Q = 1: FOR J = 1 TO 8: PRINT A\$(I,J): NEXT: PRINT: RETURN ```