double theta;
/* ... */
scanf("%f", &theta );
you must use
scanf("%lf", &theta );
to read a double
. f
conversion specifier is for reading float
.
Question
I'm writing a program in C that finds the time of flight and height at impact of a projectile and my program is compiling, but it's coming out with the wrong values for the solutions. Any idea what I might be doing wrong?
/*
*Finds final time and height of a projectile when it reaches the target
*/
#include <stdio.h> /* printf, scanf definitions */
#include <math.h> /* cos definition */
#define G 32.17 /* gravitational constant */
int
main(void)
{
double theta; /* input - angle(radians)of elevation */
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 prompts.\n\n");
/* Get angle, distance, velocity. */
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 );
/* Get time of flight */
time = distance / (velocity * cos(theta));
/*Get height at impact */
height = velocity * sin(theta) * time - ( (G * time*time) / 2.0 );
/* Display time of flight */
printf("The time of flight is %fs seconds.\n", time);
/* Display height at impact */
printf("The height at impact is %fm feet.\n", height);
system("pause");
return (0);
}
Solution
double theta;
/* ... */
scanf("%f", &theta );
you must use
scanf("%lf", &theta );
to read a double
. f
conversion specifier is for reading float
.