/* ------------------------------------------------------------------------- // Figure 6.12 Repeating a calculation. // Figure 6.13 The work() and drop() functions. // ------------------------------------------------------------------------- // Determine the time it takes for a grapefruit to hit the ground when // it is dropped from a helicopter hovering at height h, with no initial // velocity. Also determine the velocity of the fruit at impact. Use a // work function to perform the computations and output results. */ #include "tools.h" /* #include commands and user-defined utilities */ void work( void ); double drop( double height ); void main( void ) { int do_it_again; /* repeat-or-stop switch */ banner(); puts( "\n Calculate the time it would take for a grapefruit\n" " to fall from a helicopter at a given height.\n" ); do { work(); printf( " \n\n Enter '1' to continue or '0' to quit: " ); scanf( "%i", &do_it_again ); } while (do_it_again != 0); bye(); } /* ------------------------------------------------------------------------ // Perform one gravity calculation and print the results. */ void work( void ) { double h; /* height of fall (m) */ double t; /* time of fall (s) */ double v; /* terminal velocity (m/s) */ printf (" Enter height of helicopter (meters): " ); scanf( "%lg", &h ); t = drop( h ); /* calculate duration of fall. */ v = GRAVITY * t; /* velocity of grapefruit at impact */ printf( " Time of fall = %g seconds\n", t ); printf( " Velocity of the object = %g m/s\n", v ); } /* ------------------------------------------------------------------------ // Calculate time of fall from a given height. */ double drop (double height) { if (height < 0) return 0; else return sqrt( 2 * height / GRAVITY ); } /* ----------------------------------------------------------------- */