Newton's Law program

#include <iostream>

using namespace std;

int main()
{


double m1;
double m2;
double r2;
double G = ;


cout<< "How much does the first person weigh?"<< endl;
cin>> m1;

cout<< "How much does the second person weigh?"<< endl;
cin>> m2;

cout<<"What is the distance between the two bodies?" << endl;
cin>> r2;

double Force = G*((m1*m2/r2));
cout<< Force << endl;

system("pause");

return 0;



}

I'm having a little bit of trouble figuring out how to code the value of G. Adequately it's supposed to look like this:

G = (6.67428 * 10^-11) * ((m^3)/kg * (s^2))

The enitire program is supposed to illustrate the force between two body masses (represented by m1 and m2) at a particular distance (represented by r2).

I am to prompt the user to input the two body masses from a particular distance to reveal the force.

How do I code the value of G in C++?
const double G = 6.6742E-11;
Last edited on
What about the rest of it? The ((m^3)/kg * (s^2))?
((m^3)/kg * (s^2)) is the unit of G... You don't need it.

and double Force = G*((m1*m2/r2)); should be (r2*r2) instead of just r2

double Force = (G*m1*m2)/(r2*r2);
See the section on Floating Point Numbers here: http://www.cplusplus.com/doc/tutorial/constants/

See also Declared constants in the same document.
Last edited on
Why would the formula for Force be = (G*m1*m2)/(r2*r2)? I don't get it.
That's a physics question.

The gravitational attraction force between two bodies is proportional to the two masses. For example, a 10kg mass is heavier than a 1kg mass. And on the surface of Jupiter (which is more massive than Earth) both will still have the same mass, but will feel much heavier. Similarly on the surface of the Moon (which is much less massive than Earth) the two will feel much lighter than on earth.

The force is also inversely proportional to the square of the distance (thats the /(r2*r2) part). Meaning as you get further away, the gravitational attraction gets weaker. For example here on Earth, we are subject to the attraction of both the Sun and the Moon, but because of the distances involved, the force from the Moon is stronger than that of the Sun, even though it is much less massive. We can see this from the way the tides are affected by the Moon, and to a lesser extent by the Sun.

Finally the gravitational constant G is included simply to make things come out neatly in familiar man-made units (in this case Newtons are the unit of force).

The two masses (in this example the 1kg and 10kg masses) are also attracted to each other. So why don't they stick together like a pair of magnets? Well, if you do the calculation, you will see that the gravitational force is extremely weak. We only feel them to be heavy (strongly attracted towards the centre of the Earth) because the Earth itself is so extremely large and massive (though compared with other astronomical bodies, Earth and even our Sun can be considered tiny).
Last edited on
Topic archived. No new replies allowed.