Lab: Histograms

Using Loops


Last week, two of the discussion questions were:+

In this lab you will write a program that will simulate a fish (or other object) moving randomly back and forth six times, starting at location 0. Initially your program will print the final location of the object (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 object ends up in each of the possible final locations. Finally, you will enhance your program to draw a histogram (bar graph) of the various final 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 an object moving six times.

Add multiple runs.

Draw a histogram.

Submit your modifications.

If you have time...


After you have completed this lab and the More Fish! mini-lab, you can start work on Programming Project #3 (due at the beginning of Lab 4).


+These questions came from the Advanced Placement Computer Science Marine Biology Simulation Case Study, available from the College Board.