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++.