Need a little help please:
i have been trying to complete this for a couple days but i get more and more confused everytime i try.
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 = ½ gt^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 second 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, that the object has fallen during that time interval. Write a program that demonstrates the function by calling it in a loop that passes the values 1 though 10 as arguments, and displays the return value.
*Additional to this problem my prof wants two functions that calculate the falling distance.
function 1 passes arguments by value
function 2 passes argument by reference
outputs should be in form of a table
whats confusing is the prof established that in the main it should resemeble like this:
for(--------------)
call (by value)
output
for(---------------------)
call (by reference)
output
call by ref works fine but call by value only works when i put double d as global but then it only comes back as 0's in the output. its not reading the equation
my code so far:
Inline Code Example Here
# include <iostream>
# include <cmath>
# include <string>
using namespace std;
void fallingDistance2(double &);
double fallingDistance1(double);
const double g = 9.8;
int t;
double d;
int main()
{
cout<<"calculated by passby values.\n";
cout<<"Time \t\t Distance\n";
cout<<"-------------------\n";
for (t=1;t<=10;t++)
{
fallingDistance1(d);
cout<<t<<"\t\t"<<d<<endl;
}
cout<<"calculated by reference values.\n";
cout<<"Time \t\t Distance\n";
cout<<"-------------------\n";
for (t=1;t<=10;t++)
{
fallingDistance2(d);
cout<<t<<"\t\t"<<d<<endl;
}
return 0;
}
double fallingDistance1(double d)
{
d=0.5*9.8*t*t;
return d;
}
void fallingDistance2(double &refd)
{
refd=0.5*9.8*t*t;
}