Lab: Histograms

Using Loops


Consider the following exercises.+

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.


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