This is a homework assignment. The program calculates the position (height) and velocity of a falling object. The max terminal velocity we are using is -500. The program outputs time, position, velocity for every second of fall. At Terminal velocity the position calc stops working and the program should subtract 500 from the previous position for position output. When the object reaches 1000 ft. the program should output one more position, etc and stop running. My problem is that I can't get the position to work properly at terminal velocity. If you run this enter 6000 for height and 50 for velocity as an example. After height 2226, the program should show three more outputs, the last being 726. The logic is escaping me.
//Program Description: gives height and velocity of falling object for every second it falls.
// Input: Initial height, Initial Velocity
// Calculates: height and velocity at every second of fall.
// Output: time, height, velocity very second. Stops outputting when height reaches 1000 ft.
#include <iostream>
#include <cmath> // for using pow function
#include <iomanip> // for formatting output
using namespace std;
// GLOBAL CONSTANTS
const double terminalVelocity = -500;
const double minHeight = 1000; // minimum height for input and output
/* Function position calculates height of object every second.
Input parameters: Initial height, Initial Velocity, time
Returns: position- height of object at given time */
double position (double initialHeight, double initialVelocity, double time)
{
// CONSTANT
const int multiplier = -16; // used in position equation
// variable
double position;
position = multiplier * pow(time, 2) + initialVelocity * time + initialHeight;
return position;
}
/* Function velocity calculates velocity of object every second.
Input parameters: Initial velocity, time
Returns: velocity of object at given time */
double velocity (double initialVelocity, double time)
{
const int multiplier = -32;
double velocity;
velocity = multiplier * time + initialVelocity;
if (velocity > terminalVelocity)
velocity = velocity;
else
velocity = terminalVelocity;
return velocity;
}
int main()
{
double initialHeight;
double initialVelocity;
int initialTime = 0;
int time; // variable to store time as it increments
// header
cout << "Program will calculate the position and velocity of a falling object" << endl << endl;
// input for initial height
cout << "Enter the initial height in feet (above 1000): ";
cin >> initialHeight;
while (initialHeight <= minHeight) { // validates input to make sure it is greater than 1000
cout << "Invalid answer. Must enter height over 1000. " << endl;
cout << "Enter the initial height in feet (above 1000): ";
cin >> initialHeight; }
// input for initial velocity
cout << endl << "Enter the initial velocity in ft.sec: ";
cin >> initialVelocity;
while (initialVelocity <= terminalVelocity) { // validates input to mkae sure it is greater than -500
cout << "Invalid answer. Velocity must be greater then terminal Velocity (-500). " << endl;
cout << "Enter the initial velocity in ft.sec: ";
cin >> initialVelocity; }
// while (initialHeight <= minHeight) { ... }//and
// while (initialVelocity <= terminalVelocity) { ... } //should probably both be
while( initialHeight < minHeight ) { ... } //and
while( initialVelocity < terminalVelocity ) { ... }
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
* The initial time should not change though out the program.
* Isn't that what your time variable is for?
*/
// outputs time, position, velocity from user-defined functions
time = initialTime; //start at t0do{
// cout << setw (4) << initialTime << setw (12) << position (initialHeight, initialVelocity, initialTime) << setw (12) << velocity (initialVelocity, initialTime) << endl;
cout << setw (4) << time << setw (12) << position (initialHeight, initialVelocity, time) << setw (12) << velocity (initialVelocity, time) << endl;
// time = initialTime++;
time++;
} // while (position (initialHeight, initialVelocity, initialTime) >= 1000);
while(position (initialHeight, initialVelocity, time) >= minHeight);