UMBC CMSC201, Computer Science I, Fall 1994
Section 0101, 0102 and Honors
Project 5
Due: Wednesday, December 14, 1994
Objective:
The objective of this program is to practice working with arrays,
pointers and parameter passing by reference.
Assignment:
Your task is to write a function called ReportFrequency which
finds the numbers in a given integer array that occurs most often and
least often. The results of this function should be stored in
parameters that are passed by reference. The function prototype of
ReportFrequency is:
void ReportFrequency(int A[], int size, int *most_often, int *least_often) ;
To find the numbers which occur most often and least often, you should
use the following strategy.
- Dynamically create an auxiliary array of integers frequency
with size elements using the NewArrray function provided
in the genlib.h interface (see page 481 of the textbook).
- Initialize each entry of the frequency array to zero.
- For each element of the array A[i] find the smallest array
index k where the value stored in A[i] occurs.
Add one to frequency[k].
- Use frequency to find the numbers in the original array
which occurs most often and least often.
For example, if the array A contained the numbers: .
91, 93, 98, 92, 92, 95, 93, 92, 91, 95, 99, 92, 98.
Then, the frequency array would contain:
2, 2, 2, 4, 0, 2, 0, 0, 0, 0, 1, 0, 0.
This indicates that 91 occurred twice, 93 occurred 2 times, 98 occurred twice,
92 occurred 4 times, 95 occurred twice and 99 occurred once.
Write a main program that prompts the user to enter a list of non-negative
numbers terminated by the sentinel value -1. Your main program should
then call the ReportFrequency function and report the results.
Implementation Notes:
- As before, please put your name at the top
of your program and delete extra blank lines at the end of
your files.
- Your ReportFrequency function must have the prototype
shown above. This is a requirement.
- Please use the strategy outlined above. Do not use sorting.
This is a requirement.
- Break ties any way you want. If several numbers occur
just once, then you can report any of those as occurring
the least often. Note that the number that occurs least
often occurs at least once (i.e., no number in the array
occurs zero times).
- In your main program, you may assume that the user enters
no more than 100 non-negative numbers. However,in your function
ReportFrequency, you should not assume that there is an upper
limit on the number of elements in the array. You should use
the NewArray function to dynamically create an array.
What to turn in:
Make up 5 difficult sets of input to your program. These
inputs should show that your program can handle different kinds
of input, so the nastier they are, the better. For example,
they should have different sizes.
Use the script command to save a copy of your output for all of the
sample runs. To do this, remove any old typescript files you have,
type script, run your program to duplicate the sample runs then type
exit at the prompt. Use an editor (e.g., emacs) to check that the
file actually contains your output before you turn it in.
When you have successfully created the typescript file,
and have deleted all the blank lines at the end of your program,
submit your program using the submit201 command
that you copied for Project~1. To submit both your project and the
typescript file, issue the command:
% submit201 proj5.c typescript
Please check your own mail to see if you have submitted the correct files.
Last Modified:
Sun Dec 4 14:48:17 EST 1994
Richard Chang, chang@gl.umbc.edu