182
Some
Programming
Techniques
one
number,
which
increases
the
chances
that
the
common
letters
are
chosen
as
1
of
the
16
letters
in
the
grid.
Lines
60-110
randomly
generate
16
numbers
between
0
and
49.
In
line
100
the
chosen
element
of
array
N$
is
printed.
The
nested
loops
are
used
to
force
the
computer
to
print
four
letters
next
to
each
other
on
a
single
row.
The
PRINT
in
line
110
forces
the
computer
to print
the
next
four
letters
on
a
new
line.
The
next
example
uses
two
one-dimensional
arrays
to
sort
a
list
of
names
into
alphabetical
order.
Array
N$
contains
the
names
that
are
to
be
sorted.
Array
K
is
the
key
file
that
contains
the
numbers
used
to
produce
and
print
a
sorted
list
of
names.
This
sorting
technique,
which
is
called
a
bubble
sort,
uses
the
key
file
to
expedite
the sorting
procedure.
The
key
file
is
sorted
as
the
names
are
compared;
the
name
file
itself
is
not
actually
sorted.
As
soon
as
the
loop
in
lines
180-260
determines
which
of
the
two
current
names
is
first,
the
next
name
is
retrieved
from
the
N$
array.
Elements
of
array
K
are
sorted
in
line
240
when
name
N$(K(Y))
is
greater
than
the
next
name,
N$(K(Y+1)).
Line
300
prints
the
unsorted
array
N$
and
the
sorted
names,
which
are
accessed
in
the
sorted
order
by
using
the
sorted
key
file,
array
K.
5
DATA
JOE,JAYfA^EfJIM,DANrCATHERINE/SARAH,GLENN/GERRYfTERRYfBRUCEfJOHN
10
DATA
BOB,LIB,OLIVER,STELLA,MARCIA,DAVE,MARY
LOU,CATHY,JACK,END
15
REM
COUNT
HOW
MANY
NAMES
ARE
TO
BE
SORTED
20
READN$:IFN$="END"THEN
60
40
N=N+1
50
GOTO
20
60
RESTORE
70
REM
DIMENSION
ARRAYS
WITH
THE
NUMBER
OF
NAMES
TO
BE
SORTED
71
REM
N$
HOLDS
NAMES;
K
IS
A
KEY
FILE
THAT
ASSOCIATES
A
NUMBER
WITH
A
NAME
80
DIMN$(N),K(N)
90
REM
READ
NAMES
FROM
THE
DATA LIST
100
FORX=1TON
120
READN$(X)
130
REM
ASSIGN
ARRAY
N$
ELEMENT
NUMBER
TO
KEY
FILE
140
K(X)=X
160
NEXTX
180
FORX=NTO1STEP-1
200
FORY=1TOX-1
210
REM
SEE
WHICH
OF
TWO
NAMES
COMES FIRST
ALPHABETICALLY
220
IFN$(K(Y))<=N$(K(Y+1))THEN
260:REM
THEN
GO
GET
NEXT
NAME
230
REM
IF
THE
1ST
NAME
IS
>
THE
2ND,
ELEMENTS
IN
KEY
FILE
ARE
REVERSED
235
REM
THIS
IS
KEY
TO
SORT:
ONLY
KEY
FILE
IS
SORTED;
ITS
SORT
IS
BASED
ON
VALUES
TAKEN
FROM
ARRAY
N$
240
T=K(Y):K(Y)=K(Y+1):K(Y+1)=T
260
NEXTY,X
270
REM
PRINT
UNSORTED
AND
SORTED
LISTS
280
PRINTCHR$
(18)
"UNSORTED11,TAB
(10)
"SORTED"
290
REM
N$(X)
PRINTS
UNSORTED
NAMES;
KEY
FILE
IS
USED
TO
PRINT
SORTED
NAMES
300
FORX=1TON:PRINTN?(X),TAB(10)N$(K(X)):NEXT