I was wonder how this code looks to you guys. Here is what I have to accomplish and following it is my code with the output. The output is correct but I was wondering if there is a way to clean it up some. (I know there probably is some way I haven't learned yet.)
But if it looks good, just let me know! Thanks!
ObjectiveQuote
Option 2: Suppose Dave drops a watermelon off a high bridge and lets it fall until it hits the water. If we neglect air resistance, then the distance d in meters fallen by the watermelon after t seconds is d = 0.5 * g * t2, where the acceleration of gravity g = 9.8 meters/second2. Write a program that asks the user to input the number of seconds that the watermelon falls and the height h of the bridge above the water. The program should then calculate the distance fallen for each second from t = 0 until the value of t input by the user. If the total distance fallen is greater than the height of the bridge, then the program should tell the user that the distance fallen is not valid.
Code
Sample Run 1:
Please input the time of fall in seconds:
2
Please input the height of the bridge in meters:
100
Time Falling (seconds) Distance Fallen (meters)
************************************************
0 0
1 4.9
2 19.6
Sample Run 2:
Please input the time of fall in seconds:
4
Please input the height of the bridge in meters:
50
Time Falling (seconds) Distance Fallen (meters)
************************************************
0 0
1 4.9
2 19.6
3 44.1
4 78.4
Warning-Bad Data: The distance fallen exceeds the height of the bridge
Here is my code with the output.Code
// This program will calculate the the time it takes for a watermelon to fall from the top of a high bridge.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const float gravity = (9.8 / pow(1, 2)); // The constant variable gravity is the formula of 9.8 meters over time squared.
int height, time; // The height of the bridge. The time in seconds it takes for the watermelon to fall.
float distance; // The distance the watermelon falls per second.
cout << fixed << showpoint << setprecision(2); // Formatting the output to display two point precision.
cout << "Enter the height of the bridge in meters e.g. 100.\n";
cin >> height; // User inputs height of the bridge.
cout << "\nEnter the number of seconds the watermelon falls.\n";
cin >> time; // User inputs the maximum time in seconds.
cout << endl << endl;
cout << " Seconds Elapsed \t Distance in meters\n\n";
for (int t = 0; t <= time; t++) // t is the number of seconds that has elapsed.
{
distance = (0.5 * gravity * pow(t, 2)); // Calculated the distance of the watermelon traveled over the number of seconds elapsed.
cout << "\t" << t << "\t\t\t" << distance << endl;
}
if (distance > height) // Trailing if statement to catch any errors input by the user.
cout << "\nWarning-Bad Data: The distance fallen exceeds the hight of the bridge.\n\n";
return 0;
}
Output 1
Code
Enter the height of the bridge in meters e.g. 100.
100
Enter the number of seconds the watermelon falls.
2
Seconds Elapsed Distance in meters
0 0.00
1 4.90
2 19.60
Output 2 (Shows error)
Code
Enter the height of the bridge in meters e.g. 100.
100
Enter the number of seconds the watermelon falls.
5
Seconds Elapsed Distance in meters
0 0.00
1 4.90
2 19.60
3 44.10
4 78.40
5 122.50
Warning-Bad Data: The distance fallen exceeds the hight of the bridge.