//Dione Torrevillas CS1A Chapter 6, P. 370, #5
//
/****************************************************************
*
* COMPUTE FALLING DISTANCE
*_______________________________________________________________
* This program computes the distance an object falls due to
* gravity. It uses a function to calculate the distance for
* each second from 1 through 10.
*
* Computation is based on the formula:
* Distance = 0.5 * Gravity * Time * Time
* Gravity = 9.8 meters per second squared
*_______________________________________________________________
* INPUT
* time : Time the object has been falling in seconds
*
* OUTPUT
* distance : Distance the object falls in meters
*
****************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double fallingDistance(double);
int main()
{
int time; //INPUT - Falling time in seconds
double distance; //OUTPUT - Falling distance in meters
// Display table heading
cout << "Time (seconds) Distance (meters)" << endl;
cout << "------------------------------------" << endl;
// Calculate falling distance for 1 through 10 seconds
for (time = 1; time <= 10; time++)
{
// Call function to calculate distance
distance = fallingDistance(time);
// Display the time and falling distance
cout << fixed << setprecision(2);
cout << setw(7) << time
<< setw(23) << distance << endl;
}
return 0;
}
// Function definition
// Calculates and returns the falling distance
double fallingDistance(double time)
{
const double GRAVITY = 9.8; // Gravity in meters per second squared
double distance; // Falling distance in meters
// Calculate the falling distance
distance = 0.5 * GRAVITY * time * time;
return distance;
}