3.
•
4.
5.
6.
7.
8
9.
10.
•
•
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 two and
twelve.
As every dice player knows this 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
thraw
=1
TO
400
130
LET
die
1 =RND(1
TO
6)
140
LET
die-2
= RND(1
TO
6)
150
LET
scare
=
die
1 +
die
2
160
LET
taLLy(scare;
=
taLLY(scare)
+ 1
170
END
FOR
thraw
180
FOR
number
= 2
ta
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 think of the
array,
tally,
as
a set of pigeon-holes numbered 2
to
12. Each time a particular score occurs the tally of that score
is
increased
by
throwing
a stone into the corresponding pigeon-hole.
In
the second (short form) FOR loop the subscript
is
number.
As
the value of number
changes from 2
to
12
all
the values of the tallies are printed.
Notice that
in
the DIM statement for a numeric array you need only declare the number
of variables required. There
is
no question of maximum length as there
is
in
a string
array.
If
you have used other versions of
BASIC
you
may wonder what has happened
to
the
NEXT
statement
All
SuperBASIC structures end with END something.
That
is
consistent
and sensible but the NEXT statement has a part
to
playas you
Will
see
in
later chapters.
You
can score a maximum of
16
points from the following test Check your score with
the answers
on
page
109.
1.
Mention
two
difficulties which arise when the data needed
for
a program becomes
numerous and you try
to
handle
it
without
arrays.
(two points)
2.
If,
in
an
array,
ten
variables have the same name then how
do
you
know which
is
which?
What must you
do
normally
in
a program, before you can use
an
array variable?
What
IS
another word
for
the number which distinguishes a particular variable of
an array from the other variables which share
its
name?
Can you think of two Ideas
in
ordinary life which correspond
to
the concept of
an array
in
programming? (two points)
In
a REPeat loop, the process ends when some condition causes an EXIT
statement
to
be executed. What causes the process
in
a FOR loop
to
terminate?
A REPeal loop needs a name
so
that you can EXIT
to
its
END properly. A FOR
loop also has a name but what other function does a FOR loop's name
have?
What are the two phrases which are used
to
describe the variable which
is
also
the name of a FOR loop?
(two points)
The values of a loop variable change automatically
as
a FOR loop
is
executed.
Name one possible important use of these values.
Which of the following
do
the long form of REPeal loops and the long form of
FOR loops have
in
common? For each of the four Items either
say
that both have
it or which type of loop has
it
a An opening keyword or statement
b A closing keyword or statement
c A loop name.
d A loop variable or control variable (four points)
12/84
Arraysand
For
Loops
Program 2
SELF
TEST
ON
CHAPTER
6
31