Create a program to recreate the classic race between the hare and the tortoise.
ID: 645652 • Letter: C
Question
Create a program to recreate the classic race between the hare and the tortoise. Each animal will move across the screen left to right over the course of 70 squares from 1 - 70 (each will have their own variable valued 1 - 70 and will print as an H or T) . Random numbers will determine the amount each animal will move forward or backward. You will write a program calls functions that uses pointers and pass by reference and pass by value:
Hare: 20% Sleep - no move, 20% Big Hop - 9 squares to the right, 10% Big Slip - 12 squares to the left, 30% Small Hop - 1 square to the right, 20% Small Slip - 2 squares to the left.
Tortoise: 50% Fast Plod - 3 squares to the right, 20% Slip - 6 squares to the left, 30% Slow Plod - 1 square to the right.
Use 3 functions
Move the Hare Send a random number from 1 - 10 by value (will not change) and the hare value by reference (into a reference variable because it will change and you do not want to return it). Then in a switch or nested if else, change the value of the hare's reference variable based upon the percentages (ex. if random number is 1 or 2, don't move, if it is a 3 or 4 add 9 to the hare value, etc.). Make sure the hare is referencing a number between 1 and 70 (or they would be off the course).
Move the Tortoise similar to Hare function, using the tortoise percentages.
Print the Race Track Accept both hare and tortoise reference variables into pointers. Then have a for loop for the 1 to 70 squares. For each increment, use a nested if else to see if both pointers are pointing at the loop variable (Bump), or if just the hare pointer is (H), or if just the tortoise pointer is (T), otherwise no animal is on that spot (just print a space).
Main:
initialize hare and tortoise variables to 1 (start of race track).
In a while loop, while neither one is up to 70 (finish line), get a random number between 1 and 10, then call the move hare function with the random number (by value) and the hare variable as a reference variable. Get another random number and make a similar function call for tortoise. Then call the print function (that uses the two pointers).
When the loop is done (at least one animal reaches 70), check to see if it is a tie, the hare won, or the tortoise won.
Explanation / Answer
void movehare(int rand_num, *int harevalue)
{
if (rand_num = 1 || rand_num == 2)
movehare = 0
else if (rand_num == 3 || rand_num == 4)
movehare = 9
else if (rand_num == 5 || rand_num == 6)
movehare = 20
else if (rand_num == 7 || rand_num == 8)
movehare = 12
else if (rand_num == 9)
movehare = 1
else if (rand_num == 0)
movehare = 2
}
void racetrack()
{
while( pos_hare != 70 || pos_tor != 70)
{
pos_hare = pos_hare + movehare
pos_tor = pos_tor + movetor
if (pos_hare == post_tor)
printf("BUMP");
else if (pos_hare > pos_tor)
printf("H");
else
printf("T");
}
}
void main()
{
pos_hare = pos_tor = 1;
while(pos_hare != 70 || pos_tor ! = 70)
int movehare(value, &haremove)
int movetor(value, &tormov)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.