CSC 222: Computer Programming II
Spring 2004

HW2: Simulation using a Class


In this assignment, you will utilize the MarbleJar class (see MarbleJar.h and MarbleJar.cpp) in order to simulate series of marble drawings. In particular, your simulations will allow you to collect data and hopefully solve the following puzzle:

Suppose a jar has an arbitrary mixture of black and white marbles. As long as there are at least two marbles in the jar, draw two random marbles and Is it possible that the marble drawing process go on forever? Furthermore, if you are told the initial contents of the jar, can you predict the final outcome?

Write a C++ program named puzzle.cpp that will help you solve the puzzle. Your program should prompt the user for the initial contents of the jar and create a corresponding MarbleJar object. It should then repeatedly draw two marbles from the jar and perform the appropriate action based on the colors of the drawn marbles. If the process ends (with less than two marbles in the jar), then the final contents of the jar should be displayed. For example:

How many black marbles in the jar? 4 How many white marbles in the jar? 5 1: I drew black and white -- replacing a white marble. 2: I drew white and white -- replacing a black marble. 3: I drew white and black -- replacing a white marble. 4: I drew white and black -- replacing a white marble. 5: I drew white and black -- replacing a white marble. 6: I drew white and white -- replacing a black marble. 7: I drew black and black -- replacing a black marble. 8: I drew black and white -- replacing a white marble. 9: Only one marble left... it is white.

ANALYSIS: Once you have your program working, answer the following questions:

  1. Perhaps using repeated simulations as a guide, what is the answer to this puzzle? Can the process of drawing and replacing marbles go on forever? Why or why not?
  2. If you know the initial contents of the jar, can you predict the final outcome? Explain.