fork download
  1. //Matthew Santo CS1A Ch. 6, Pg. 370, #5
  2. /***********************************************
  3.  *
  4.  * SIMULATE FALLING DISTANCE
  5.  * _____________________________________________
  6.  * Simulates falling distance based off the
  7.  * objects falling time, which in this case
  8.  * is 1-10 seconds.
  9.  * _____________________________________________
  10.  * INPUT
  11.  * N/A
  12.  *
  13.  * OUTPUT
  14.  * Falling distance simulation
  15.  ***********************************************/
  16. #include <iostream>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. double fallingDistance(double);
  21.  
  22. int main() {
  23.  
  24. //Initialize variable
  25. double distance;
  26.  
  27. cout << fixed << setprecision(2);
  28. cout << "Time (s)\tDistance (m)\n";
  29. cout << "---------------------------\n";
  30.  
  31. // Loop from 1 to 10 seconds
  32. for (int t = 1; t <= 10; t++) {
  33. double distance = fallingDistance(t);
  34. cout << setw(5) << t << "\t\t" << setw(10) << distance << endl;
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40. //Calculates distance
  41. double fallingDistance(double time)
  42. {
  43. const double g = 9.8;
  44. double distance = 0.5 * g * time * time;
  45. return distance;
  46. }
Success #stdin #stdout 0.01s 5288KB
stdin
9
stdout
Time (s)	Distance (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