frogwalk3.cc From Astrachan pp 327-328 --------------------------------------- #include #include #include "prompt.h" #include "walko.cc" #include "observer.h" // simulate two random walkers at once // Owen Astrachan, 6/29/96 // // uses a WalkObserver to monitor each of the random walkers WalkObserver::WalkObserver(const string & filename) { myOutputStream.open(filename); } void WalkObserver::Update(int step, int position) { myOutputStream << step << " " << position << endl; } int main() { int numSteps = PromptRange("enter # steps",0,1000000); WalkObserver frogObserver("frog.out"); WalkObserver toadObserver("toad.out"); RandomWalkO frog(frogObserver,numSteps); // define two random walkers RandomWalkO toad(toadObserver,numSteps); int samePadCount = 0; // # times at same location frog.First(); toad.First(); while (! frog.IsDone() && ! toad.IsDone()) { if (frog.GetPosition() == toad.GetPosition()) { samePadCount++; } frog.Next(); toad.Next(); } cout << "frog position = " << frog.GetPosition() << endl; cout << "toad position = " << toad.GetPosition() << endl; cout << "# times at same location = " << samePadCount << endl; return 0; }