# Assignment 5 Pointer-based pass-by-reference usage Solution

\$30.00 \$24.90

## Description

Book problem 8.12 (Simulation: The Tortoise and the Hare) In this exercise, you will re-create the classic race of the tortoise and the hare. You will use random number generation to develop a simulation of this 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 another function “moveHare” to adjust the position of the animals according to the rules in Fig. 8.18. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare. You do not need to create a class and you can do all this in one .cpp file. Note that this is not a real time simulation, thus you may use a counter to count the number of steps (seconds) the simulated race took to complete.

 Percentage of Animal Move type 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

Fig. 8.18 Rules for moving 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 l. 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!!!! !

For each tick of the clock (i.e., each repetition of a loop), print 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 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.

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. Yuck. 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 tie. If neither animal wins, perform the loop again to simulate the next tick of the clock.

that function does. You should have comments at the top that includes your name, the date, explains what the program does and comments above the functions explaining what they do in addition to some comments as needed on lines that explain what that line does. You do not need to implement a class and you may do all of this assignment in one file.

Example output:

BANG !!!!

AND THEY’RE OFF !!!!

OUCH!!!

1. T H T H T H T

H

H

T

T

H

T

1. T

HT

H

H

T

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

H

T

T

H

T

H

T

H

T

H

T

H

T

H

T

H

H

T

T

H

H

T

T

H

T

H

H

T

T

H

T

H

T

H T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

TORTOISE WINS!!! YAY!!!

TIME ELAPSED = 46 seconds

Press any key to continue . . .Book problem 8.12 (Simulation: The Tortoise and the Hare) In this exercise, you will re-create the classic race of the tortoise and the hare. You will use random number generation to develop a simulation of this 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 another function “moveHare” to adjust the position of the animals according to the rules in Fig. 8.18. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare. You do not need to create a class and you can do all this in one .cpp file. Note that this is not a real time simulation, thus you may use a counter to count the number of steps (seconds) the simulated race took to complete.

 Percentage of Animal Move type 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

Fig. 8.18 Rules for moving 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 l. 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!!!! !

For each tick of the clock (i.e., each repetition of a loop), print 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 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.

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. Yuck. 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 tie. If neither animal wins, perform the loop again to simulate the next tick of the clock.

that function does. You should have comments at the top that includes your name, the date, explains what the program does and comments above the functions explaining what they do in addition to some comments as needed on lines that explain what that line does. You do not need to implement a class and you may do all of this assignment in one file.

Example output:

BANG !!!!

AND THEY’RE OFF !!!!

OUCH!!!

1. T H T H T H T

H

H

T

T

H

T

1. T

HT

H

H

T

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

H

T

T

H

T

H

T

H

T

H

T

H

T

H

T

H

H

T

T

H

H

T

T

H

T

H

H

T

T

H

T

H

T

H T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

H

T

TORTOISE WINS!!! YAY!!!

TIME ELAPSED = 46 seconds

Press any key to continue . . .