Sinclair QL Beginner's Manual page 37

Hide thumbs Also See for QL:
Table of Contents

Advertisement

130 FOR number = 1 TO 4 : PRINT ! high_st$(number) !
140 DATA "VAL", "HAL", "MEL", "DEL"
Colons serve as end of statement symbols instead of ENTER and the ENTER symbols of lines 120
and 130 serve as END FOR statements.
There is an even shorter way of writing the above program. To print out the contents of the array
high_st$ we can replace line 130 by:
130 PRINT ! high_st$ !
This uses an array slicer which we will discuss later in chapter 13.
We have introduced the concept of an array of string variables so that the only numbers involved
would be the subscripts in each variable name. Arrays may be string or numeric and the following
examples illustrate the numeric array.
Program 1:
Simulate the throwing of a pair of dice four hundred times. Keep a record of the number of
occurrences of each possible score from 2 to 12.
100 REMark DICE1
110 LET two = 0
120 LET seven = 0:eight = 0:nine = 0:ten = 0 :eleven = 0:twelve = 0
130 FOR throw = 1 TO 400
140
LET die1 = RND(1 TO 6)
150
LET die2 = RND(1 TO 6)
160
LET score = die1 + die2
170
IF score = 2 THEN LET two = two + 1
180
IF score = 3 THEN LET three = three + 1
190
IF score = 4 THEN LET four = four + 1
200
IF score = 5 THEN LET five = five + 1
21O
IF score = 6 THEN LET six = six + 1
220
IF score = 7 THEN LET seven = seven + 1
230
IF score = 8 THEN LET eight = eight + 1
240
IF score = 9 THEN LET nine = nine + 1
250
IF score = 10 THEN LET ten = ten + 1
26O
IF score = 11 THEN LET eleven = eleven + 1
270
IF score = 12 THEN LET twelve = twelve + 1
280 END FOR throw
290 PRINT ! two ! three ! four ! five ! six
300 PRINT ! seven ! eight ! nine ! ten ! eleven ! twelve
In the above program we establish eleven simple variables to store the tally of the scores. If you plot
the tallies printed at the end you find that the bar chart is roughly triangular. The higher tallies are for
scores six, seven, eight and the lower tallies are for 2 and 12. As every dice player knows, the reflects
the frequency of the middle range of scores (six,seven,eight) and the rarity of twos or twelves.
100 REMark Dice2
110 DIM tally(12)
120 FOR throw = 1 TO 400
130 LET die_1 = RND(1 TO 6)
140 LET die_2 = RND(1 TO 6)
150 LET score = die_1 + die_2
160 LET tally(score) = tally(score) + 1
170 END FOR throw
180 FOR number = 2 TO 12 : PRINT tally(number)
In the first FOR loop, using throw, the subscript of the array variable is score. This means that the
correct array subscript is automatically chosen for an increase in the tally after each throw. You can
:three = 0:four = 0:five = 0:six = 0

Advertisement

Table of Contents
loading

Table of Contents