C++ Pointers Lab Have you heard or read the story of The Tortoise and the Hare?
ID: 3703440 • Letter: C
Question
C++ Pointers Lab
Have you heard or read the story of The Tortoise and the Hare?
You can watch Walt Disney’s rendition here:
https://youtu.be/
MeZe2qPLPh0
or you can read the story here:
http://read.gov/
aesop/025.html
.
Now that you are familiar with the story, let’s code an algorithm
for a simulation!
(Simulation: The Tortoise and the Hare) In this exercise, you’ll re-
create 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 represents 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 ticks 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
following rules:
These functions should use pointer-based
pass-by-reference to modify the position of
the tortoise and the hare.
Use variables 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 left
before square 1, move the animal back to
square 1.
Generate the percentages in the preceding
table by producing a random integer i in
the range 1
?
i
?
10. For the tortoise, perform
a “fast plod” when 1
?
i
?
5, a “slip” when 6
?
i
?
7 or a “slow plod” when 8
?
i <10. Use
a similar technique to move the hare.
Begin the race by displaying
BANG !!!!!
AND THEY’RE OFF !!!!!
For each tick of the clock (i.e., each
repetition of a loop), display a 70-position
line showing the letter T in the tortoise’s
position and the letter H in the hare’s
position. Occasionally, the contenders land
on the same square. In this case, the
tortoise bites the hare and your program
should display OUCH!!! beginning at that
position. All positions other than the T, the
H or the OUCH!!! (in case of a tie) should
be blank.
After displaying each line, test whether
either animal has reached or passed
square 70. If so, display the winner and
terminate the simulation. If the tortoise
wins, display TORTOISE WINS!!! YAY!!! If
the hare wins, display 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 display It’s
a tie. If neither animal wins, perform the
loop again to simulate the next tick of the
clock.
To the right is a screenshot example of
what your output may look like
Explanation / Answer
here is your program : ----------->>>>>>>>>
#include<iostream>
#include<cstdlib>
#include <chrono>
#include <thread>
#include<ctime>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
void move(int *m){
int i = rand()%10 + 1;
if(i <= 5){
*m = *m + 2;
}else if(i <= 7){
*m = *m - 1;
if(*m < 0){
*m = 0;
}
}else if(*m <= 10){
*m = *m + 1;
}
}
void print(int T,int H){
system("cls");
int st = 1;
if(T >= 70 && H >= 70){
cout<<" The Game is tie";
st = 0;
}else if(T >= 70){
cout<<" Tortoise Win The Game ";
st = 0;
T = 69;
}else if(H >= 70){
cout<<" Hare win the game ";
st = 0;
H = 69;
}
int i;
cout<<" O - OUCH!!! T - TORTOISE H - HARE Start";
cout<<" +";
for(i = 0;i<25;i++){
cout<<"---+";
}
cout<<" |";
for(i = 0;i<25;i++){
if(H == i && T == i){
if(H == 0){
cout<<"H T|";
}else{
cout<<" O |";
}
}else if(H == i){
cout<<" H |";
}else if(T == i){
cout<<" T |";
}else{
cout<<" |";
}
}
cout<<" +";
for(i = 25;i<50;i++){
cout<<"---+";
}
cout<<" +";
for(i = 0;i<25;i++){
cout<<"---+";
}
cout<<" |";
for(i = 25;i<50;i++){
if(H == i && T == i){
cout<<" O |";
}else if(H == i){
cout<<" H |";
}else if(T == i){
cout<<" T |";
}else{
cout<<" |";
}
}
cout<<" +";
for(i = 0;i<25;i++){
cout<<"---+";
}
cout<<" +";
for(i = 50;i<70;i++){
cout<<"---+";
}
cout<<" |";
for(i = 50;i<70;i++){
if(H == i && T == i){
cout<<" O |";
}else if(H == i){
cout<<" H |";
}else if(T == i){
cout<<" T |";
}else{
cout<<" |";
}
}
cout<<" +";
for(i = 50;i<70;i++){
cout<<"---+";
}
cout<<" finish";
cout<<" ";
if(st == 0){
exit(0);
}
}
int main(){
int T = 0;
int H = 0;
print(T,H);
srand(time(0));
while(true){
sleep_for(nanoseconds(20000));
move(&T);
move(&H);
print(T,H);
sleep_for(nanoseconds(200000));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.