C++ Program: Dealing with Distance and Gravity Equations.

Oct 26, 2008 at 4:16am
Hi, I"m a beginner at C++. This is my first semester at college.

And I am having so much trouble with this program. Can someone please help me???

Thank you in advance,

sandy

Here is the assignment:

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 * t squared., where the acceleration of gravity g = 9.8 meters / second squared. 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 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.

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.

If anyone knows how to do this: please message me... leave a comment or something.....

-thanks-

sandy








Oct 26, 2008 at 5:01am
You seem to know the formula...so it should be just a matter of making a loop that inputs values into the formula and displays it...
Oct 26, 2008 at 5:13pm
well, I have tried everything....

Here is the code i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

	 double time;
	 const double GRAVITY = 9.8 / time;
	
int main()
{
	int i;
	//int height;
	double distance=0;
	double fallingDistance;

		
	while(time != -1)
	{

		cout << "Please input the time of fall in seconds: ";
		cin >> time;

		cout << "Please input the height of the bridge in meters: ";
		cin >> fallingDistance;

		
	for(i=0; i<time; i++)
	{
		time = i + 1.0;
		cout << "Given gravity of, " << GRAVITY << " meters per second squared, an object will fall " 
             << distance << " meters in " << time << " seconds." << endl;
	}

		distance = 1/2 * GRAVITY * pow(time,2);
		//distance = (0.5 * gravity * (seconds * seconds));



		cout << "Distance: " << distance << endl;

		if (distance > fallingDistance)
		{
			cout << " Warning Bad Data: The distance Fallen exceeds the height of the bridge. ";
		}


	}



	system("pause");
	return 0;

}


Oct 26, 2008 at 6:13pm
Last edited on Oct 26, 2008 at 6:14pm
Oct 26, 2008 at 6:19pm
when you define GRAVITY=9.8/time, time hasn't been initialized yet
shouldn't GRAVITY just be 9.8?
and you try to output distance in your for loop, but distance hasn't been calculated yet.
See if you understand how this for loop operates, and how distance is different from totalDistance.
1
2
3
4
5
6
7
8


for(i=1; i<=time; i++)
	{
		distance = .5 * GRAVITY * pow(time,2);
		totalDistance += distance;
		
	}

Last edited on Oct 26, 2008 at 6:34pm
Oct 26, 2008 at 10:14pm
Okay - I tried that. And yes you are probably right about the GRAVITY should only equal the 9.8.

so i guess i'll have to re-define the equation somewhere else? so it calculates the correct gravity???

also, when i run the program I'm in a infinite loop! it looks messy and it just won't stop calculating like it keeps going and going.....

:(
Oct 27, 2008 at 12:46am
The code you posted above shouldn't go into an infinite loop.
Post what you have now.
Oct 27, 2008 at 3:16am
Thank you for helping me. I figured it out tho! :) yay!

If you would like the code, just e-mail me at sburre1@gmail.com. cause I check that account more often.

-sandy
Topic archived. No new replies allowed.