This program will recreate the classic race of the tortoise and the hare. You\'l
ID: 3628385 • Letter: T
Question
This program will recreate the classic race of the tortoise and the hare. You'll use random number generation to develop a simulation of this memorable event.
Our contenders begin the race at "square 1" of 70 squares. Each square represems a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.
There is a clock that ricks once per second. With each tick of the clock, your program should use function moveTortoise and moveHare to adjust the position of the animals according to the rule in the information below. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and hare.
Animal Move type Percentage of the time Actual time
Tortoise fast plod 50% 3 squares to the right
Slip 20% 6 squares to the left
Slow plod 30% 1 square to rhe right
Hare sleep 20% No move at all
Big hop 20% 9 squares to the right
Big slip 10% 12 squares to the left
Small hop 30% 1 square to the right
Small hip 20% 2 squares to the left
Use variable to keep track of the positions of the animals (i.e., position numbers are 1-70). Start each animal at position 1 (i.e., the "starting gate"). If an animal slips lefr before square 1.
Generate the percentage in the preceding table by producing a random integer i in the range 1i 5, a slip when 6i7 or a "slow plod" when 8i10. Use a similar technique to move the hare. Begin the race by priting
Bang !!!!!
AND THEY ARE OFF !!!!!
For each tick the clock (i.e., each repetition of a loop), print a 70-posirion line showing the letter T in the toroise's position. Occasionally, the contenders land on the same square. In that case, the tortoise bites the hare and your program should print OUCH !!! begining at the position. All print positions other than the T, the Hor the OUCH!! (in case of a tie) should be blank.
After printing each line, test whether either animal has reached or passed square 70. If so, print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the same clock tick, you may want to favor the tortoise (the "underdog"), or you may want to print It's a ti e. If neither animal wins, perform the loop again to simulate the next tick of the clock.
Please add your sample run for this program. thank alot.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void movetort(int&);
void movehare(int&);
void print(int,int);
int main()
{int finish=70,tort=1,hare=1,rtime=0;
srand(time(0));
cout<<"ON YOUR MARK, GET SET BANG !!!!! AND THEY'RE OFF !!!!! ";
do
{movehare(hare);
movetort(tort);
print(tort,hare);
rtime++;
}while(tort<finish&&hare<finish);
if(tort>hare )
cout<<" TORTOISE WINS!!! YAY!!! ";
else if(tort<hare )
cout<<"Hare wins. Yuch. ";
else
cout<<"Would you believe IT'S A TIE!! ";
cout<<"time of race: "<<rtime<<" simulated seconds ";
system("pause");
return 0;
}
void print(int t,int h)
{if(h==t)
cout <<setw(h)<<"OUCH!!!";
else if(h<t)
cout<<setw(h)<<'H'<<setw(t-h)<<'T';
else
cout<<setw(t)<<'T'<<setw(h-t)<<'H';
cout<<endl;
}
void movehare(int& r )
{int num;
num=rand()%10;
if(num<2)
r-=2;
else if(num<5)
r++;
else if(num<6)
r-=12;
else if(num<8)
r+=9;
if(r< 1 )
r=1;
}
void movetort(int& t)
{int num;
num=rand()%10;
if(num<5)
t+=3;
else if(num<7)
t-= 6;
else
t++;
if(t<1)
t=1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.