-2

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.

  • 1
    You forgot to return the value. – Eugene Sh. Jan 08 '18 at 20:57
  • 4
    I'm baffled by why `TOF()` calls `main()`. – TypeIA Jan 08 '18 at 20:58
  • This is really bad formatting and asking for trouble: `*Y=*outpVy*t+(0.5*a*(t*t))` – meaning-matters Jan 08 '18 at 20:59
  • Make sure the order of parameters is the same in the function definition and function call. Take a long hard look at `TOF` to see what I mean. – John Bode Jan 08 '18 at 20:59
  • 1
    What is this line supposee to achieve? `main(Vo,ang);` It looks like a recursive call to main(), which would leave me surprised if it does not cause trouble. – Yunnosch Jan 08 '18 at 21:00
  • 1
    Aside from others things: Never call main manually from a function inside your program. – MikeMB Jan 08 '18 at 21:01
  • Your function `components()` shows that somebody has shown you how to return two values from a function. Why don't you do that in `TOF()`, too. Which currently only plays with local copies of parameter values.... – Yunnosch Jan 08 '18 at 21:02
  • Please consider this article, for getting your coding experiments back on firm ground. Split your goal into smaller parts. Split into simpler parts. Until the smallest, simplest thing works cleanly without problems. Then start working back to your final goal, incrementing in small, tested, working steps. https://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Yunnosch Jan 08 '18 at 21:06
  • On a point of style: `a=-9.81;` would be better as the well known `g=9.81;` as used in function `TOF`. Also I recommend that you always use `double` except when `float` is the only option. – Weather Vane Jan 08 '18 at 21:08

2 Answers2

1

You seem to have some trouble with C function calls.

A simplified solution for your canon problem would be:

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

#define PI 3.14159265

int main()
{
   double Vo,ang,rad,g,Vx,Vy,t;

   printf("\nEnter Velocity:\n");
   scanf("%lf",&Vo);

   printf("\nEnter Angle:\n");
   scanf("%lf",&ang);

   g = 9.81;
   rad = ang*PI/180;
   Vx = Vo*cos(rad);
   Vy = Vo*sin(rad);
   t = 2*Vy/g;

   printf("Velocity\tAngle\t\tHorizontal\tVertical\tTime of flight\n");
   printf("%lf\t%lf\t%lf\t%lf\t%lf\n", Vo, ang, Vx, Vy, t);

   return 0;
}

Example of execution:

Enter Velocity:
10

Enter Angle:
45
Velocity        Angle           Horizontal      Vertical        Time of flight
10.000000       45.000000       7.071068        7.071068        1.441604
Jos
  • 422
  • 4
  • 10
0

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198