Experiment
#6
Computing the Median
of
the First N Values
The previous program will be changed so that only the first N values will be read in
from the RAM file, instead
of
all 30. The median of these N values will then be
computed and displayed.
You will be able
to
input the value for N when the program
is
executed. The main
purpose of this experiment is to generalize the median calculation, and show you how
to
determine whether there are an even or an odd number
of
values.
Make the following changes
to
the program:
20
INPUT
"NUMBER
OF
DAYS
(2
-
30)";
N
30
FOR
I = 1
TO
N
550
N1=INT«N+1)/2)
:
N2=INT~(N+2)/2)
560
MO
= (S
(N
1) + S (N2» / 2
570
PRINT
"MEDIAN
IS";
MO
List the program
to
verify that it
is:
5
OPEN
"RAM:SALEOA.OO"
FOR
INPUT
AS
1
10
DIM
S(30)
20
INPUT
"NUMBER
OF
DAYS
(2
-
30)";
N
30
FOR
I = 1
TO
30
L10
INPUT
# 1 t
S(I)
50
NEXT
I
500
FOR
I = 1
TO
N - 1
510
FOR
J = 1
TO
N - I
520
IF
S(J)
<
S(J
+
1)
GOTO
5L10
530
S=S(J)
:
S(J)=S(J+l)
:
S(J+l)=S
5L10
NEXT
J :
NEXT
I
550
N1=INT«N+1>/2)
:
N2=INT«N+2)/2)
560
MO
=
(S
(N
1) + S (N2» / 2
570
PRINT
"MEDIAN
IS";
MO
Execute the program.
This time you will be prompted for the number
of
days of sales
to
be read. After you
enter a value, the program will read the first N values from the
RAM
file and display
the median.
Here is an example of the execution and output of the program:
NUMBER
OF
DAYS
(2
-
30)?
20
MEDIAN
IS
285
Thus the median of the first 20 sales is 285.
Execute the program several times. Try entering both even
and
odd values for
N.
You
will find that the program will correctly calculate the median
in
every case.
Line
20
An
INPUT statement has been added which prompts you to enter the value
for N.
139