0

ok so ive been working on the code for weeks now for class and i dont know how to start inputing a function this is what i have

#include <iostream>
using namespace std;

int main()
{
//declare variable//

const double g = 9.8;
 double s =1;
double d;
cout<<"Seconds    "<< "    Distance" <<endl;
cout<<"========================" <<endl;

for(s=1; s <= 10; s++)

{

//equation for distance falling//
d = ((.5)* g * s * s);


cout << s << "           " << d << endl;
 


}

#pragma region wait
cout<<" Press Enter to Quit";
cin.ignore();
cin.get();
#pragma end region
return 0;
}

the code does what i want it to but how do i get the equation "d = ((.5)* g * s * s)" as the function?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

One, make g a global constant (i.e. define it BEFORE, not INSIDE main). Then move your code outside of main and call it from main. You already have the formula:

d = ((.5)* g * s * s);

Make a function out of it:

double getDistance(double s)
{
    double d = ((.5)* g * s * s);
    return d;
}

Now call it from main:

d = getDistance(s);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.