CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: c++ physics

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    9

    c++ physics

    hey im new here, so easy lol. um im writing a c++ projectile motion code. What i got doesnt really work, like the values are wrong, and i dont know why. Any help? And this only has 1 function so far, once i get it to work, ill finish it. Also this is to all of u physics programmers, how can i take this a step further and make it solve things like where about will a soccer ball land if given the angle and some stuff? Anyways, just were am i going wrong? Thanks.


    //Richard Chaaya
    //1-25-09
    //kinimatics

    #include <iostream>
    #include <conio.h>
    #include <iomanip>
    #include <string>

    using namespace std;

    int main()
    {
    //declare variables
    double dblVix = 0.0;
    double dblViy = 0.0;
    double dblVfx= 0.0;
    double dblVfy= 0.0;
    double dblAx= 0.0;
    double dblAy= 0.0;
    double dblTime= 0.0;
    double dblDisx= 0.0;
    double dblDisy= 0.0;
    double Disx= 0.0;
    double Disy= 0.0;
    double dblDifference1 =0.0;
    char charAsk;
    char charDisx='y';
    char charTime='y';
    char charVfx=' ';
    char charVix=' ';
    char charAx=' ';
    char charAy=' ';
    char chartime= ' ';
    char charViy= ' ';
    char chrDisx=' ';
    char charDisy= ' ';
    char chrVfx=' ';
    char chrTime=' ';
    char chrVfy= ' ';
    char chrDisy= ' ';
    //Prototypes:
    double Ax (double, double, double, double, double, char, char, char);
    double Ay (double, double, double, double, double, char, char, char);

    //enter input
    cout << "hello, and welcome. This program is useful for projectile motion, it prompts you to enter the givens and the unknowns, and it outputs the answer ";
    cout << "\n\n Is Vi in the x given ?" <<endl;
    cin >> charVix;
    if(charVix=='Y' || charVix=='y')
    {cout << "what is the Initial velocity in the x?" <<endl;
    cin >> dblVix;}
    cout << "\n\n Is Vi in the y given ?" <<endl;
    cin >> charViy;
    if(charViy=='Y' || charViy=='y')
    {cout << "what is the Initial velocity in the y?" <<endl;
    cin >> dblViy;}
    cout << "\n\n Is A in the x given ?" <<endl;
    cin >> charAx;
    if(charAx=='Y' || charAx=='y')
    {cout << "what is the acceleration in the x?" <<endl;
    cin >> dblAx;}
    cout << "\n\n Is A in the y given ?" <<endl;
    cin >> charAy;
    if(charAy=='Y' || charAy=='y')
    {cout << "what is the Initial velocity in the x?" <<endl;
    cin >> dblAy;}
    cout << "\n\n Is Time given ?" <<endl;
    cin >> charTime;
    if(charTime=='Y' || charTime=='y')
    {cout << "what is the time (delta t)?" <<endl;
    cin >>dblTime;}
    cout << "\n\n Is the Displacement in the x given ?" <<endl;
    cin >> charDisx;
    if(charDisx=='Y' || charDisx=='y')
    {cout << "what is the displacement in the x?" <<endl;
    cin >> dblDisx;}
    cout << "\n\n Is the Displacement in the y given ?" <<endl;
    cin >> charDisy;
    if(charDisy=='Y' || charDisy=='y')
    {cout << "what is the Displacement in the y?" <<endl;
    cin >> dblDisy;}



    if(charAx=='Y' || charAx=='y')
    {
    dblAx=Ax(dblVix, dblVfx, dblTime, dblAx, dblDisx, chrDisx, chrTime, chrVfx);
    };

    cout <<dblAx;
    cout << " The acceleration in the x is" << dblAx;



    if(charAy=='Y' || charAy=='y')
    {
    dblAy=Ay(dblViy, dblVfy, dblTime, dblAy, dblDisy, chrDisy, chrTime, chrVfy);
    };

    cout << dblAy;

    cout << " The acceleration in the y is" << dblAy;


    _getch();
    ;
    }








    // This function solves for acceleration in the X direction.
    double Ax(double dblVix, double dblVfx, double dblTime, double dblAx, double dblDisx, char chrDisx, char chrTime, char chrVfx)
    {

    //define local varibles
    int intFunc = 0;

    //calculate
    if(toupper(chrDisx) != 'Y')//they dont have dblDisX
    intFunc = 1;
    if(toupper(chrTime) != 'Y')//they dont have dblTime
    intFunc = 2;
    if(toupper(chrVfx) != 'Y')//they dont have dblVfx
    intFunc = 3;

    switch (intFunc)
    {
    case 1:
    dblAx=(dblVfx - dblVix)/ dblTime ;
    break;

    case 2:
    dblAx=(dblVfx*dblVfx - dblVix*dblVix)/(2*dblDisx);
    break;

    case 3:
    dblAx= 2*(dblDisx - dblVix*dblTime)/(dblTime*dblTime) ;
    break;
    default:
    cout << "error";
    break;
    }
    return dblAx;
    }
    double Ay(double dblViy, double dblVfy, double dblTime, double dblAy, double dblDisy, char chrDisy, char chrTime, char chrVfy)
    {

    //define local varibles
    int intFunc = 0;

    //calculate
    if(toupper(chrDisy) != 'Y')//they dont have dblDisy
    intFunc = 1;
    if(toupper(chrTime) != 'Y')//they dont have dblTime
    intFunc = 2;
    if(toupper(chrVfy) != 'Y')//they dont have dblVfy
    intFunc = 3;

    switch (intFunc)
    {
    case 1:
    dblAy=(dblVfy - dblViy)/ dblTime ;
    break;

    case 2:
    dblAy=(dblVfy*dblVfy - dblViy*dblViy)/(2*dblDisy) ;
    break;

    case 3:
    dblAy=2*(dblDisy - dblViy*dblTime)/(dblTime*dblTime) ;
    break;
    default:
    cout << "error";
    break;
    }
    return dblAy;
    }
    Last edited by richard_chaaya2; January 26th, 2009 at 05:38 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)