Question: COS 160 : Program 7 - Histogram Objectives Working with loops and arrays. To encourage incremental development, we are requiring that in your solutions document
COS 160 : Program 7 - Histogram
Objectives
Working with loops and arrays.
To encourage incremental development, we are requiring that in your solutions document you show your code and result for each part!
Histogram
A histogram is a visual diagram showing the quantities of various categories by the lengths of bars. We are going to be creating data values based on a series of random dice rolls, and then displaying that data visually as a histogram.
Part 1 Statistics on Rolling Dice
To begin, write a program to roll a die 100 times and count how many times it comes up with each value from 1 to 6. Use Random to roll the die. Use a for loop to repeat it 100 times.
You must use an array to keep track of the counts of the number of rolls of each value. To make things easier, use an array of size 7 and just don't use index 0. At index 1 count the number of 1 rolls. At index 2 count the 2 rolls and so forth.
Print the final contents of your array to make sure it is working correctly. On average you would expect about 16 or 17 rolls for each value, but because of random variations, the numbers will usually vary by a few rolls. Run it multiple times and you should see different results each time.
In your solutions doocument show your code and its output.
Part 2 The Histogram
Now modify your printing code to print your results like the example shown below. Print the roll value in brackets followed by tab character "\t". Then print 1 asterix for each roll of that value, and then finish the line by printing the count.
[1] ******************* 19 [2] ************ 12 [3] ********************* 21 [4] ******************** 20 [5] ************* 13 [6] *************** 15
In your solutions doocument show your code and its output.
Part 3 Sum of 2 dice
Now modify your code to roll two dice and add them together. Count the sums, which can now range from 2 to 12. You will need a larger array. Your histogram should now look similar to this:
[2] ** 2 [3] ***** 5 [4] ******** 8 [5] *********** 11 [6] ********** 10 [7] **************** 16 [8] ******************** 20 [9] *********** 11 [10] ********* 9 [11] ***** 5 [12] *** 3
Normally we would expect 7 to be the most common, but in this run there were more 8's.
In your solutions doocument show your code and its output.
Part 4 Scaling
Now modify your code to roll the dice 30,000 times. To make the histogram readable you will print 1 star for each 100 rolls matching that value. Your histogram should now look similar to this:
[2] ********* 852 [3] ***************** 1715 [4] ************************* 2462 [5] ********************************* 3329 [6] ***************************************** 4099 [7] ************************************************** 5023 [8] ****************************************** 4172 [9] ********************************* 3326 [10] ************************* 2518 [11] ***************** 1661 [12] ******** 843
Round your calculations to the nearest number of stars. In the run above, [2] was rolled 852 times, which should be 8.52 stars. This rounded up to 9 stars. [12] was rolled 843 times, which rounded down to 8 stars.
In your solutions doocument show your code and its output.
Part 5 Sum of 3 dice
Now modify your code to roll three dice and add them together. Still do 30,000 rolls.
In your solutions doocument show your code and its output.
Extra Credit (5 points) Auto-scaling
Add code to ask the user how many times to roll, and then after all the rolls have been counted, figure out which was the most frequent roll and calculate a scaling factor for the histogram so that the longest bar is 50 stars. Also print the scaling facor.
In your solutions doocument show your code and its output when run at 1000000 rolls.
Part 6 Any number of dice
Add code to also ask the user how many dice to roll. This could be any number 1, 2, 3, 4 ... You will need to calculate how big to make your array before you declare it.
In your solutions doocument show your code and its output when run with 5 dice and 1000000 rolls.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
