Sharp MZ-80A Owner's Manual page 91

Hide thumbs Also See for MZ-80A:
Table of Contents

Advertisement

101
Notice that the problem has been divided neatly into a main
program
and two subroutines. This is
one of
the basic
principles
of structured
programming suggested by Dijsktra.
Now.
proceed
to the next step.
Since we
are
to
fmd 1000
prime numbers before printing them,
we need to
store
them
somehow
in a
storage
location. The
next step is to
determine the method of
storing
the
prime numbers.
It would be
unwise to declare a numeric array
of 1000 elements simply because 1000 prime numbers
must be
memorized. It
is possible
to
define a
string
array and place T's (true) in
locations
in the string designated by subscripts
which
happen
to
be prime numbers and
F's (false) in other locations. Another
possible method is to record the prime
numbers in a data file on
cassette tape.
It
is easy to identify
prime
numbers
if
they
are
stored in a string array and
identified
by the
characters
"T"
and
"F";
and it is possible to store
prime
numbers for a long
time
if
they
are
recorded
on
cassette tape. What method
should
be
used?
For an
algorithm in which unprocessed numbers are divided by the
prime
numbers already found
to
identify
prime
numbers, the
first method
is most
appropriate;
that
is, to
prepare a numeric array of 1000
elements. Accordingly,
we
declare a
numeric array
of 1000 elements
at the beginning
of the
main
program. DIM
"Numeric array
of 1000 ele¬
ments".
DIM
"Numeric array
of 1000 elements"
Next, the
structure
of the array
must be
determined. We
can use PRIM as the
array
name
(though the array name is
identified
only by the
first
two
characters) and
use a
2-dimensional numeric array. This is
because,
since the maximum
subscript value
for
1
-dimensional arrays is 255,
it is not possible to identify
all
array
elements
with
a 1-dimensional
array. Since all array elements must be identified within the subscript range of 255, we
use an
array structure such that
the 1000 elements are grouped into 10
subarrays
of 100 elements each.
The
DIM statement
for the required array is as
follows:
DIM PRIM
(9
.
99)
With this decided, we
can
go on to determine the format for display of the 1000 prime numbers.
There
are
many ways
of
outputting
the results, such
as
displaying them
on the CRT
display unit
or
printing
them
on the printer.
As
for
format,
one
prime number may be printed
on one line or
they
may
be printed in
a tabular form;
and
so on. We will use
the simplest
format; that
is,
sequentially
printing
the numbers on the CRT
screen
without
formatting them. The following subroutine ("Print all numbers found") will be adequate
for this purpose:
FOR
M
=
0 TO 9 : FOR N = 0 TO 99
PRINT PRIM
(M
.
N)
:
NEXT N .
M
Now
we have finished that
main program and the subroutine "Print
all numbers found." The
remaining
task
is to
formulate the subroutine "Find 1000
prime
numbers." Since we are
going to
use
the array described above, it is natural
to
find the
prime numbers sequentially, starting
with the smallest one, and
to place
them
into the array in increasing
order of its subscripts.
Assuming
that we have
a subroutine "Find the next prime number" which, given
parameter I,
examines I +
1,1 +
2,
....
insequence, places the
first
prime number
found
into
I,
and returns control to the calling program, we can form
the subroutine "Find
1000
prime
numbers" as follows:
1-1
FOR M =0 TO 9
:
FOR N=0 TO 99
GOSUB
"Find the
next
prime
number"
PRIM <M
.
N)«-I
NEXT N . M
RETURN

Advertisement

Table of Contents
loading

Table of Contents