C++ program using visual c++. Functions, call and ref statements with for loops, output two separate tabless?

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:

main()
{
for(--------------)
call (by value)
output

for(---------------------)
call (by reference)
output

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;
}


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;
}
Here

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;
}


instead of fallingDistance1(d); you should write

d = fallingDistance1(t);

The function that declares the parameter as a reference is incorrect. You shall provide as a parameter also a time value.
Last edited on
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.
oohhhhh ok, i get now. thanx,....that worked perfectly.
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
Topic archived. No new replies allowed.