In: Computer Science
Write a C++ program, falling.cpp, that inputs a distance in meters from the user and computes the amount of time for the object falling from that distance to hit the ground and the velocity of the object just before impact. Air resistance is discounted (assumed to fall in a vacuum).
To compute the time, use the formula: t = Square Root (2d / g)
where d is the distance in meters and g is the acceleration due to gravity on earth (use 9.807 meters/sec2). The time is measured in seconds.
To compute the velocity, use the formula:
v = Square Root (2dg)
The velocity is measured in meters per second.
You are required to write the following functions:
// Computes the amount of time for an object to fall to the // ground given the distance to fall as a parameter. The // time is returned (computeTime does not print anything). double computeTime (double distance);
// Computes the final velocity of an object falling to the // ground given the distance to fall as a parameter. The // velocity is returned (computeVelocity does not print // anything).
double computeVelocity (double distance);
// print prints its three parameters with labels.
// Print does not return a value.
void print (double distance, double time, double velocity);
Sample run:
Enter the distance: 100
Distance: 100.00 meters Time: 4.52 seconds
Velocity: 44.29 meters/second
Double values should be printed with two digits after the decimal.
Please comment, if you need any corrections.Please give a Thumps up if you like the answer.
Program
#include <iostream>
#include <cmath>
#include <iomanip>
#define g 9.807
using namespace std;
double computeTime (double distance);
double computeVelocity (double distance);
void print (double distance, double time, double velocity);
int main()
{
double distance,time,velocity;
cout<<"Enter distance in meters: ";
cin>>distance;
time=computeTime(distance);
velocity=computeVelocity (distance);
print (distance,time,velocity);
return 0;
}
double computeTime (double distance)
{
double time;
time = sqrt(2*distance / g);
return(time);
}
double computeVelocity (double distance)
{
double velocity;
velocity = sqrt(2*distance*g);
return(velocity);
}
void print (double distance, double time, double velocity)
{
cout << fixed << showpoint;
cout << setprecision(2);
cout<<"Distance: "<<distance<<"
meters Time: "<<time <<" seconds"<<endl;
cout<<"Velocity: "<<velocity<<"
meters/second"<<endl;
}
Output
Enter distance in meters: 100
Distance: 100.00 meters Time: 4.52 seconds
Velocity: 44.29 meters/second