fork download
  1. // Marvyn Jesse De La Torre Chapter 6, p. 370, #5
  2. //
  3. /*************************************************************************
  4.  *
  5.  * CALCULATE FALLING DISTANCE
  6.  * This program calculates the distance an object falls during each second
  7.  * from 1 through 10.
  8.  *
  9.  * INPUT
  10.  * seconds : Time an object has been falling, from 1 through 10
  11.  *
  12.  * OUTPUT
  13.  * distance : Distance fallen in meters
  14.  *************************************************************************/
  15.  
  16. #include <iomanip>
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. const double GRAVITY = 9.8; // Gravitational acceleration in meters/second^2
  21.  
  22. // Function Prototype
  23. double fallingDistance(double seconds);
  24.  
  25. int main()
  26. {
  27. // Declare Variables
  28. int seconds; // Number of seconds the object has fallen
  29.  
  30. // Display Table Heading
  31. cout << fixed << setprecision(2);
  32. cout << "Time (seconds)\tDistance Fallen (meters)" << endl;
  33. cout << "---------------------------------------" << endl;
  34.  
  35. // Display the distance for 1 through 10 seconds
  36. for (seconds = 1; seconds <= 10; seconds++)
  37. {
  38. cout << setw(7) << seconds << setw(23)
  39. << fallingDistance(seconds) << endl;
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. /*************************************************************************
  46.  * fallingDistance
  47.  * This function returns the distance, in meters, an object falls in the
  48.  * specified number of seconds.
  49.  *************************************************************************/
  50. double fallingDistance(double seconds)
  51. {
  52. return 0.5 * GRAVITY * seconds * seconds;
  53. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Time (seconds)	Distance Fallen (meters)
---------------------------------------
      1                   4.90
      2                  19.60
      3                  44.10
      4                  78.40
      5                 122.50
      6                 176.40
      7                 240.10
      8                 313.60
      9                 396.90
     10                 490.00