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.
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)
}
Create a program to recreate the classic race between the hare and the tortoise. Each animal...
In Java: I got most of the logic down but I'd like to seen an answer that actually prints out the T and H and moves them or those something with an image since I only print out the winner and responses with my own code. This project involves writing a program to simulate a tortoise and hare race. The contenders will each race along a horizontal course that contains at least 50 squares. You may add more if you...
python. It is better to use "random" Now we want to analyze how often the tortoise and hare win or lose the race as we vary the race length. We can wrap the logic of our race simulation in a function and then call it many times to simulate many races. Then, we can collect some statistics to see how likely is it that the tortoise (or hare) wins a race of a given length. Specifically, write a function simulate_race...
In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...
Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFilename and MapFilename. Use char arrays and set them to nulls by using {}. Create a structure called RowCol that has two int members called row and col to hold the row and column. Function MoveNorth should have a return value of void and a parameter of a pointer to RowCol. { To move north, you need to subtract 1 from PositionPtr->row. Before doing so, you need to check...
Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
C++ Program - Arrays- Include the following header files in your program: string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step. Create a program which has: 1. The following arrays created: a. an array...
code in C++ Create a program that uses the pass by reference operator instead of the return command. Your main program will need to: create empty variables call void function 1 with no input to display your name to the screen call function 2 to collect the square feet that a gallon of paint covers from user call function 2 to collect the square feet of wall that needs to be painted call function 3 to calculate the number of...
C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...
Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...
In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...