INTRODUCTION TO PROGRAMMING IN C++

# Lab: Reporting Fish Movement Using Histograms

This lab is based on the two exercises on p. 8 of the AP Computer Science Marine Biology Case Study:+

• One can simulate the movement of a fish in an aquarium by repeatedly flipping a coin. When "heads" is flipped, the fish moves one foot to the right; when "tails" is flipped, it moves one foot to the left. What are the possible positions of the fish, relative to its starting position, at the end of a simulation with six coin flips? Explain. (Don't worry about the tank being too short.)

• Conduct several such simulations, each with six coin flips as described in the exercise above, and keep track of where the fish ends up after each simulation. Is the fish more likely to end up in one position than another? Explain why or why not.
In this lab you will write a program that will simulate a fish moving randomly back and forth six times, starting at location 0. Your program will print the final fish location (an integer between -6 and 6). You will then modify your program to run the simulation 1000 times, keeping track of how many times the fish ends up in each of the possible final fish locations. Finally, you will enhance your program to draw a histogram (bar graph) of the various fish locations. For example, a text-based histogram might look like the following:
```     -6   xxxxx
-4   xxxxxxxxx
-2   xxxxxxxxxxxxxx
0   xxxxxxxxxxxxxxxxx
2   xxxxxxxxxxxxxx
4   xxxxxxxxx
6   xxxxx
```

Simulate a fish moving six times.

• Download onedwalk.cpp from the College Board web site, compile it, and run it.

• In order to keep track of how many times the fish ends up in each location over the course of many runs of the simulation, create an integer variable to represent each of the possible final locations, e.g., minusSixCount, zeroCount, etc. Be sure to ininitialize each of them to 0. (Question: how many additional integer variables will you need?)

• Embed the original code from onedwalk.cpp in a loop, in order to run the simulation 1000 times. Remember that the fish needs to start at location 0 each time. After each run, increment the appropriate location counter rather than printing out the final location of the fish.

• After running the simulation 1000 times, print the number of times the fish ended up in each of the possible final locations. Run your program several times to test it. Do your results seem to make sense? You may wish to double-check that the various counts add up to 1000.

Draw a histogram.

• Copy the statements from your stand-alone aquarium program (or from ABhouse) that create and initialize a CMU graphics window* and paste them into your main function. Copy and paste the statements to set the pen and brush color and the call to WaitNClear also. Make sure that you have included the header file for WaitNClear in your main code file and that the implementation file will be compiled in as part of your program.

• For each of your final location counts, draw a rectangle whose width is proportional to the number of times the fish ended up in that final location. Look at your aquarium program (or ABhouse) to review how to draw a rectangle using the CMU graphics package.

The upper-left corner of the first rectangle (representing -6) should be at pixel location (100, 150). The upper-left corner of each of the other rectangles should be 10 pixels below the lower-left corner of the rectangle above it. The height of each rectangle should be 20. The width should be the number of times the fish ended up in that location. (You may use numeric constants for all of the parameters to DrawRectangle except the width. Or you may use variables and adjust the variable representing the rectangle top for each rectangle.)

• Run your program several times to test it. When it runs correctly, you may remove the cout statements that printed the various final location counters.

• If you like, make each bar in the histogram a different color. Test your program again. You may also wish to look at the demo program in the CMU graphics package to see how to write text to a graphics window, and then modify your own program to add text (such as the final location each bar represents, and the exact total for each location) to add clarity to your histogram.