/* ------------------------------------------------------------------------ // Figure 3.9: An action with an alternative. // ------------------------------------------------------------------------ // 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 time value is checked for being positive. */ #include #define GRAVITY 9.81 /* gravitational acceleration (meters/seconds) */ 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 valid data */ y = .5 * GRAVITY * t * t; /* calculate the 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 ); } else { /* otherwise, */ puts( " Try again; input must be a positive number." ); } puts( " Normal termination.\n" ); }