/* --------------------------------------------------------------------- // Figure 3.10: Asking more than one question. // --------------------------------------------------------------------- */ /* This program will determine the velocity and distance travelled by a */ /* grapefruit with no initial velocity, t seconds after being dropped */ /* from a building. Input value must be between 0 and MAX seconds. */ #include #define GRAVITY 9.81 /* gravitational acceleration (meters/seconds) */ #define MAX 60 /* Upper bound on time of fall. */ void main( void ) { double t; /* elapsed time during fall (s) */ double y; /* distance of fall (m) */ double v; /* final velocity (m/s) */ printf( "\n\n Welcome.\n" " Calculate the height from which a grapefruit fell\n" " given the number of seconds that it was falling.\n\n" ); printf( " Input seconds: " ); /* Prompt for the time, in seconds. */ scanf( "%lg", &t ); /* keyboard input for time */ if (t < 0) { /* check for negative input */ printf( " Error: time must be positive.\n\n" ); } else if (t > MAX) { /* Is time value too big? */ printf( " Error: time must be <= %i seconds.\n\n", MAX ); } else { /* Input is valid; calculate distance and velocity of the fall */ y = .5 * GRAVITY * t * t; /* calculate distance of the fall */ v = GRAVITY * t; /* velocity of grapefruit at impact */ printf( " Time of fall = %g seconds \n", t ); printf( " Distance of fall = %g meters\n", y ); printf( " Velocity of the object = %g m/s \n", v ); } puts( " Normal termination." ); }