L.A.15.1 - frogs

LAB EXERCISE

 

FROGS

 

 

Background:

 

Random motion is an important process to model using computers.  The scientific phenomena known as Brownian motion, describes the random motion of molecules in a gas or liquid state.  Such random simulations are also used to build mathematical models that predict stock market prices.  This lab exercise will present a non-class-based version of a one-dimensional walk, and then instruct you to convert it to a version which uses a class.  Consider the following program.

 

// frogwalk.cpp

 

#include <iostream.h>

#include <dice.h>

 

// simulates a one-dimensional random walk

// This lab idea is credited to Owen Astrachan, Duke University

// A Computer Science Tapestry, McGraw-Hill, ©1997, p.311

 

main()

{

       int numSteps, k;

       int position = 0;   // frog starts at position 0

       dice die(2);                            // used for coin flipping

      

       cout << "Enter # of steps ---> ";

       cin >> numSteps;

       for (k=0; k < numSteps; k++)

       {

             if (die.roll() == 1)

                   position--;                                                 // step to the left

             else

                   position++;                                               // step to the right

             int blanks = 40 + position;                          // a pictoral view of the walk

             for (int j=0; j<blanks; j++)

                   cout << " ";

             cout << "*" << endl;

 

       }

       cout << "final position = " << position << endl;

       return 0;

}

 

When the program is run, the asterisk (*) marking the position of the frog will start at the middle (column 40) and wander all over the screen.  The program works fine, but tracking the walk of more than one frog would be difficult and a two-dimensional walk would be especially difficult.  Your task is to design and code a frog class to simulate one-dimensional random motion problems.

 


Assignment:

 

1.    You are to solve three different problems regarding a frog taking a one-dimensional walk.  Assume that a frog can only take one step to the right or the left (x-axis movement only).  After reading the description for all three problems, design and implement a frog class to be used in three different client programs.

 

2.    Problem 1.  Write a program to solve the same problem as the example program above (frogwalk.cpp).  The program should prompt the user for the number of steps to take, then print out the final position of the frog.

 

3.    Problem 2.  Write a program which prompts the user for a distance from the origin.  The frog must walk until it attains that distance from the origin in either the negative or positive direction.  Output the final position and total number of steps the frog took.  The client program should not keep track of the number of steps for the frog.  The frog object should keep track of its own number of steps.  This is identical to the strategy of having a dice object keep track of the number of times it has been rolled.

 

4.    Problem 3.  Write a program to track the movement of 2 frogs, both of which start at position 0.  Prompt the user for the number of steps for both frogs and have the frogs take a walk.  Print the final positions of both frogs and the number of times the two frogs occupy the same position.

 

 

Instructions:

 

1.    Complete the frog class.  Implement it as a two file system, frog.h containing the class interface, and frog.cpp containing the implementation.

 

2.    Complete each problem and turn in 3 different source codes, with the run output appended at the bottom of each source code.

 

3.    You must also turn in a copy of frog.h and frog.cpp.  Cut and paste both files together and turn them in on one piece of paper.