/* Falling Distance - When an object is falling because of gravity, the
following formula can be used to determine the distance the object
falls in a specific time period.
* d = 1/2gt²
The variables in the formula are as follows:
* d: distance in meters
* g: 9.8
* t: amount of time (seconds) the object is falling
The following function is used:
* fallingDistance()
This program demonstrates the function by calling it in a loop that
passes the values 1 through 10 as arguments, and displays the return
value. */
#include "Utility.h"
/* Prototype: Falling Distance */
double fallingDistance(double);
int main()
{
/* Variable: Falling time */
double distance = 0.0;
/* Display: Information, Header */
cout << "\t\tFree Falling Object Demo\n\n"
<< "This program demonstrates the function 'fallingDistance'\n"
<< "which calculates the distance an object falls in a specific\n"
<< "amount of time in seconds.\n\n";
cout << "After second(s):" << "\t" << "The ball has fallen:"
<< "\n------------------------------------------------------\n";
/* This loop calls fallingTime ten times, falling time, declared in
the loop header, passes fallingTime as argument to the function
fallingDistance, receives the return value stored in the variable
"distance", and displays both time and distance in meters/sec. */
for (double fallingTime = 1; fallingTime <= 10; fallingTime++)
{
distance = fallingDistance(fallingTime);
/* Set up: Formatted numeric output
Display: Time and Falling distance */
cout << fixed << showpoint << setprecision(2);
cout << setw(8) << left << fallingTime
<< "\t\t\t " << setw(8) << right << distance << " m.\n";
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: fallingDistance
This function accepts an object's falling time in seconds
as an argument. It returns the distance in meters that the
object has fallen during that time interval.
********************************************************** */
double fallingDistance(double fallingTime)
{
/* Static local variable: Acceleration (gravity) */
static double acceleration = 9.8;
/* Variable: Falling Distance */
double distance = 0.0;
/* Calculation: Falling distance */
distance = acceleration * pow(fallingTime, 2) / 2;
/* Return: Falling distance */
return distance;
}
Monday, January 2, 2017
Programming Challenge 6.5 - Falling Distance
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment