In this assignment, your last assignment this semester, you will write one program that uses arrays. This program must be turned in by 4:00 PM on Thursday, 10 December. No late assignments will be accepted.
In this program you will test the random number generator of C++. You do this by generating a large uniformally distributed sequence of integers between 0 and 19 and recording the frequency with which each of these numbers occurs. You will use a histogram stored in an array of twenty integers to accomplish this task.
Let's assume you have declared H to be your histogram
array.
The top-level design of your program should consist of
a while
loop that does
something like:
for
loop that:
for
loop from 0 to 19 that:
Here's a sample run of a program that solves this problem:
Enter number of randoms to generate 2000 0: 83 ************ 1: 93 ************** 2: 91 ************** 3: 110 ***************** 4: 107 **************** 5: 97 *************** 6: 107 **************** 7: 99 *************** 8: 91 ************** 9: 98 *************** 10: 98 *************** 11: 87 ************* 12: 113 ***************** 13: 108 **************** 14: 105 **************** 15: 94 ************** 16: 110 ***************** 17: 104 **************** 18: 103 *************** 19: 102 *************** Enter number of randoms to generate 0
You may find the generation of those pretty output lines to be a challenge.
First of all, you will need to use output formatting operations to make sure that your columns are lined up evenly. The output lines in the sample program were generated by a C++ statement that starts with
cout << setw(2) << i << ":" << setw(7) << H[i]
for
loop to generate
the right number of asterisks in your output.
The tricky part is figuring out how many asterisks to
generate. The sample program contains the following line:
m = ( H[i] * 300 + n/2) / n ;to do this computation. In the formula above,
n
is the
total number of random numbers that were generated, and m
is the number of asterisks to be printed for H[i]
.
Below is a skeleton for the program that you need to write. This skeleton contains the complete segment of code needed to print the histogram.
int main()
{
while ( ) {
srand((unsigned int) time(0)) ; //seed the random number generator
for (i=0; i<20; ++i) { // loop used to print the histogram
cout << setw(2) << i
<< ":" << setw(7) << H[i] << " ";
m = ( H[i] * 300 + n/2) / n ; // number of *'s to be printed
for (j=0; j<m; ++j)
cout << '*' ;
cout << endl ; // end of loop used to print the histogram
}
} // end of while loop
return 0 ;
} // end of main
You should turn in a floppy disk containing the source code for the program described above either on the last day of class before the final exam or in Robinson 221 before 4:00 PM on Thursday, 10 December. The disk should be labeled (on the outside) with your name and section number. The disk should contain either a cleaned workspace for this assignment or you can turn in just the C++ source code described above. Either format will be accepted.