// lem.cxx // Written by Michael Main, mostly to show how to use functions. #include #include #include using namespace std; const double AG = -9.8/6; // Acceleration from lunar gravity m/sec^2 const double AT = 10.0; // Thrust upward in m/sec^2; const double DELTA = 0.1; // How much time passes each iteration // FUNCTION PROTOTYPES double calc_height(double height, double velocity); // The value calculated by this function is the new height of the // lander after DELTA seconds have passed. The two parameters are // the height and velocity of the lander just before the time passed. int main( ) { double height; // Height in meters above lunar surface double velocity; // Current velocity m/sec (negative is down) bool is_thrust; // True if the thruster is on // Get the initial data cout << "The Lunar Lander program" << endl; cout << "Type the initial height (m) and velocity (m/sec): "; cin >> height >> velocity; is_thrust = false; // Print a heading: cout << " HEIGHT VELOCITY T" << endl; while (height > 0) { // Update the variables to reflect the passage of time new_height = calc_height(height, velocity); // Print the current variable values cout << setw(10) << height; cout << setw(10) << velocity; cout << setw(2) << is_thrust << endl; } return EXIT_SUCCESS; } double calc_height(double height, double velocity) { return (height + velocity * DELTA); }