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:
my code so far
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.
--------------------------
# 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;
}
You aren't saving the result of 'fallingDistance1' anywhere. Also, both falling distance functions are supposed to take the TIME as input, not 'd', which is somewhat meaningless as input.
now im just trying to figure out the ref value....when i switch parameter to t it does not work and when i move the int t; out of global into local...the t's become undefined eveverywhere