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

Correct way.

double fallingDistance1() // no need for any parameters
{
    double d=0.5*9.8*t*t;
    return d;
}

d = fallingDistance1(); // harness the return value

The way you had it...

double fallingDistance1(double d) // no need for any parameters
{
    d=0.5*9.8*t*t;
    return d;
}

fallingDistance1(d);

There's no point returning a value if you don't do anything with it. There's also no point in passing a parameter by value if all you do is immediately overwrite that value without ever using it. In my opinion, you really should be passing time as a parameter instead of having it global, but as far as the issue you were having, that's the problem. Try it with the above changes.

Edited 4 Years Ago by VernonDozier

I think this is how you should have done it

# include <iostream>
# include <cmath>
# include <string>
using namespace std;

void fallingDistance2(double &, double);
double fallingDistance1(double);
const double g = 9.8;
double 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++)
    {
        cout<<t<<"\t\t"<<fallingDistance1(t)<<endl;
    }


    cout<<"calculated by reference values.\n";
    cout<<"Time \t\t Distance\n";
    cout<<"-------------------\n";
    for (t=1;t<=10;t++)
    {
        fallingDistance2(d, t);
        cout<<t<<"\t\t"<<d<<endl;
    }


    return 0;
}


double fallingDistance1(double t)
{
    return 0.5*9.8*t*t;
}

void fallingDistance2(double &refd, double t)
{
    refd=0.5*9.8*t*t;
}

yes that worked perfectly...this is my first time doing this so i can do functions loops etc separately but got confused when i had to put everything together...ty u very much...great help

That is the cleaner version :)

# include <iostream>

using namespace std;

double fallingDistance(double t)
{
    return 0.5*9.8*t*t;
}


int main()
{
    static double t;
    cout<<"Different falling times and distances.\n";
    cout<<"_____________________\n";
    cout<<"Time \t\t Distance\n";
    cout<<"_____________________\n";
    for (t=1;t<=10;t++)
    {
        cout<<t<<"\t\t"<<fallingDistance(t)<<endl;
    }


    return 0;
}
This article has been dead for over six months. Start a new discussion instead.