CSCI 1300 - Program for Homework 11
The purpose of this homework is to help you and me gauge your skill
in writing small programs that involve arrays. It is simpler than the
other recent assignments, but I need you to do it entirely on your
own (with help from instructors only). Think of it like a take-home exam.
Due date: midnight on Friday, Nov 22. However, please plan ahead and don't
leave things until the last moment. To encourage this future planning:
None of the instructors will offer help by e-mail or otherwise on
Friday, Nov 22. Any problems that you run into on that day must
be handled by yourself. So, try to complete this early so we can
help with any problems.
Grading
This assignment will be graded automatically by running the
program and comparing the output with the correct output.
Because of the automated grading, the output of your program must match the
specification exactly. You must not add any extra features that will
confuse the grader. For example, the program must not print
directions to the user unless such directions are required by the
specification.
PROGRAM SPECIFICATION
The program is an interactive program
that reads input from the keyboard. Several instructions (listed below) may be
entered by the user. For each input, the program will give a short
response printed to cout. Each response to cout is followed by a
single endl.
One warning: The program must not try to store all the numbers in a
single big array.
In fact, I forbid you from having any array that is
larger than 100 elements.
Instead, you should create a frequency array
which has indexes from [0] to [99]. A location, such as
frequency[i] tells you how many times the number i has occurred
in the input. This frequency array should be initialized to all
zeros, and each time a number is read, the program will add one
to the corresponding array location. For example, frequency[42]
begins at zero. When one 42 is read, frequency[42] is increased
to one. If another 42 is read, frequency[42] is increased to two,
and so on.
All of the statistics that you need to calculate can be computed
based on the frequency array (so that you don't need to store all
those numbers separately).
-
Input: The letter N followed by an integer in the range 0 to 99.
The biggest possible input number is 99. The specification does not
care what the program does for numbers that are outside of the legal range.
The program must work correctly up 20,000,000 input numbers.
Output: The word "OK".
- Input: The letter S.
Output: The output is the SUM of all the input numbers read
so far. This output is 0 if there has not yet been any numbers read
by the program.
- Input: The letter A.
Output: The output is the AVERAGE of all the input numbers
read so far, calculated as a double number. For example, if there have
been four input numbers 2, 2, 6 and 5, then the average is 3.75.
If there has not yet been any input numbers, then the program should
print the word "ERROR" instead of an average.
- Input: The letter B.
Output: The output is the BIGGEST of all the input numbers
read so far, For example, if there have
been four input numbers 2, 2, 6 and 5, then the biggest is 6.
If there has not yet been any input numbers, then the program should
print the word "ERROR" instead of a number.
- Input: The letter F.
Output: The output is the most FREQUENT of all the input numbers
read so far--in other words, the number that has been read most often
(also called the "mode").
If there are several numbers that are equally often, then the smallest
number is printed. For example, if there have
been four input numbers 2, 2, 6 and 5, then the most frequent is 2.
If there has not yet been any input numbers, then the program should
print the word "ERROR" instead.
- Input: The letter H.
Output: The output is a count of HOW MANY numbers
have been input so far. This could be as small as zero.
- Input: The letter M.
Output: The output is the MEDIAN of all the input numbers
read so far. The median is a number that has no more than half of the
input numbers below it and no more than half the input numbers above
it.
For example, if there have
been five input numbers 2, 2, 3, 6 and 5, then the median is 3 since
two of the five numbers are below 3 and two of the five numbers are
above 3. Notice that if there has been an even number of inputs (such
as 2, 3, 6, 5) then there may be several choices for the median (in
this example, the median could be either 3 or 5). In this case, Dora
does not care whether the output is the 3 or the 5.
If there has not yet been any input numbers, then the program should
print the word "ERROR" instead of the median.
- Input: The letter Q.
Output: The program outputs the word "END" and then stops.
SAMPLE SESSION WITH THE PROGRAM: User input in the session is
written in italic. Program output is written in bold:
N 2
OK
N 2
OK
N 6
OK
N 5
OK
H
4
S
15
A
3.75
F
2
N 3
OK
M
3
Q
END
HINTS:
Read the command characters with an input statement like this (where
command
is a char
variable):
cin >> command;
Use a switch statement to control the action of the program based on
the command character that has been read. If the command character was
an N, then read the number with an input statement like this (where
number
is an integer variable):
cin >> number;
Don't forget the break
statement at the end of each case.