Passing arguments byValue and using return types include us
// Passing arguments by-Value and using return types.
#include<iostream>
using namespace std;
// Constant factor for gravitational acceleration
const double GRAVITATION_CONSTANT = 9.8; // meters/sec^2
// TODO: Declare 3 function prototypes
// - Name: getFallingTime
// Parameters: <none>
// Returns: a double for the amount of time an object falls (to be entered by the user)
// - Name: fallingDistance
// Parameters:
// - 1 double for time in seconds (pass by value)
// Returns: a double for the number of meters the object falls
// - Name: displayResults
// Parameters:
// - 1 double for time (in seconds - pass by value)
// - 1 double for distance (in meters - pass by value)
// Returns: <nothing>
// ********************************************
int main()
{
// Declare program variables.
double timeInSeconds, distanceInMeters;
// TODO: Call function to get the amount of time the object falls.
// A double value is returned - assign to the correct variable
// TODO: Call function to calculate the distance the object falls,
// given the seconds that it falls
// Pass one argument representing the amount of time the object falls.
// Assign the returned value to the correct variable
// TODO: Call function to print the results.
// Pass the correct two arguments to the function
// Nothing is returned, so do NOT provide a variable assignment
cout << \"End program - \";
return 0;
}
// ********************************************
// TODO: Define getFallingTime()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter the seconds the object falls
// - Read the user\'s keyboard response
// Until a value more than 0 is entered
// ********************************************
// TODO: Define fallingDistance()
// Refer to the function prototype above for parameter and return type info
//
// The formula for the distance an object falls due to gravity is:
// d = 1/2 gt^2 (^2 means squared)
// where:
// d = the distance the object falls (in meters)
// g = the gravitational acceleration constant (in meters per second squared)
// t = the amount of time the object falls (in seconds)
// ********************************************
// TODO: Define displayResults()
// Refer to the function prototype above for parameter and return type info
// Output statement to format results as shown in Sample Output
// using values passed to parameters.
/* Sample Output
Sample Run #1: <this line not in output>
===================== <this line not in output>
Enter seconds the object falls: 0
Enter seconds the object falls: 5.75
In 5.75 seconds, the object falls 162.006 meters.
End program - Press any key to continue . . .
Sample Run #2: <this line not in output>
===================== <this line not in output>
Enter seconds the object falls: -10
Enter seconds the object falls: -1
Enter seconds the object falls: 0.75
In 0.75 seconds, the object falls 2.75625 meters.
End program - Press any key to continue . . .
*/
Solution
#include <iostream>
using namespace std;
// Constant factor for gravitational acceleration
const double GRAVITATION_CONSTANT = 9.8; // meters/sec^2
// TODO: Declare 3 function prototypes
// - Name: getFallingTime
// Parameters: <none>
// Returns: a double for the amount of time an object falls (to be entered by the user)
// - Name: fallingDistance
// Parameters:
// - 1 double for time in seconds (pass by value)
// Returns: a double for the number of meters the object falls
// - Name: displayResults
// Parameters:
// - 1 double for time (in seconds - pass by value)
// - 1 double for distance (in meters - pass by value)
// Returns: <nothing>
// ********************************************
double getFallingTime();
double fallingDistance(double);
void displayResults(double,double);
int main()
{
// Declare program variables.
double timeInSeconds, distanceInMeters;
// TODO: Call function to get the amount of time the object falls.
// A double value is returned - assign to the correct variable
timeInSeconds = getFallingTime();
// TODO: Call function to calculate the distance the object falls,
// given the seconds that it falls
// Pass one argument representing the amount of time the object falls.
// Assign the returned value to the correct variable
distanceInMeters = fallingDistance(timeInSeconds);
// TODO: Call function to print the results.
// Pass the correct two arguments to the function
// Nothing is returned, so do NOT provide a variable assignment
displayResults(timeInSeconds,distanceInMeters);
cout << \"End program - \" << endl;
return 0;
}
// ********************************************
// TODO: Define getFallingTime()
// Refer to the function prototype above for parameter and return type info
//
// Repeat
// - Prompt the user to enter the seconds the object falls
// - Read the user\'s keyboard response
// Until a value more than 0 is entered
double getFallingTime()
{
double timeInSeconds;
while(true)
{
cout << \"Enter seconds the object falls: \";
cin >> timeInSeconds;
if (timeInSeconds > 0)
{
break;
}
}
return timeInSeconds;
}
// ********************************************
// TODO: Define fallingDistance()
// Refer to the function prototype above for parameter and return type info
//
// The formula for the distance an object falls due to gravity is:
// d = 1/2 gt^2 (^2 means squared)
// where:
// d = the distance the object falls (in meters)
// g = the gravitational acceleration constant (in meters per second squared)
// t = the amount of time the object falls (in seconds)
double fallingDistance(double timeInSeconds)
{
double distance = 0.5*GRAVITATION_CONSTANT*timeInSeconds*timeInSeconds;
return distance;
}
// ********************************************
// TODO: Define displayResults()
// Refer to the function prototype above for parameter and return type info
// Output statement to format results as shown in Sample Output
// using values passed to parameters.
void displayResults(double timeInSeconds, double distanceInMeters)
{
cout << \"In \" << timeInSeconds <<\" seconds, the object falls \" << distanceInMeters << \" meters.\" << endl;
}
/* Sample Output
Sample Run #1: <this line not in output>
===================== <this line not in output>
Enter seconds the object falls: 0
Enter seconds the object falls: 5.75
In 5.75 seconds, the object falls 162.006 meters.
End program - Press any key to continue . . .
Sample Run #2: <this line not in output>
===================== <this line not in output>
Enter seconds the object falls: -10
Enter seconds the object falls: -1
Enter seconds the object falls: 0.75
In 0.75 seconds, the object falls 2.75625 meters.
End program - Press any key to continue . . .
*/



