Asignment 2

advertisement
Spring 1, 2004
Algorithms
Name: __________________
ASSIGNMENT #2
[20 points]
Write a COMPLETE program that builds a frequency array for data values in the range of 1 to
20, and then prints their histogram. The data values are to be read in from a data file. The first
entry in the data file is the number of elements, and all the following numbers are the individual
elements. For example:
6
5
7
9
13
20
3
 6 elements in the file
 element #1
 element #6
Your program driver should be in a file named histogram.cpp. Your program MUST BE
MODULAR. You must contain at least one class named Histogram with appropriate methods
for keeping track of the historical data and producing the graph.
Ask the user for a file name and reads the file and stores the data into an array, dynamically
allocate the array based on the count in line 1 of the file. If the count in the file and the actual
number of elements differ, throw an error and stop the program. Also throw an error if the file
requested does not exist, but in that case, loop and ask for another file until a valid file name is
entered.
Then, prints the data in the array. Print the elements one at a time, with a space separating each
element. Example:
5 6 7 9 13 20
Finally, print a vertical histogram using asterisks for each occurrence of an element.
example, if their were 5 1’s and eight 2’s in the data, it would print:
1: *****
2: ********
3: ***
4: *****
5: *
6: *******
7: ***********
// etc.
Finally, give the user the option to repeat the program as many times as they like.
For
Spring 1, 2004
Algorithms
Name: __________________
A sample I/O session would look like thus:
Assignment: Histogram
Author
: James M. Hare
Date
: August 21st, 2000-08-21
Please enter a file name for the source data: test1.dat
*** ERROR *** I’m sorry, that file does not exist, please try again!
Please enter a file name for the source data: goodTest.dat
Thank you, that file will work just fine.
Your data in the file is as follows:
3 3 1 2 3 4 2 3 5 6 7 8 9 9 9 1 11 12 12 12 12 12 12 13 13 4 19 18 17
Which gives us the following histogram:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
**
**
****
**
*
*
*
*
***
*
*****
**
*
*
*
Would you like to print this histogram to file?
(Y/N) : N
Thank, you, would you like to try another file? (Y/N) : Y
Please enter a file name for the source data: test1.dat
//// etc. etc. etc.
Download