My project for my C++ class is to develop an application that will help me out in my programming life, and since I constantly have to use a calculator to determine the values in free fall acceleration I decided to write an application to do it for me. I was wondering if someone here could give me a quick inspection of my code to make sure it's all correct. It runs fine and there are no syntax errors what-so-ever, but I'm not 100 percent sure the math is right.

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
	float startPosY = 0;
	const float stopY = 0;
	float Acceleration = 0, InitialAcc = 0;
	float posY = 0;
	int sElapsed = 1;

	cout << "Please input the starting height of free fall in meters: ";
	cin >> startPosY;
	posY = startPosY;

	cout << "Please input the acceleration of the object in m/s: ";
	cin >> InitialAcc;
	Acceleration = InitialAcc;

	while (posY > 0)
	{
		posY -= Acceleration;
		Acceleration += InitialAcc;

		if (posY >= 0)
		{
			cout << "Current height: " << posY << " meters\nTime Elapsed: " << sElapsed << " seconds.\n\n";
			sElapsed++;
		}

		else
		{
			posY = 0;
			cout << "Current height: " << posY << " meters.\nTime Elapsed: " << sElapsed << " seconds.\n";
		}

		Sleep(1000);
	}

	cout << "\n\nThe object fell: " << startPosY << " meters in " << sElapsed << " seconds with a final acceleration of: " << Acceleration << "m/s\n";
	system("pause");
}

Thanks in advance,
Jamie

It runs fine and there are no syntax errors what-so-ever, but I'm not 100 percent sure the math is right.

Then whip out your calculator and test it.
Pick several cases form short to tall height and a single acceleration, calculate it old-school and see if your program agrees.

Then do the same for various accelerations holding the height constant.

Be sure to also test limits like height=0, acc=0, and the like.

Something's wrong somewhere and I'm not entirely sure where. It seems like it SHOULD WORK, but SHOULD WORK rarely works the way it should.

Have a look at this:
http://en.wikipedia.org/wiki/Equations_of_motion#Applications
(I don't know why they picked the variables they did, but they did.)
A free fall object at point s reaches a final velocity of u after t seconds.
To find u, take the square root of s divided by 2 times the Gravity constant (acceleration).
The code looks like u=sqrt(s/(2*G));
Divide u by your G to get your time.

The main thing that's giving you trouble is confusing velocity and acceleration. As Galileo famously showed, the acceleration of an object in free-fall is constant (since you're not accounting for air resistance here), so I think that pretty much everywhere where you have acceleration, you mean velocity. The one place where you do need the acceleration is when you're increasing the velocity a little bit in each cycle of the loop (which is basically the definition of acceleration), you've used your initialAcc variable for this. This should just be called acceleration and set to [tex]$9.81 \mathrm{ms^{-2}}$[/tex]. However, there are two problems with the way you have it here. Firstly, the change in velocity is given by:
[tex]$v_i = v_{i - 1} + a\Delta t$[/tex],
where [tex]v_i[/tex] is the new velocity and [tex]v_{i-1}[/tex] is the velocity from the previous cycle. So, you need to multiply the acceleration by your chosen time-step, [tex]\Delta t[/tex], to get the value that you need to add to the velocity each cycle. This leads to the second problem, you have an integer time-step (of 1 second). I corrected your program and ran it, I get this answer:

Current height: 10 meters, Time Elapsed: 50 ms.
Current height: 9.5095 meters, Time Elapsed: 100 ms.
Current height: 8.5285 meters, Time Elapsed: 150 ms.
Current height: 7.057 meters, Time Elapsed: 200 ms.
Current height: 5.095 meters, Time Elapsed: 250 ms.
Current height: 2.6425 meters, Time Elapsed: 300 ms.
Current height: 0 meters, Time Elapsed: 300 ms.

The object fell: 10 meters in 0.3 seconds with a final velocity of: 3.4335m/s

As you can see, the object only takes 0.3 seconds to get to the floor, so make your time-step a float too, and make it fairly small (I have it at 0.05 seconds here, as you can see)

I hope that helps a little :)

@ravenous: Yeah, I noticed that too, but got caught up in dusting off the math brain cells that I forgot to say anything about the "acceleration" and "initial acceleration" bits.
And I modified my copy to use doubles. I also neglected to mention that.
And nice use of Latex. I completely forgot this board has that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.