Sei sulla pagina 1di 2

/* When an object is falling because of gravity, the following formuJa can be

used to determine the distance the object falls in a specific time period:
d = .5* g * t^2
The variables in the formula are as follows: d is the distance in meters, g is 9
.8,
and t is the amount of time, in seconds, that the object has been falling.
Write a function named fallingDistance that accepts an object's falling time (in
seconds) as an argument. The function should return the distance, in meters, tha
t the
object has fallen during that time interval. Write a program that demonstrates t
he
function by calling it in a loop that passes the values 1 through 10 as argument
s,
and displays the return value.
By: Asif Akash
Student ID# 900325657
*/

#include<iostream>
#include<cmath> // Required for exponential function
#include<iomanip> // Required for setw (set width)
#include<cstring> // to use strcmp string comparison function
#include<fstream> // Used for file output and input
using namespace std;

// Function prototypes
int getSeconds(); // 1
double fallingDistance(int); // 2
double loopDistanceCalc(double); // 3

int main ()
{
int intFallingSec; // Accept value from getSeconds function
double dblFallingDistance; // Accept value from fallingDistance function
double loopDistance; // Accept value from loopDistanceCalc function
int time; // Define parameter for loop
// Header
char letter; // delet
cout << "\n\n\t\t This program calculates falling distance with time ";
cout << "\n\n\t\t----------------------------------------------------\n";
// Set precision to 2 points
cout << fixed << showpoint << setprecision(2) ;
// Call getSeconds function
intFallingSec = getSeconds();
// Call fallingDistance function and provide value of intFallingSec variable
dblFallingDistance = fallingDistance(intFallingSec);
// Display distance to specified time
cout << "\n\a Distance traveled in " << intFallingSec << " seconds is " ;
cout << dblFallingDistance << " meters. " << endl;
// Loop header
cout << "\n\n\t\t Table below displays distance traveled from 1 to 10 second
s \n\n";
cout << "\t\t------------------------------------------------------------\n\
n";
// Loop for argument 1 to 10
for (time = 1 ; time <=10 ; time ++)
{
loopDistance = loopDistanceCalc(time);
// Display distance for time 1 to 10
cout <<"\t\tDistance covered in : " <<"\t" << time << " seconds is
";
cout <<"\t - "<< loopDistance << " meters." << endl;
}

cin >> letter; // delet


return 0;
}
// 1 Accept time in seconds
int getSeconds()
{
int intReturnFallingSec;
cout << "\n Enter time needed in seconds for object to travel : ";
cin >> intReturnFallingSec;
return intReturnFallingSec;
}
// 2 Calculates falling distance with specified time
double fallingDistance(int intP_FallingSec)
{
double dblReturnFallingDistance;
const double dblGravityConst = 9.8; // Gravity
// Calculation formulae
dblReturnFallingDistance = .5 * dblGravityConst * (pow(intP_FallingSec, 2
.0));
return dblReturnFallingDistance;
}
// 3 calculate distance with specified parameter argument
double loopDistanceCalc(double intP_time)
{
double returnLoopDistance;
const double dblGravityConst = 9.8; // Gravity
// Calculation formulae
returnLoopDistance = .5 * dblGravityConst * (pow(intP_time, 2.0));
return returnLoopDistance;
}

Potrebbero piacerti anche