CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 15th, 2009, 06:16 PM
    noey699 noey699 is offline
    Junior Member
     
    Join Date: Apr 2009
    Posts: 3
    noey699 is an unknown quantity at this point (<10)
    Gravity Simulator Problem

    I cannot figure out what is going on when I run this gravity simulator. I put in the correct numbers for the variables but when I run it it doesn't do what I want to. In the while loops in int main it is supposed display the positions of object 1 and 2 update the positions as many times as the accuracy calls for then repeat it but it only updates the positions once. The next ones are all the same. I am using Dev-C++ on windows vista.

    this is the code

    Code:
    #include <iostream>
    #include "required.h"
    #include <math.h>
    
    //declare needed variables
    using namespace std;
    double force1;
    int time3;
    double x1, yone, z1;
    double x2, y2, z2;
    double Mass1;
    double Mass2;
    double velocityx1;
    double velocityy1;
    double velocityz1;
    double velocityx2;
    double velocityy2;
    double velocityz2;
    double accuracy;
    int range;
    int second = 0;
    //F = GMm/R²
    //calculates gravity with the given parameters
    double GravityCalc(double Mass1, double Mass2, double Distance){
        //declare variables for order
        double top;
        double botom;
        //G is 1.0 x10^10 times bigger
        double G = .667;
        double Force;
        
        //calculate gravity
        top = G * Mass1 * Mass2;
        botom = Distance * Distance;
        Force = top / botom;
        return Force;
    }
    
    //finds the distance in 3 dimensional space between two objects
    double finddistance( double x1, double yone, double z1, double x2, double y2, double z2 ){
                  //declare order variables
                  double cx, cy, cz;
                  double distance;
                  //calculate the distances
                  cx = x1 - x2;
                  cy = yone - y2;
                  cz = z1 - z2;
                  distance = pow(cx, 2) + pow(cy, 2) + pow(cz, 2);
                  distance = sqrt(distance);
                  
                  //return distance always positive
                  if (distance > 0){
                               return distance;
                  }
                  if (distance < 0){
                               distance = distance * -1;
                               return distance;
                  }
                  
    }
    
                          
    
    int main(int argc, char *argv[]){
        double b = 0;
        double c = 0;
        //receive values of variables
        cout << "Enter mass of object 1: ";
        cin >> Mass1;
        cout << endl << "Enter mass of object 2: ";
        cin >> Mass2;
        cout << endl << "Enter position of object 1 x, y, and z:" << endl;
        cout << "x: ";
        cin >> x1;
        cout << endl << "y: ";
        cin >> yone;
        cout << endl << "z: ";
        cin >> z1;
        cout << endl << "Enter position of object 2 x, y, and z:" << endl;
        cout << "x: ";
        cin >> x2;
        cout << endl << "y: ";
        cin >> y2;
        cout << endl << "z: ";
        cin >> z2;
        cout << endl << "Enter the starting velocities of object 1:" << endl;
        cout << "x: ";
        cin >> velocityx1;
        cout << endl << "y: ";
        cin >> velocityy1;
        cout << endl << "z: ";
        cin >> velocityz1;
        cout << endl << "Enter the starting velocities of object 2:" << endl;
        cout << "x: ";
        cin >> velocityx2;
        cout << endl << "y: ";
        cin >> velocityy2;
        cout << endl << "z: ";
        cin >> velocityz2;
        cout << endl << "Enter the accuracy of the simulation from int between 0 and 1: ";
        cin >> accuracy;
        cout << endl << "Enter how long you want the sim to run in seconds: ";
        cin >> range;
        system("pause");
        system("cls");
        
        
        //update the positions according the range of the simulation and the accuracy
        while ( b <= range ){
              //display the positions
              cout << "Object 1 position: " << endl;
              cout << "x = " << x1 << "   y = " << yone << "   z = " << z1 << endl;
              cout << " Object 2 position: " << endl;
              cout << "x = " << x2 << "   y = " << y2 << "   z = " << z2 << endl << endl << endl;
              //calculate to the given accuracy
              while ( c <= 1 / accuracy ){
                    //positionupdate function coded in required.h
                    //calculate the force of the gravity between the two objects
                    force1 = GravityCalc( Mass1, Mass2, finddistance(x1, yone, z1, x2, y2, z2) );
                    //update positions of x
                    x1 = x1 + positionupdate(accuracy, force1, vectorx( x1, yone, z1, x2, y2, z2, 1));
                    x2 = x2 + positionupdate(accuracy, force1, vectorx( x1, yone, z1, x2, y2, z2, 2));
                    //update positions of y
                    yone = yone + positionupdate(accuracy, force1, vectory( x1, yone, z1, x2, y2, z2, 1));
                    y2 = y2 + positionupdate(accuracy, force1, vectory( x1, yone, z1, x2, y2, z2, 2));
                    //update positons of z
                    z1 = z1 + positionupdate(accuracy, force1, vectorz( x1, yone, z1, x2, y2, z2, 1));
                    z2 = z2 + positionupdate(accuracy, force1, vectorz( x1, yone, z1, x2, y2, z2, 2));
                    
                    //move loop forward
                    c = c + 1;
              }
              //move loop forwards
              b = b + 1;
              
        }
        
        
        system("pause");
        return EXIT_SUCCESS;
    }

    Last edited by noey699; October 15th, 2009 at 06:22 PM.
    Reply With Quote
      #2    
    Old October 16th, 2009, 05:46 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 291
    olivthill2 is a jewel in the rough (200+) olivthill2 is a jewel in the rough (200+) olivthill2 is a jewel in the rough (200+)
    Re: Gravity Simulator Problem

    Maybe the c variable needs to be initialized between the first and the second while line.
    Reply With Quote
      #3    
    Old October 16th, 2009, 05:50 AM
    g.eckert's Avatar
    g.eckert g.eckert is offline
    Member
     
    Join Date: Feb 2009
    Location: USA
    Posts: 68
    g.eckert is an unknown quantity at this point (<10)
    Re: Gravity Simulator Problem

    why are these global???
    Code:
    double force1;
    int time3;
    double x1, yone, z1;
    double x2, y2, z2;
    double Mass1;
    double Mass2;
    double velocityx1;
    double velocityy1;
    double velocityz1;
    double velocityx2;
    double velocityy2;
    double velocityz2;
    double accuracy;
    int range;
    int second = 0;
    __________________
    Google is your friend.
    Reply With Quote
      #4    
    Old October 16th, 2009, 07:48 AM
    treuss's Avatar
    treuss treuss is offline
    Elite Member
     
    Join Date: Jan 2004
    Location: Düsseldorf, Germany
    Posts: 2,332
    treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+)
    Re: Gravity Simulator Problem

    Quote:
    Originally Posted by man sqrt
    DESCRIPTION
    `sqrt' computes the positive square root of the argument. You can modify error handling for this
    function with `matherr'.
    This should help you remove superfluous code
    __________________
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth

    Computer programming requires a great deal of attention to detail and an ability to read and understand abstract instructions. Posting a question to this forum in a way that it will get answered, requires the same, though too far smaller degree. Thus, if you don't get your question answered here, maybe it's time to accept that programming is not your thing. --Yours truly

    Please read Information on posting before posting, especially the info on using [code] tags.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:29 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.