/* --------------------------------------------------------------------- // Figure 7.28: A projectile. // Mixing floats and doubles in a computation. // --------------------------------------------------------------------- */ #include "tools.h" void main( void ) { int alpha; /* angle of ball from horizontal */ float vel_o; /* initial velocity in m/s */ float x; /* horizontal dimension of travel */ double vx, vy; /* x and y components of velocity */ double a_rad; /* angle alpha, in radians */ double t; /* time in seconds for ball to hit ground */ banner(); printf( "\n Enter the initial velocity (m/s): " ); scanf( "%g", &vel_o ); printf( " Enter firing angle from the horizontal, in degrees: " ); scanf( "%i", &alpha ); if (alpha < 0 || alpha > 90) /* check for bad firing angle */ printf( "Error: Firing angle must be in range 0...90 degrees." ); else if (vel_o < 0 || vel_o > 100) /* check for bad velocity */ printf( "Error: Initial velocity must be in range 0...100 m/s." ); else { /* inputs are valid */ a_rad = alpha * PI / 180.; /* convert angle to radians. */ vx = vel_o * cos( a_rad ); vy = vel_o * sin( a_rad ); t = 2 * vy / GRAVITY; /* time of fall */ x = vx * t; /* distance of travel */ printf( "\n For initial velocity = %.3f m/s" "\n and angle from the horizontal = %i degrees" "\n The ball travels %.3f meters \n", vel_o, alpha, x ); bye(); } }