here is what i need and what i go so far can aybody help

i need to write a fuction called fallingDistance that accepts an objects falling time as an argument. The fuction 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 through 10 as arguments, and displays the return value. i'm at a lost on this one.

can anybody give me the basic skeleton of the program and i can go from there.
just tell me what comes before the int main() and if i need any #include besides <iostream>, also were do i put d=1/2gt^2. if i should put the loop after the int main() or near the end of the program.

[code]
#include <iostream>
namespace std;

double fallingDistance(double, double, double)

int main ()
{
double d, g, t;

cout << "Giving the G and the T I will give you the D ";
cin >> d
return d;
}
double fallingDistance(double, double, double)

{

)
[code]
i got this so far i dont know if that right, if it is where do i put the for loop and the d=1/2gt^2. if its not right what do i need to change.
First of all you might want to use more descriptive variable names. It doesn't hurt to use multiple-lettered variables, and it can help make your code readable.

In your description of the function you need, you say it only takes the time that passes while the object is falling, so you should create fallingDistance() to only accept one argument. Also, if your program isn't going to be handling different values for gravity, which i assume is what 'g' stands for, you can define that as a constant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
/* namespace std is default, so unless you are using more than one namespace, declaring it isn't strictly required by the compiler. */

#define GRAV /* your g value here */

double fallingDistance(double);

int main(){
  int i;
  double time,dist;
  for(i = 0; i < 10; i++){
    time = i + 1.0; /* or use 'time = (double)i + 1; */
    dist = fallingDistance(time);
    cout << "Given gravity of, " << GRAV << 
           " meters per second squared, an object will fall " 
           << dist << " meters in " << time << " seconds." << endl;
  }
  return 0;
}

double fallingDistance(double time){
  /* your code */
}


I don't know why you have cin>> in your program; accepting a parameter is different from accepting user input. From your explanation, I would have thought that the loop would have handled the changing values. I began the increment at 0 out of habit. I find that because arrays are zero-indexed, things get crazy when using for() loops that begin at other values. In case you were wondering, I used an int for the loop because double arithmetic can be prone to rounding error. It shouldn't make a difference for whole numbers, but from experience I've seen that it can.

Also remember when you print text to make a newline ( "endl" or "\n" ) at the end. And don't forget arithmetic is explicit in c/c++, so the compiler won't understand 'd = 1/2gt^2'. Instead put 'd = 1/2 * g * pow(t,2)', and #include <cmath>. Unless I overlooked something, '^' doesn't mean 'to the power of' in c/c++.
what code are talking about that i put under the double fallingDistance (double time) and where do i put my equation at
You put your equation in the fallingDistance(). According to your description, your equation takes one argument, time, and gives back the distance an object would have fallen. Which is why you see
 
    dist = fallingDistance(time);

Topic archived. No new replies allowed.