Gravity

CMPC :: Computing :: Programming :: C++

Go down

Gravity

Post by Admin on Wed Dec 23, 2009 5:37 am

Create a program that finds the gravitational force acted upon between two given masses and a given distance.


AND I POSTED THIS IN THE C++ FORUM FOR A REASON Suspect


Last edited by Admin on Wed Dec 23, 2009 6:29 am; edited 1 time in total
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: Gravity

Post by Admin on Wed Dec 23, 2009 6:25 am

now i tried this myself. and i got the thing written up but i keep getting a damn error. so .... Razz


code is in spoiler

Spoiler:
Code:
#include <iostream>
#include <cmath>

using namespace std;


double compute(double m1, double m2, double d)
{
     
      static double G =6.67428*(double pow(10,-11));
      double force=((G*m1*m2)/(double pow(d,2)));/*you can just return this thing instead of
      declaring a whole new variable for this line,
        but this is just for making the code more legible.*/
      return force;
}     


int main()
{
    double mass1,mass2;
    double distance;
   
  cout<<"This is the gravitational force calculating program. All input must be in SI units!\n";
  cout<<"Please input two masses in the following lines. Remember they must be in kilograms.\n\n";
 
  cout<<"First Mass: ";
  cin>>mass1;
  cout<<"\nSecond Mass: ";
  cin>>mass2;
 
  cout<<"\n\nOK so far so good, now we need the distance between the centers of the two masses."<<endl;
  cout<<"\nDistance: ";
  cin>>distance;
 
  cout<<"Time to work some programming magic here..... answer: "<<compute(mass1,mass2,distance);
  cin.get();
  return 0;
}

_________________
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: Gravity

Post by Admin on Wed Dec 23, 2009 6:33 am

hmm i got an interesting idea. if i don't use the math library it compiles fine. wtf?

well here it is


edit: wait this doesn't work yet.... Razz gimme a sec...


edit: ok after tweaking around i found that if you enter a double into the DISTANCE variable, it screws up the program. .... wtf.?
Spoiler:
Code:
#include <iostream>

using namespace std;


double compute(double m1, double m2, double d)
{
     
      static double G =.0000000000667428;
      double force=((G*m1*m2)/d*d);/*you can just return this thing instead of
      declaring a whole new variable for this line,
        but this is just for making the code more legible.*/
      return force;
}     


int main()
{
    double mass1,mass2;
    double distance;
   
  cout<<"This is the gravitational force calculating program. All input must be in SI units!\n";
  cout<<"Please input two masses in the following lines. Remember they must be in kilograms.\n\n";
 
  cout<<"First Mass: ";
  cin>>mass1;
  cout<<"\nSecond Mass: ";
  cin>>mass2;
 
  cout<<"\n\nOK so far so good, now we need the distance between the centers of the two masses."<<endl;
  cout<<"\nDistance: ";
  cin>>distance;
 
  cout<<"Time to work some programming magic here..... answer: "<<compute(mass1,mass2,distance);
  cin.get();
  return 0;
}
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: Gravity

Post by Paul on Wed Dec 23, 2009 8:33 pm

Code:

double compute(double m1, double m2, double d)
{
   
      static double G =.0000000000667428;
      double force=( G*( (m1*m2)/(d*d) ) );
      return force;
}

try this. . .

I'm not checking the code, I'm just correcting one BLATANT error
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: Gravity

Post by Admin on Wed Dec 23, 2009 9:10 pm

when i enter a double for the value of distance, the console just disappears Razz i've had this problem before your correction and still afterwards.

_________________
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: Gravity

Post by Paul on Thu Dec 24, 2009 10:56 pm

Code:

double compute(double m1, double m2, double d)
{
      static double G =0.0000000000667428;
      double force=( G*( (m1*m2)/(d*d) ) );
      return force;
}
avatar
Paul
Pickaxe

Posts : 611

Back to top Go down

Re: Gravity

Post by Lunsfordium on Tue Aug 10, 2010 5:04 am

i love how you guys just post code willy-nilly without explanations
avatar
Lunsfordium
[Blankie]

Posts : 194

Back to top Go down

Re: Gravity

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

CMPC :: Computing :: Programming :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum