Programmers Workshop Series Part 1

Feeling DIM about arrays? This easy-to-follow article will help you see the light.

Volume 1

Number 12

February 1984

Let's make arrays work for you


AN array is a way of storing several connected variables, whether numeric or string. It is much easier to access an array element (that is one variable within the array) than it is to access' one variable out of several ordinary variables that have not been connected in the form of an array.

To illustrate this, let us first consider a badly written program (Program I) which is intended to print out a telephone number when a person's name is typed into the computer:-

10 INPUT requiredname$
20 IF reguiredname$ = "FRED" THEN PRINT 545 27364 :GOTO 10
30 IF requiredname$ = "JANE" THEN PRINT 485 4849 :GOTO 10
40 IF reguiredname$ = "JIM" THEN PRINT 475 9398 :GOTO 10
50 IF requiredname$ = "JACK" THEN PRINT 455 9989 :GOTO 10
Program I

Although it works, it has several faults which we will now try to improve.

Line 10 will generate a question mark on the screen, with no explanation as to what it is for.

Lines 20 to 40 are very similar. It should be possible to find a way to achieve the same result without the repetition (imagine a program written like this containing 1,000 names!).

Telephone numbers contain spaces. These cause problems in the printout

when they are printed as shown in this program.

There is no way to stop the program (except using ESCAPE or BREAK).

It is very difficult to add new names to the directory.

It must be realised that the use of a computer as a telephone directory is not of practical use - it is quicker to use the book. But it is an easy application to understand and illustrates the use of arrays quite clearly.

Below is a flowchart showing some of the requirements of a better directory program.

Rather than use several separate variables, this time we shall use a one dimensional array. This means that we

use a single variable name followed by a subscript (which is just a number in brackets).

For example, instead of using first-name$, secondname$, and thirdname$ as variable names we can use name$(l), name$(2), and name$(3). The advantage of this method is that all three can be referred to as name$(N) and the required one accessed by setting the variable N to the correct value.

Obviously it is much easier to go beyond three variables using this method than by using a completely new name each time.

Let us start by looking at a procedure for setting up two one-dimensional arrays, one of names and one of numbers as in Program II.

500 DEF PROCinput
510 count=0
530 count=count+1
540 READ name$(count).number$(count)
550 UNTIL name$(count)="EOF"
560 total=count-1
Program II

The procedure uses a READ statement, so we will also require DATA statements as in Program III.

1000 DATA FRED,545 27364,JANE,485 4849,JIM,475 9398,JACK,455 9969
Program III

It is possible to have as many DATA statements as required anywhere in the program. In the above example they start at line 1000 and the end of file is at line 5000. This means that more DATA statements can be inserted if required using line numbers between these.

Two EOF markers are used as the program reads two items of data each time it executes line 540.

Before running a program which contains an array it is necessary to reserve memory for the array. This is done with a DIMension statement. A program containing the above procedure would require a statement such as:-

10 DIM name$(25),number$(25)

This reserves 26 memory locations for names - name$(0) to name$(25) -and 26 locations for numbers. The procedure shown will not use location 0 but swopping lines 530 and 540 could cure this. The same effect can also be achieved if a variable is used in the DIMension statement, such as:

5 N=25
10 DIM name$(N),number$(N)

The above procedure and DATA statements will set up the two required arrays in the memory and also a variable (total) storing how many names and numbers are in the arrays. The arrays can be pictured as in Figure I.




545 27364




485 4849




475 9398




455 9989





Figure I

If we search through the array of names and find the required one, the corresponding telephone number will have the same subscript and can be accessed directly.

It should be noticed that a string array has been used to store the numbers. This means that they will be printed as stored with no corruption caused by the spaces as would be the case if a numeric array was used.

Now look at a procedure to search through the array of names. If the required one is found the corresponding number is output (Program IV).

700 DEF PROCsearch
710 count=0
730 count=count+l
740 IF requiredname$ = name$(count) THEN FOUND=TRUE ELSE FOUND=FALSE
750 UNTIL FOUND OR count=total
760 IF FOUND THEN PRINT requiredname$"'s phone numder is "number$(count) ELSE PRINT requiredname$" is not in the list"
Program IV

The two procedures (lines 500-570 and 700-770) together with DATA statements (lines 1000-5000) can now be put together and with a few extra lines (10-60, Program V) we have a far better telephone directory program.

Several improvements can still be made to this program, one simple one being a tidy up of the output. This is left as an exercise for the reader (use CLS and TAB).

10 DIM name$(l00).number$(100)
20 PROCinput
40 INPUT "Whose number is required" ,requiredname$
50 PROCsearch
60 UNTIL requiredname$="STOP'
Program V

Of the five faults found in the first program, the latter program corrects the first four and improves the last. The names are still contained in the program, but are now in DATA statements and so are quite easy to change. The cure for this is to store the data on tape or disc.

The arrays we have looked at so far are one dimensional, but it is possible to use more than one dimension.

In the above program we could replace the two one dimensional arrays (nameS(X) and number$(X) by one two dimensional array, which we could call info$(X,Y) where X will be the number of names and phone numbers and Y will be a 1 or 2 (1 corresponding to a name and 2 corresponding to a number).

The "picture" that you can use of a two-dimensional array is shown in Figure II.




545 27364




485 4849




475 9398




455 9989





Figure II

To use a two dimensional array in your program the following changes are required (Program VI).

10 DIM info$(100,2)
540 FOR N=lT0 2 :READ info$(count,N) :NEXT
550 UNTIL info$(count,l)="EOF"
740 IF requiredname$ = info$(count ,1) THEN FOUND=TRUE ELSE FOUND=FALSE
760 IF FOUND THEN PRINT requiredname$"'s phone number is "info$(count,2) ELSE PRINT requiredname$" is not in the list"
Program VI

There are advantages to both methods. The two one dimensional array method has the advantage that name$(20) obviously refers to the twentieth name and number$(20) to the twentieth number. This is much clearer than info$(20,l) and info$(20,2).

On the other hand a two dimensional array uses less memory, and if more items are stored - for example, address, date of birth, occupation - all that is required is to change the limits of the FOR . . . NEXT loop in line 540 and use a loop in line 760.

All the relevant information about a person whose name is typed in would then be printed out. But note that the DATA will also need corresponding changes.

Finally we present a program which prints out sentences, missing out a word (or group of words). Although the present data is nursery rhymes, other data can easily be substituted. The program uses three one dimensional arrays:


A random number is chosen and the corresponding elements of the first and last array are printed out, together with stars corresponding to each letter of the middle one.

If a word is typed in correctly, a congratulation message is chosen at random from the three messages in the one dimensional array CONGRAT$(NUMBER).

If an error is made the message is taken from the two dimensional array ERRMSG$(NUMBER1,NUMBER2), where NUMBER1 corresponds to the number of attempts at the question and NUMBER2 is chosen at random so that one of three different messages is printed out at random, making nine different messages possible.

To modify this program the following line numbers are easily changed:

780 ... the number of sentences in the data.

800 ... the title.

1000 onwards ... the sentences. These are entered in three parts with commas around the word(s) that you want missing out.

Note that if your sentence contains commas, quotes must be put around that part of the sentence.

820 to 960 ... the messages. REM statements show which are error and which are congratulations.