# Project #14930 - C++

Exercise 1 pointers to arrays with functions 10-20

Instructions

1.1    Create a program that will use pointers to determine the average (to 1 decimal place) of a series of grades entered into an array.  You can assume a maximum of 15 students.

You will use a function called getgrades.  Send the array name (a pointer constant) to the function and accept it as a pointer variable (you can now use the pointer to increment through the array).  Using the pointer, keep accepting grades to fill up the array until they enter a sentinel value.  Input into where the pointer is pointing.  When the sentinel is entered, you will only return the number of grades entered.

Back in main, by setting a grade pointer to the beginning of the grades array, you will use the pointer to increment through each grade (only the grades inputted) to compute the total.  You will then use the total to compute and print out the average grade in main.

2.              Hare & Tortoise RacE - C++

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 regular int variable in main with a value 1 - 70 and will print as an H or T (it will be exciting to watch the H and T race across the screen until the finish line at column 70.  The program will call functions that use pointers and pass by reference and pass by value.  The move function will move two integer variables from main (one from hare and tortoise), but you can not return both at once so reference variables should be used.  The print function will print a snapshot of the race each instant of the race, by using pointers.

Random numbers will determine the amount each animal will move forward or backward.  Get a random number between 1 - 10.  Here are the percentages of moving the hare and tortoise:

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.

Each of the ten random numbers has a 10% chance of occurring, so if you need a 20% number use 2 of the possible random numbers.  For example, a 20% chance of sleeping, would be random numbers 1 and 2, a 20% big hop would be random numbers 3 and 4, etc.

Use 2 functions plus main.  The move function will use two value arguments and two reference arguments and the print function will use two pointer arguments.

Move the animals function - The move function will change the two integer variables from main (one each for the hare and tortoise), but you can not return both at once (so use by reference).  Send a hare 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).  Also send a tortoise random number from 1 - 10 by value and the tortoise value by reference.  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).  Then, in the same function, move the tortoise using the tortoise percentages and make sure the tortoise is referencing a number between 1 and 70.

Print the Race Track function - Accept both hare and tortoise reference variables into pointers (they will not change in the function, but use pointers anyways).  Then have a for loop for the 1 to 70 spaces on the race track.  For each increment of the loop, use a nested if else to see if both pointers are pointing at the loop variable ( print Bump at that spot), or if just the hare pointer is (print H), or if just the tortoise pointer is (print T), otherwise no animal is on that spot (just print a space).  End the line at the end of the loop.  Each call will print one snapshot of the race.

Main function - 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 hare random number between 1 and 10, get a tortoise random number between 1 and 10, then call the move function with the hare random number by value, the hare variable as a reference variable, the tortoise random number by value, and the tortoise as a reference variable.  Then call the print function (that uses the two pointers) to print one snapshot of the race track.

When the loop is done (at least one animal reaches 70), check and print whether it is a tie, the hare won, or the tortoise won.

Submit the program with increased documentation - at least name, exercise, four to five lines about the purpose, and at least 5 lines throughout the program explaining what is going on including some telling what each function does, etc.

Run your program three times (to show the random results) and save the output (you can copy and paste them into a blank .txt text file).  Submit both the .cpp program file and the .txt output file.

 Subject Computer Due By (Pacific Time) 09/20/2013 03:00 pm
TutorRating
pallavi

Chat Now!

out of 1971 reviews
amosmm

Chat Now!

out of 766 reviews
PhyzKyd

Chat Now!

out of 1164 reviews
rajdeep77

Chat Now!

out of 721 reviews
sctys

Chat Now!

out of 1600 reviews

Chat Now!

out of 770 reviews
topnotcher

Chat Now!

out of 766 reviews
XXXIAO

Chat Now!

out of 680 reviews