C ++ - Create a program that calculates the distance an object travels in meters on Earth for a specific number of seconds. Also calculate the distance traveled on the moon, also in meters, for a specific number of seconds.
C ++ - Create a program that calculates the distance an object travels in meters on Earth for a specific number of seconds. Also calculate the distance traveled on the moon, also in meters, for a specific number of seconds.
Do not include any global variables
Your program must have the main function and, at least, the following four additional functions. The signatures for these functions must be as follows:
double readSeconds()
double calculateEarthDistance(double seconds)
double calculateMoonDistance(double seconds)
void displayResults(double seconds, double earthDistance, double moonDistance)
The readSeconds function will be an input function that will read in a double value from cin and return that value back to main.
The calculateEarthDistance function will calculate the distance an object falls (on Earth) during the specified number of seconds.
The calculateMoonDistance function will calculate the distance an object falls (on the Moon) during the specified number of seconds.
The displayResults function that will display the number of seconds an object has fallen as well as the distance the object has fallen on the Earth and on the Moon.
The prompt from the function should be:
Enter the time (in seconds)
If the value is less than zero you should output the following message.
The time must be zero or more
double calculateEarthDistance(double seconds)
This function calculates the distance traveled (on Earth) during the number of seconds pass in as a parameter. The distance is calculated in meters and is returned to the calling function.
The formula is:
d = 0.5 * g * pow(t, 2)
Where d is distance (in meters), t is time (in seconds) and g is 9.8 meters / second squared (the acceleration due to gravity on the earth). Formula for the moon is the same but g is 1.6 meters / second squared. Use double for calculations.
The displayResults function takes three parameters of type double. The first is the number of seconds and the second is the distance traveled on the Earth, and the third parameter is the distance traveled on the Moon. Note that the displayResults function must be passed the values for seconds, earthDistance, and moonDistance. The displayResults function MUST NOT call readSeconds, calculateEarthDistance, or calculateMoonDistance.
The output is the text:
The object traveled xxx.xxxx meters in zz.zz seconds on Earth The object traveled yy.yyyy meters in zz.zz seconds on the Moon
int main()
First you need a loop that will process input values until you get an input value that is equal to 0. You will get this input value by calling the readSeconds function.
If the value is greater than zero the main function needs to call the calculateEarthDistance, calculateMoonDistance, and displayResults functions.
If the value is less than zero the loop should end and your program should then end.
Note that all of the required non-main functions are called from main.
Note that a zero is used to terminate the loop, but a value of zero is actually a valid value for seconds.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images