Search code examples
cprojectile

cannon projectile program using c programming


I am making a program that let's users enter velocity and angle and then program calculates the vertical and horizontal component and now i am trying to make the program also calculate time of flight (TOF). The equation is t=2*Vo*sin(ang)/g. but i am stuck on how to implement it. I have tried to implement it but i am not so successful.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void components(float Vo, float ang, float* outpVx, float* outpVy,float* X,float* Y, float a,float t)
{
    t=0.1;
    a=-9.81;

    *outpVx=Vo*cos(ang);
    *outpVy=Vo*sin(ang);

    *X=*outpVx*t;
    *Y=*outpVy*t+(0.5*a*(t*t));

}

void TOF(float Vo,float ang,float g,float t)
{
    g=9.81;
    main(Vo,ang);
    t=(2*Vo*sin(ang))/g;


}

int main(void)
{
    float Vo,ang;
    float outVx=0.0;
    float outVy=0.0;
    float X;
    float Y;
    float t=0.1;
    float a=-9.81;
    float g= 9.81;

    printf("\nEnter Velocity:\n");
    scanf("%f",&Vo);
    printf("\nEnter Angle:\n");
    scanf("%f",&ang);

    components(Vo,ang, &outVx, &outVy, &X, &Y,a,t);
    printf("Horizontal \t Vertical\n");
    printf("%f \t %f", X, Y);

    TOF(t,g,Vo,ang);
    printf("%f",t);


return 0;
} 

some guidance would be much appreciated.


Solution

  • Some big red flags right up front:

    void TOF(float Vo,float ang,float g,float t)
    {
        g=9.81;
        main(Vo,ang);
        t=(2*Vo*sin(ang))/g;
    }
    
    int main( void )
    {
      ...
      TOF(t,g,Vo,ang);
      ...
    }
    
    1. C doesn't match function arguments by name, but by position. The order of the arguments between the function call (TOF(t,g,Vo,ang)) and the function definition (void TOF(float Vo,float ang,float g,float t)) don't appear to match up. Also remember that each of Vo, ang, g and t in TOF are completely different objects in memory from the Vo, ang, g and t in main; writing to t in TOF does not affect the value of t in main. Also, the parameter g already contains the value 9.81; there's no need to write to it again in TOF.

    2. Why are you calling main from TOF? What is the purpose? Note that the definition of main does not expect any arguments, but you're passing two arguments in the call from TOF.

    Here's what I think you're going for:

    float TOF( float Vo, float ang, float g )
    {
      return (2 * Vo * sin( ang ) ) / g;
    }
    
    int main( void )
    {
      ...
      t = TOF( Vo, ang, g );
      ...
    }
    

    If you want to keep TOF typed void and have it update t in the argument list, then you must pass a pointer to t:

    void TOF( float Vo, float ang, float g, float *t )
    {
      *t = (2 * Vo * sin( ang ) ) / g;
    }
    
    int main( void )
    {
      ...
      TOF( Vo, ang, g, &t );
      ...
    }
    

    Also, remember that sin and cos expect their arguments to be expressed in radians, not degrees.

    Finally, unless you have a really good reason not to, use double instead of float. sin and cos both expect double arguments and return double values.