In: Computer Science
comments please
Your program will calculate the distance an object travels (in meters) on Earth for a specified number of seconds. You will also calculate the distance traveled on the Moon (in meters) for the specified number of seconds.
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.
ThecalculateEarthDistance function will calculate the distance an object falls (on Earth) during the specified number of seconds.
ThecalculateMoonDistance 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.
You can have additional function is needed.
Here is a summary of the processing that is required in the various functions:
double readSeconds()
This function reads in a value from cin. If the value is less than zero the function should output a message.
The value read in (valid or not) should be returned to the calling function.
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).
Use good variable names and not just d, g and t. Use double values for your calculations.
double calculateMoonDistance(double seconds)
This function calculates the distance traveled (on the Moon) 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 the same as the formula on Earth, but the value of g is different. For the Moon g is 1.6 meters / second squared.
Use good variable names and not just d, g and t. Use double values for your calculations.
void displayResults(double seconds, double earthDistance, double moonDistance)
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
Note that the distance is output with four digits to the right of the decimal point while seconds is output with two digits to the right of the decimal point. Both are in fixed format.
Assume that the number of seconds is 10.5, the output would be:
The object traveled 540.2250 meters in 10.50 seconds on Earth The object traveled 88.2000 meters in 10.50 seconds on the Moon
int main()
The main function will be the driver for your program.
First you need read in the input value. 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 main function should end.
Note that all of the required non-main functions are called from main.
Sample run with valid data
Assume the input is as follows:
10.5
Your program should output the following:
Enter the time (in seconds) The object traveled 540.225 meters in 10.50 seconds
Here is a run with invalid data
Here is the input to cin:
-12.4
The output would be:
Enter the time (in seconds) The time must be greater than zero
Failure to follow the requirements for lab lessons can result in deductions to your points, even if you pass the validation tests. Logic errors, where you are not actually implementing the correct behavior, can result in reductions even if the test cases happen to return valid answers. This will be true for this and all future lab lessons.
Expected output
There are 11 tests.
The first eight tests will run your program with input and check your output to make sure it matches what is expected.
For the comparison of output tests you will get yellow highlighted text when you run the tests if your output is not what is expected. This can be because you are not getting the correct result. It could also be because your formatting does not match what is required. The checking that zyBooks does is very exacting and you must match it exactly. More information about what the yellow highlighting means can be found in course "How to use zyBooks" - especially section "1.4 zyLab basics".
The last three tests are unit tests.
The unit tests are programs that have been written that will call your calculateEarthDistance and calculateMoonDistance functions to make sure it is correctly processing the arguments and generating the correct replies.
The unit tests will directly call the calculateEarthDistance and calculateMoonDistance functions. The compilation of the unit tests could fail if your calculateEarthDistance and calculateMoonDistance functions do not have the required signatures.
The unit tests will also test any results to make sure you have followed the directions above.
Finally, do not include a system("pause"); statement in your program. This will cause your verification steps to fail.
Note: that the system("pause"); command runs the pause command on the computer where the program is running. The pause command is a Windows command. Your program will be run on a server in the cloud. The cloud server may be running a different operating system (such as Linux).
Error message "Could not find main function"
Now that we are using functions some of the tests are unit tests. In the unit tests the zyBooks environment will call one or more of your functions directly.
To do this it has to find your main function.
Right now zyBooks has a problem with this when your int main() statement has a comment on it.
For example:
If your main looks as follows:
int main() // main function
You will get an error message:
Could not find main function
You need to change your code to:
// main function int main()
If you do not make this change you will continue to fail the unit tests.
Please find the program with the comments:
#include <iostream>
// C++ header file for pow function , which is being used to calculate the distance
#include <cmath>
using namespace std;
//Read Seconds method
double readSeconds(){
double timeTravelled;
cout << "Enter the time (in seconds) \n";
//Takes a double value in variable timeTravelled
cin >> timeTravelled;
return timeTravelled;
}
//Calculates the distance travelled by Earth
double calculateEarthDistance(double seconds){
//Assigns the value to g for earth
double g= 9.8;
double distanceTravelledbyEarth= 0.5 * g * pow(seconds, 2);
return distanceTravelledbyEarth;
}
//Calculates the distance travelled by Moon
double calculateMoonDistance(double seconds){
//Assigns the value to g for moon
double g= 1.4;
double distanceTravelledbyMoon= 0.5 * g * pow(seconds, 2);
return distanceTravelledbyMoon;
}
//Displays the result
void displayResults(double seconds, double earthDistance, double moonDistance){
cout<<"The object traveled "<< earthDistance<<" meters in "<<seconds<<" seconds on Earth \n";
cout<<"The object traveled "<< moonDistance<<" meters in "<<seconds<<" seconds on Moon \n";
}
//Driver function
int main(){
//Reads the input from readSeconds function
double timeTravelled= readSeconds();
//Checks the condition for time less than 0, and exits the program
if(timeTravelled<0){
cout<<"The time must be greater than zero";
exit(0);
}
//Calculate the distance travelled form earth
double distanceTravelledByEarth=calculateEarthDistance(timeTravelled);
//Calculate the distance travelled form moon
double distanceTravelledByMoon=calculateMoonDistance(timeTravelled);
//Displays the result
displayResults(timeTravelled,distanceTravelledByEarth,distanceTravelledByMoon);
}