fork download
  1. //Andrew Alspaugh CS1A Chapter 6 P. 370 # 5
  2. //
  3. /**********************************************************************
  4.  *
  5.  * Calculate Distance Fallen
  6.  * ____________________________________________________________________
  7.  * This program displays a table of time of travel and distance travled.
  8.  *
  9.  * The purpose of the program is to showcase the function named
  10.  * fallingDistance() Which uses a time input to calculate the distance
  11.  * fallen.
  12.  *
  13.  * NOTE ABOUT PROCESS
  14.  * The Process contains the output in this program because the programming
  15.  * challenge asks that you display the use of the function by creating
  16.  * a loop that uses the function to create the OUTPUT. Therefore, the
  17.  * "for loop" contains both the process and output of this program
  18.  * ____________________________________________________________________
  19.  * INPUT:
  20.  * In this program There are no input variables, the number of
  21.  * iterations for the loop are fixed and do not need inputs.
  22.  *
  23.  * In normal computer programming, the only input used would be
  24.  * t :Time of Distance Falling
  25.  *
  26.  * OUTPUT
  27.  * d :Calculated Distance Object has Fallen
  28.  *
  29.  *
  30.  *********************************************************************/
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. //Function Prototype fallingDistance()
  35. double fallingDistance(double t);
  36.  
  37. int main() {
  38.  
  39. //Data Dictionary
  40. const float g = 9.8; //Constant Rate of Gravity
  41. double t; //Input Time in Seconds
  42. double d; //Output Distance Fallen in Meters
  43. int Start = 1; //Start Time loop
  44. int End = 10; //End Time loop
  45.  
  46. //INPUT
  47. // NO CIN: INPUTS ARE FIXED IN LOOP FROM 1-10//
  48.  
  49. //PROCESS AND OUTPUT
  50.  
  51. for(t = Start; t <= End; t++)
  52. {
  53. //PROCESS//
  54. double d = fallingDistance(t);
  55.  
  56. //OUTPUT//
  57. cout << "Time is: " << t << " Distance is " << d << endl;
  58. }
  59.  
  60. //OUTPUT
  61. //NO COUT: OUTPUTS ARE PART OF PROCESS IN LOOP
  62. return 0;
  63. }
  64.  
  65. //Function Definition fallingDistance
  66. //takes any time input and uses gravity constant to solve for distance fallen
  67. //In this program the time input "t" is fixed in the "for loop" however this
  68. //function allows for any "t" input as long as it is passed into the function
  69.  
  70. double fallingDistance(double t)
  71. {
  72. const float g = 9.8;
  73. return 0.5*g*(t*t);
  74. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Time is: 1     Distance is 4.9
Time is: 2     Distance is 19.6
Time is: 3     Distance is 44.1
Time is: 4     Distance is 78.4
Time is: 5     Distance is 122.5
Time is: 6     Distance is 176.4
Time is: 7     Distance is 240.1
Time is: 8     Distance is 313.6
Time is: 9     Distance is 396.9
Time is: 10     Distance is 490