function for gravity project beginner

i need some help with a project of gravity for class and using it in a function here is what i currently have. But i cant seem to figure out how to make gravity a function within the loop and return the value distance

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main()
{
double seconds,time,gravity,distance;

seconds =0;
time =1;

cout <<"please enter the seconds that the object will be falling" <<endl;
cin >> seconds;
cout << "seconds distance" << endl;
cout << "=================" << endl;
while (1)
{
cout << time;
gravity =9.8;
distance =1/2*gravity*time*time;
if (seconds<=time) break;
time =time+1;
cout << " ";
cout << distance << endl;
}
cout << " ";
cout << distance << endl;
return 0;



i need it to look like this

Seconds Distance
=====================
1 4.9
2 19.6
3 44.1
4 78.4
5 122.5
6 176.4
7 240.1
8 313.6
9 396.9
10 490
1. to make a function you must declare a return type (in this case double)
2. declare the name,
3. parenthetically declare the parameters, seperated by ,'s
4. do whatever you want to do in your function (int this case nothing)
5. return what you want to (return (gravity / 2) * time * time;)

1
2
3
4
function_return_type function_name (parameter_type parameter /*, continue pattern*/){
//do stuff with the function
return what_function_did;
}


then you can use a for loop to go from 0 seconds to (whatever your user input) seconds.
do something like this:

1
2
3
4
5
6
7
8
9
10
for (int temp = 0; temp <= USER_INPUT; temp++){
 //The following lines up all numbers from 0 - 999 
 if (temp < 100){
        cout << " ";
        if (temp < 10){
         cout << " ";
        }
    }
cout << temp << " seconds = " << distance_function(temp) << endl;
}


edit: tense misuse
Last edited on
Topic archived. No new replies allowed.