INTRODUCTION TO
PROGRAMMING IN
C++
Kalamazoo
College
LAB:
REPORTING
FISH
MOVEMENT
USING
HISTOGRAMS
Last week, two of the discussion questions were:+
- 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.
- Copy the Histogram Lab folder from my CS 420 folder on Dragon
(under Network Neighborhood) to the Temp folder.
Open the histogram project and edit main.cpp.
- Construct a coin using the default
Coin
constructor. Also construct an integer variable, initialized to 0,
representing the fish location.
- Research the Coin interface
to learn how to toss a coin and how to determine whether the tossed
coin is showing heads or tails. Modify your program to toss the coin
six times. Each time you toss the coin, move the fish to the right if
the coin comes up heads, and to the left if the coin comes up tails.
- After moving the fish six times, print the final location of the
fish. Run your program several times to test it.
Add multiple runs.
- 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 code you wrote earlier 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.
- 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 or
display.cpp) 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.
Print and save your modifications.
- Print your main function.
Remove the object code from the project (there is an option under the
Project Menu to do this).
Copy the Histogram Lab folder
from the Temp folder to your floppy disk. You should then remove the
folder from the Temp folder on the C: drive.
- Be sure to turn in the printout of your main function.
+These questions came from the
Advanced Placement Computer Science Marine
Biology Simulation Case Study,
available from the College Board for face-to-face teaching purposes.
*The CMU Graphics Library was created by the Carnegie Mellon
University School of Computer Science and is used in their introductory
programming courses. It is available from
Mark Stehlik's
Advanced Placement page.