// File: balldrop.cpp (used in Lecture 11)
// Displays the height of a ball dropped from a building
// at user-specified intervals, until it hits the ground

#include <iostream>   // cin, cout, endl, <<, >>
#include <cmath>      // pow
using namespace std;

const double G = 9.80665;  // Earth's gravitational constant in m/s/s

int main ()
{
   double ballHeight,         // height of the ball
          buildingHeight,     // height of the building
          time,               // elapsed time
          deltaT;             // time interval

   // Prompt and read building height and time interval
   cout << "Enter building height in meters: ";
   cin >> buildingHeight;
   cout << "Enter time in seconds between table lines: ";
   cin >> deltaT;

   // Set formatting
   cout.setf(ios::showpoint);
   cout.setf(ios::fixed);
   cout.precision (2);

   // Initialize time and ball height
   time = 0.0;
   ballHeight = buildingHeight;

   // Print table headings
   cout << "\n      Time    Height\n\n";

   // Display ball height until it hits the ground
   while (ballHeight > 0.0)
   {
      cout << "      " << time << "    " << ballHeight << endl;
      time += deltaT;
      ballHeight = buildingHeight - 0.5 * G * pow (time, 2);
   }  // end while

   // Ball hits the ground
   cout << "\nSPLAT!!\n\n";

   return 0;
}  // end main
