fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 6, P. 370, #5
  3. //
  4. /****************************************************************
  5.   *
  6.   * COMPUTE FALLING DISTANCE
  7.   *_______________________________________________________________
  8.   * This program computes the distance an object falls due to
  9.   * gravity. It uses a function to calculate the distance for
  10.   * each second from 1 through 10.
  11.   *
  12.   * Computation is based on the formula:
  13.   * Distance = 0.5 * Gravity * Time * Time
  14.   * Gravity = 9.8 meters per second squared
  15.   *_______________________________________________________________
  16.   * INPUT
  17.   * time : Time the object has been falling in seconds
  18.   *
  19.   * OUTPUT
  20.   * distance : Distance the object falls in meters
  21.   *
  22.   ****************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. // Function prototype
  29. double fallingDistance(double);
  30.  
  31. int main()
  32. {
  33. int time; //INPUT - Falling time in seconds
  34. double distance; //OUTPUT - Falling distance in meters
  35.  
  36. // Display table heading
  37. cout << "Time (seconds) Distance (meters)" << endl;
  38. cout << "------------------------------------" << endl;
  39.  
  40. // Calculate falling distance for 1 through 10 seconds
  41. for (time = 1; time <= 10; time++)
  42. {
  43. // Call function to calculate distance
  44. distance = fallingDistance(time);
  45.  
  46. // Display the time and falling distance
  47. cout << fixed << setprecision(2);
  48. cout << setw(7) << time
  49. << setw(23) << distance << endl;
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55. // Function definition
  56. // Calculates and returns the falling distance
  57. double fallingDistance(double time)
  58. {
  59. const double GRAVITY = 9.8; // Gravity in meters per second squared
  60. double distance; // Falling distance in meters
  61.  
  62. // Calculate the falling distance
  63. distance = 0.5 * GRAVITY * time * time;
  64.  
  65. return distance;
  66. }
  67.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Time (seconds)    Distance (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