fork download
  1. // Nicolas Ruano CS1A Chapter 6, P. 370, #5
  2. /******************************************************************************
  3. * FUNCTION SOLVES FALLING OBJECT DISTANCE FOR 1 TO 10 SECONDS.
  4.  
  5. * _____________________________________________________________
  6.  
  7. * Program Function computes how far an object falls under
  8.  
  9. * gravity between 1 and 10 seconds using this formula:
  10.  
  11. * Computation is based on the formula:
  12.  
  13. *
  14.  
  15. * d = 1/2 * g * t^2
  16.  
  17. *
  18.  
  19. * Where:
  20.  
  21. * d = distance in meters
  22.  
  23. * g = acceleration due to gravity (9.8 m/s²)
  24.  
  25. * t = time in seconds
  26.  
  27. * ____________________________________________________________
  28.  
  29. * INPUT
  30.  
  31. * t -> int Time (seconds)
  32.  
  33. *
  34.  
  35. *
  36.  
  37. * OUTPUT
  38.  
  39. * d -> double Distance fallen (meters).
  40.  
  41. *******************************************************************************/
  42.  
  43.  
  44.  
  45. #include <iostream> // Needed for input and output
  46.  
  47. using namespace std;
  48.  
  49.  
  50.  
  51. // Function to calculate falling distance
  52.  
  53. double fallingDistance(int t) {
  54.  
  55. const double g = 9.8; // Acceleration due to gravity (m/s^2)
  56.  
  57. double d = 0.5 * g * t * t; // d = 1/2 * g * t^2
  58.  
  59. return d;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. int main() {
  66.  
  67. cout << "Time (s)\tDistance (m)\n";
  68.  
  69. cout << "---------------------------\n";
  70.  
  71.  
  72.  
  73. // Loop through time values from 1 to 10 seconds
  74.  
  75. for (int time = 1; time <= 10; time++) {
  76.  
  77. double distance = fallingDistance(time);
  78.  
  79. cout << time << "\t\t" << distance << endl;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. return 0;
  86.  
  87. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Time (s)	Distance (m)
---------------------------
1		4.9
2		19.6
3		44.1
4		78.4
5		122.5
6		176.4
7		240.1
8		313.6
9		396.9
10		490