Page 420 - Introduction to Programming with Java: A Problem Solving Approach
P. 420

                386 Chapter 10 Arrays and ArrayLists
three heads), print a series of *’s where the number of these asterisks is proportional to the number of times the case occurred. Each series of asterisks represents a histogram bar. That should make more sense by looking at this sample output:
Number of times each head count occurred:
0 124960 **************
1 375127 ******************************************
2 375261 ******************************************
3 124652 **************
Note the first row of asterisks. That’s a horizontal “bar” that pictorially describes the number of times that the zero-heads case occurred. The zero at the left is the label for the zero-heads case. The 124960 is the spe- cific number of times that the zero-heads case occurred. Or said another way, 124960 is the frequency of the zero-heads case. Note that the zero-heads and three-heads frequencies (124960 and 124652, respectively) are nearly the same, and the one-head and two-heads frequencies (375127 and 375261, respectively) are also
nearly the same. Also note that the zero-heads and three-heads frequencies are each approxi- mately one third of the one-head and two-heads frequencies. It’s always a good idea to use some kind of independent calculation to predict what a computer’s answer should be like. For this simple problem, it’s relatively easy to compute an exact answer. Assuming that “T” means “tails” and “H” means “heads,” here are all the possible flipping results:
 Compare program results with predicted results.
  TTT (0 heads) TTH (1 head) THT (1 head) THH (2 heads) HTT (1 head) HTH (2 heads) HHT (2 heads) HHH (3 heads)
Apago PDF Enhancer
Note that there is only one way to obtain zero heads and only one way to obtain three heads, but there are three ways to obtain one head and three ways to obtain two heads. So the zero-head and three-head frequencies should each be one third of the one-head or two-head frequency. If you look at the numbers and bar lengths in the above sample output, you’ll see that the computer result does indeed conform to this expectation.
See Figure 10.9’s CoinFlips program. It does what we want. It simulates flipping three coins a million times, and it prints the simulation results in the form of a histogram. It uses a four-element frequency ar- ray to keep track of the number of times each head-count value occurs. Each element in the frequency array is called a bin. In general, a bin contains the number of occurrences of an event. For the CoinFlips program, the frequency[0] element is the first bin, and it holds the number of times none of the three coins lands heads up. The frequency[1] element is the second bin, and it holds the number of times one of the three coins lands heads up. After each three-coin-flip simulation iteration, the program adds one to the appropriate bin. For example, if a particular iteration generates one head, the program increments the frequency[1] bin. And if a particular iteration generates two heads, the program increments the frequency[2] bin.
Let’s now examine how the CoinFlips program prints the histogram asterisk bars. As specified by the second callout in Figure 10.9, the second large for loop prints the histogram. Each iteration of the for loop prints the bin label (0, 1, 2, or 3) and then the frequency for that bin. Then it computes the number of asterisks to print by dividing the frequency in the current bin by the total number of repetitions and multi- plying by 100. Then it uses an inner for loop to display the computed number of asterisks.




















































































   418   419   420   421   422