Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. //Declaring the functions
  9. float enter_data(const string &);
  10.  
  11. double height_calc(const float &, const int &, const float &);
  12.  
  13. void display_header(const string &, const string &);
  14.  
  15. void display_results(const float &, const double &);
  16.  
  17. int main() {
  18.     const float gravity_ms2 = 9.8;
  19.  
  20.     // Declaring variables
  21.     const string desc_1 = "Time(seconds)";
  22.     const string desc_2 = "Height(Meter)";
  23.  
  24.     // Calling the enter_data function to ask for the initial velocity
  25.     const float in_vel = enter_data("Enter the Initial Velocity in Meters/Second: ");
  26.  
  27.     display_header(desc_1, desc_2);
  28.  
  29.     double current_height = 0;
  30.     int seconds = 0;
  31.     do {
  32.         current_height = height_calc(in_vel, seconds, gravity_ms2);
  33.  
  34.         display_results(seconds, current_height);
  35.  
  36.         seconds += 1;
  37.     } while (current_height >= 0);
  38.  
  39.     return 0;
  40. }
  41.  
  42. float enter_data(const string &prompt)//Outputting the prompt and storring as in_vel
  43. {
  44.     float in_vel;
  45.  
  46.     cout << prompt << endl; //Asking for an the input value
  47.     cin >> in_vel;//Setting input value as in_vel
  48.  
  49.     return in_vel;//returning to in_vel in int main
  50.  
  51. }
  52.  
  53. double height_calc(const float &in_vel, const int &seconds, const float &gravity_ms2) {
  54.     return ((in_vel * seconds) - (.5 * gravity_ms2 * pow(seconds, 2)));
  55. }
  56.  
  57. void display_header(const string &desc_1, const string &desc_2) {
  58.     //Displaying the headers
  59.     cout << setw(1) << desc_1 << setw(20) << desc_2 << endl;
  60. }
  61.  
  62. void display_results(const float &seconds, const double &height) {
  63.     //Displaying the values for both time and height every second
  64.     cout << setw(1) << setprecision(4) << seconds << setw(20) << height << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement