# Thread: need help with program

1. ## need help with program

I am working on a program that computes the duration of a projectiles's flight and its height above ground when it reacdhes the target. I can get it to compile and run but the math is wrong. Can anyone please help?

#include <stdio.h> /*printf, scanf definitions*/
#include <math.h> /*cos definition*/

#define G 32.17 /*gravitational constant*/

int
main(void)
{
double Distance; /*input-distance (ft) to target */
double Velocity; /*input-projectile velocity (ft/sec)*/

double Time; /* output-time(sec) of flight*/
double Height; /*output-height at impact*/

/*opening explanation to user*/
printf("This program computes the duration of a projectile's flight and its height above the ground when it reaches the target. To use this program, enter the angle, the distance, and the velocity after each of the prompt.");

printf("Enter the angle of elevation in radians>");
scanf( "%f", &Theta );
printf("Enter the distance to target in feet>");
scanf( "%f", &Distance );
printf( "Enter the projectile velocity in ft/sec>");
scanf( "%f", &Velocity );

Time = Distance / (Velocity * cos(Theta)) ;
Height = Velocity * sin(Theta) * Time -G*Time*Time/2.0;

printf("The time of flight is %.3f seconds.\n", Time);
printf("The height at impact is %.3f feets.\n", Height);

system ("pause");
return (0);

2. 1. Is the actual algorithm for this available?
2. Do you have sample (known correct) inputs and outputs?

Both would help with debugging....

3. Yes the Relevant formula is

time = distance
----------------------------------
velocity x cos(theta)

height= velocity x sin(theta) x time - g x time 2
------------------------
2

G 32.17

sample input is 0.3 for angle
11000 for distance
800 for velocity

sample output is 14.393seconds
70.632feet

I keep getting a negative number. (in the height equation that is time squared)

4. You might want to error check your input values (such as if the input fails).

I would recommend fgets() and strtod() so you can check for overflows. At least for the time being print out each value as you read it. You could also comment out the scanf()'s and just declare the values as per your examples.

And surely
Code:
`#define GRAVITY ...`
Isn't so long that it had to be shortened to "G"