fork download
  1. //Ryan Shahriyarpour CS1A Carl Argila CH6 HW Q:5
  2. //
  3. /*****************************************************************************
  4.  *
  5.  * Falling Distance
  6.  *
  7.  *********************
  8.  *
  9.  * This program displays the distance an object falls because of gravity for
  10.  * 1 through 10 seconds. The function fallingDistance is called in a loop.
  11.  * The formula is:
  12.  * d = (1/2) * g * t^2 (g = 9.8 m/s^2)
  13.  *
  14.  * INPUT
  15.  * None (fixed values 1 through 10)
  16.  *
  17.  * OUTPUT
  18.  * Table showing time in seconds and distance fallen in meters
  19.  *
  20.  ****************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. // Function prototype
  27. double fallingDistance(int seconds);
  28.  
  29. const double GRAVITY = 9.8; // Acceleration due to gravity (m/s^2)
  30.  
  31. int main()
  32. {
  33. cout << "Time (sec)\t\tDistance Fallen (m)\n";
  34. cout << "---------------------------------------\n";
  35.  
  36. // Pass the values 1 through 10 to fallingDistance
  37. for (int time = 1; time <= 10; time++)
  38. {
  39. cout << time << "\t\t\t" << fixed << setprecision(2)
  40. << fallingDistance(time) << endl;
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46. // Accepts the falling time in seconds and returns the distance in meters
  47. double fallingDistance(int seconds)
  48. {
  49. return 0.5 * GRAVITY * seconds * seconds;
  50. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Time (sec)		Distance Fallen (m)
---------------------------------------
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