d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > In Need Of A Bit Of Guidance
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 14 2014 10:13pm
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!

Objective

Quote
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.

Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Mar 15 2014 09:07am
Spelled height wrong.

:P
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 15 2014 02:17pm
just some random thoughts, all meaningless stuff:

because of your constants and your restrictions to inputs be integers, the answer will never have more than one decimal point, so no need to print the extra 0
I don't why you need this division... "9.8 / pow(1, 2)"
because X/(1^2)= X

and for total cheese:
the output of these calculates never change, so unless your planning on making an option later to change the gravitational constant (watermelons on mars or something) you could just store the results and print them :evil:
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 15 2014 03:28pm
Quote (Azrad @ 15 Mar 2014 15:17)
just some random thoughts, all meaningless stuff:

because of your constants and your restrictions to inputs be integers, the answer will never have more than one decimal point, so no need to print the extra 0
I don't why you need this division... "9.8 / pow(1, 2)"
because X/(1^2)= X

and for total cheese:
the output of these calculates never change, so unless your planning on making an option later to change the gravitational constant (watermelons on mars or something) you could just store the results and print them  :evil:


I know. The second squared made no sense to me but the instructions said use it. :P I don't make the rules, I just follow them. Haha

I was going to pick the easy option that requires sentinel values but I thought a loop and math would be fun. It was a challenge to write it at first but I figured out what I had did wrong.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll