I\'m pretty mediocre at programming, so help with writing this C Program would b
ID: 3851090 • Letter: I
Question
I'm pretty mediocre at programming, so help with writing this C Program would be great!! Thanks!!
In this problem, you’ll recreate one of the truly great moments of our very own childhood tale, namely 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’s a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according to the rules as follows:
Animal Move type Percentage of the time Actual move
Tortoise: Fast plod 50% 3 squares to the right
Slip 20% 6 squares to the left
Slow plod 30% 1 square to the 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 slip 20% 2 squares to the left
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 printing BANG !!!!! AND THEY'RE OFF !!!!!
Then, for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the hare and your program should print OUCH!!! beginning at that position. All print positions other than the T, the H, or the OUCH!!! (in case of a tie) should be blank as shown in Figure 1 and 2 below. You can print S as the start position and F as the finish position.
After each line is printed, test whether either animal has reached or passed square 70. If so, then print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! If the hare wins, print HARE WINS!!! As shown in Figure 3.
If both animals win on the same tick of the clock, print IT'S A TIE!!!
If neither animal wins, perform the loop again to simulate the next tick of the clock.
Explanation / Answer
Here is the code and last few lines of output is shown. Since I did not find the figure showing the needed format of output, I have designed it myself. Please let me know if any help is needed to modify it. Post a comment in case of issues, I'll respond. Please rate the answer if it helped. Thank you very much.
Note: I have used a function to display the positions. I hope functions are covered for you in class :-)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void display(int tick, int start, int finish, int tortoise, int hare);
int main()
{
int tortoise = 1, hare = 1; //initial positions of the animals
int start = 1, finish = 70;
int n;
srand(time(NULL));
int tick = 1;
printf(" BANG !!! AND THEY'RE OFF !!! ");
while(1)
{
display(tick, start, finish, tortoise, hare);
n = 1 + (rand() % 10); //generate random number from 1-10
//generate move for hare
if(1 <= n && n <=2) //20 % no move
;
else if(3<=n && n <=4) //20% big hop , 9 squares right
hare += 9;
else if(n == 5) //10% , big slip, 12 squares left
hare -= 12;
else if(6<=n && n<=8) //30%, small hop, 1 square right
hare += 1;
else //20% , small slip, 2 squares to left
hare -= 2;
//generate move for tortoise
if(1 <= n && n <=5) //50 % fast plod, 3 squares to right
tortoise += 3;
else if(6<=n && n <=7) //20% slip , 6 squares left
tortoise -= 6;
else //30% , slow plod, 1 square right
tortoise += 1;
//check if animals have slipped behind start
if(hare < start)
hare = start;
if(tortoise < start)
tortoise = start;
if(hare >= finish || tortoise >= finish) //check if either of them have gone past finish
break;
tick++;
}
printf(" Total time elapsed is %d ticks", tick);
if(tortoise == hare)
printf(" It's a Tie !!! ");
else if(tortoise >= finish)
printf(" Tortoise wins !!! ");
else
printf(" Hare wins !!! ");
return 0;
}
//function to display the line with positions of tortoise and hare
void display(int tick, int start, int finish, int tortoise, int hare)
{
int i;
printf(" Tick %3d: [S]", tick);
for( i = start + 1; i < finish; i++)
{
if(i == hare && hare == tortoise)
printf("[OUCH!!] ");
else if(i == hare)
printf("[H]");
else if(i == tortoise)
printf("[T]");
else
printf("[]");
}
printf("[F] ");
}
output
BANG !!! AND THEY'RE OFF !!!
Tick 1: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 2: [S][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 3: [S][H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 4: [S][][][][][T][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 5: [S][][][][][][][][T][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 6: [S][][][][][][][][][T][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 7: [S][][][][][][][][][][][][T][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 8: [S][][][][][][][][][][][][][][][T][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 9: [S][][][][][][][][][][][][][][][][T][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 10: [S][][][][][][][][][][T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 11: [S][][][][][][][][][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 12: [S][][][][][T][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 13: [S][][][][][][T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 14: [S][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 15: [S][T][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 16: [S][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 17: [S][T][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 18: [S][][T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 19: [S][][][T][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 20: [S][][][][][][T][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 21: [S][][][][][][][][][T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 22: [S][][][][][][][][][][T][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 23: [S][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 24: [S][][][][][H][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 25: [S][][][H][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 26: [S][][][][H][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 27: [S][][][][][OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 28: [S][][][][][][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 29: [S][][][][][][][][][][][T][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 30: [S][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 31: [S][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 32: [S][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 33: [S][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 34: [S][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 35: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 36: [S][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 37: [S][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][F]
Tick 38: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][F]
Tick 39: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][F]
Tick 40: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][F]
Tick 41: [S][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][F]
Tick 42: [S][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][F]
Tick 43: [S][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 44: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 45: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 46: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][F]
Tick 47: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][F]
Tick 48: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][F]
Tick 49: [S][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][F]
Tick 50: [S][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][F]
Tick 51: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][F]
Tick 52: [S][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][F]
Total time elapsed is 52
Hare wins !!!