We can now write the framework for a subroutine to deal a card:
10 DIM PACK(52)
20 FOR X = 1 TO 52
30 LET PACK(X)=0
40 NEXT X
. . . . . . . . . . . .
1 0 0 0 C A R D = I N T ( R N D * 5 2 ) + 1
1 0 1 0 I F P A C K ( C A R D ) = 1 T H E N G O T O 1 0 0 0
1020 PACK(CARD) = 1
1030 RETURN
Note that the DIM statement is the first line in the program . This is because an array can only be
dimensioned once - it cannot be redimensioned further on in the program.
Lines 20 to 40 set the elements of the array to zero. The subroutine that starts at line 1000 then
chooses a card at random and checks that it has not been already dealt. If it has, then another is
chosen until one is found that is still in the deck
.
The routine then changes the value of the
appropriate element in the array to signify that the card has now been dealt and returns from the
subroutine.
Array handling is not restricted to single dimensions but can be extended to any number of
dimensions desired. This is achieved by simply adding further reference numbers to the variable. For
example a 10x10x10 array (or matrix) can be set up by the command:
D I M A R R A Y ( 1 0 , 1 0 , 1 0 )
This technique is useful for dividing data into smaller subsets within a large group.
In our example we could split the pack into four suits of thirteen cards which can then be accessed
separately by using the format:
DIM PACK (4,13)
Now
if we wish to find the four of clubs,which might have been element 43 in our original array, we
simply have to examine element (2,4) - assuming that clubs are the second ‘row’ in our new array.
Arrays do not have to be used to store numerical data but can be used to handle strings as well
.
An
application for this might be to record the names of people booked into seats at a theatre or on an
aeroplane flight.
4.12 DATA
This command, in conjunction with the command READ, can be used to input data into a program.
The required data is listed in a line with each item separated by a comma and the whole list
preceeded by the DATA command. The data can now be accessed sequentially using the READ
command.
Summary of Contents for CPC464
Page 1: ......
Page 102: ...PRINT 10 AND 12 Results in 8 PRINT 10 AND 1000 Results in 8 again ...
Page 118: ...TO DO DRAW THIS PAGE ...
Page 219: ...actual value may be made to vary during the execution of a program ...
Page 240: ...Text and WINDOW planner Mode 2 80 Columns ...
Page 241: ......