Question

In: Computer Science

comments please Your program will calculate the distance an object travels (in meters) on Earth for...

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.

Solutions

Expert Solution

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);
        
        }





Related Solutions

A light year is the distance light travels in 1 year. Light travels at 3*108 meters*...
A light year is the distance light travels in 1 year. Light travels at 3*108 meters* sec-1. Write a program that calculates and displays the number of meters light travels in a year.
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance...
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance = speed * time For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each...
calculate the distance d from the center of the earth at which a particle experiences equal...
calculate the distance d from the center of the earth at which a particle experiences equal attractions from the earth and from the moon. The particle is restricted to the line through the centers of the earth and the moon. Justify the two solutions physically.
Calculate the final speed of an object dropped on Earth from altitude of 8 times the...
Calculate the final speed of an object dropped on Earth from altitude of 8 times the radius of Earth. (hint: neglect friction due to atmosphere and use expression of gravitational potential energy and principle of conservation energy. Answer = 10.6 km/s).
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class,...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class, and so please use more basic techniques such as if else statements, switch operators, and for loops if needed. http://imgur.com/a/xx9Yc Pseudocode for the main method: Print the headings             Print the directions             Prompt for the month             If the month is an integer       (Hint: Use the Scanner class hasNextInt method.)                         Input the integer for the month                         Get the...
Calculate at what distance from the centre a satellite has to orbit Earth (of mass 6...
Calculate at what distance from the centre a satellite has to orbit Earth (of mass 6 x 10 24 kg) if it period or revolution is to equal Earth’s rotation period of 23h 56m (about 86, 160 seconds.) What is important about such a satellite? c) The mass of the moon is 81 times smaller (7.4074x 10 22 kg) and the period of orbit is 27.3 days. (Convert using 86, 400 sec = 1 day.) What should the radius of...
1a. The distance of an object and its image is 20cm. A lens has a focal point of -30cm. Calculate the object distance, describe the image, and construct a ray diagram for the lens (not in scale).
  1a. The distance of an object and its image is 20cm. A lens has a focal point of -30cm. Calculate the object distance, describe the image, and construct a ray diagram for the lens (not in scale). 1b. A 0.8Kg microphone is connected to a spring and is oscillating in simple harmonic motion up and down with a period of 3s. Below the microphone there is a source of sound with a frequency of 550Hz. Knowing that the change...
How can I calculate the amount of work done when moving a 567N crate a distance of 20 meters?
How can I calculate the amount of work done when moving a 567N crate a distance of 20 meters?
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT