Only one DIM statement is needed to set up the size of both arrays, line 60. The
loop from 170 to 210 examines each name in the array to see if it is the one
required.
It is also possible to use integer arrays. In the previous program all the ages
were integers, and could have been stored in an array age%( ).
Files
The last section showed how you can store data in arrays. One weakness of this
storage method is that it wastes computer memory. Every data item is stored
twice: once as part of the DATA statements within the program, and again
elsewhere in memory when the computer copies each data item into the array.
A more sensible method is to store the data completely separately from the
program, as a data file. The file can be saved onto disc (in a similar manner to
a program) and can be loaded back when required.
This program creates a file of names and telephone numbers:
10 MODE 135
20PR0Ctake_names
30 PR0Cmake_file
40 END
50 DEFPR0Ctake_names
60 DIM name$(100), tele$(100)
70 PRINT' "PLease type in the names and"
80 PRINT"te0ephone numbers of your friends."
90 PRINT"You can end by typing XXX when you"
100 PRINT"are asked for a name. "''
110 count=0
120 REPEAT
130 count=count+1
140 INPUT "Name",name$(count)
150 IF name$(count)<>"XXX" THEN INPUT "Telephone number",tele$(count)
160 UNTIL name$(count)="XXX" OR count=100
170 ENDPROC
180 DEFPROCmake_file
190 CLS
200 PRINT'"What name do you want to give to"
210 INPUT"your data file",file$
220 this_one=0PEN0UT(file$)
230 IF name$(count)="XXX" THEN count-count-1
240 F0Rnumber=1 TO count-1
250 PRINT#this_one,name$(number) ,tele$(number)
260 NEXT number
C54