PDA

View Full Version : C++ help: height/distance of cannon ball?

Josh
Feb 16, 2006, 09:25 PM
I have to write a program that calculates the height and distance of a cannon ball over time.

The velocity and the angle are defined by the user, and time is increased in whole-second intervals.

I've wrote it, but seem to be having trouble with my formulas.

For the distance, I have: d = v*cos(angle)*time

for height, I have: h = (v*sin(angle) - (9.8*time^2))

Here is my code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
// declare the variables

float angle,velocity;
float time,distance,height;
float gravity;

gravity = 9.80665;

// begin program and ask for user input

cout << "Cannon Trajectory Program" << endl << endl;
cout << "Enter the velocity (0-100): ";
cin >> velocity;
cout << "Enter the angle (0-90): ";
cin >> angle;
cout << endl << endl;

cout << " Time Distance Height" << endl;
cout << "------ ---------- --------" << endl;

// calculate results and display w/ loop

time = 0;
height = 0;
distance = 0;

while(height >= 0)
{
cout << "[" << time << "] [" << distance << "] [" << height << "]" << endl;
distance = velocity * cos(angle) * time;
height = (velocity * sin(angle)) - (gravity * pow(time,2));

time++;

};

return 0;
}

(I'm not worried about the format and iomanip until I get the values correct).

My formulas must be wrong, because the first 3 values, when velocity is 100, and the angle is 45, should be:

[0] [0.0000] [0.0000]
[1] [70.710] [65.720]
[2] [141.42] [121.46]
[3] [212.13] [167.22]

Any help, tips, or pointers is greatly appreciated.

zimv20
Feb 16, 2006, 09:42 PM
are you asking if you're using the correct formulas or if you've programmed those formulas correctly?

HexMonkey
Feb 16, 2006, 09:53 PM
There are a few bugs in your code, once fixed the results it gives are approximately the same as the expected results.

Here are the bugs I found:

You need to increment time before calculating the new distance and height, not after. In your existing code, you are calculating the new distance and height, with the old time, so you get a distance of 0 in the second iteration.
The cos and sin functions use radians, not degrees, so you need to convert angle to radians.
You are using the wrong formula for height. You are using d = v + at^2 (gravity is a negative acceleration), but you should be using d = vt + 1/2 * at^2

After making these changes, here's the code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
// declare the variables

float angle,velocity;
float time,distance,height;
float gravity;

gravity = 9.80665;

// begin program and ask for user input

cout << "Cannon Trajectory Program" << endl << endl;
cout << "Enter the velocity (0-100): ";
cin >> velocity;
cout << "Enter the angle (0-90): ";
cin >> angle;
cout << endl << endl;

cout << " Time Distance Height" << endl;
cout << "------ ---------- --------" << endl;

// calculate results and display w/ loop

time = 0;
height = 0;
distance = 0;
angle *= 3.1415926536 / 180; //Convert to radians

while(height >= 0)
{
cout << "[" << time << "] [" << distance << "] [" << height << "]" << endl;

time++;
distance = velocity * cos(angle) * time;
height = (velocity * sin(angle) * time) - (gravity * pow(time,2)) / 2;

};

return 0;
}

Here's the output I get for the first 4 iterations after making these changes (with velocity=100 and angle=45):

[0] [0] [0]
[1] [70.7107] [65.8074]
[2] [141.421] [121.808]
[3] [212.132] [168.002]

Josh
Feb 16, 2006, 10:38 PM
Thanks a *ton* hexmonkey!

I was thinking about the height formula on my way to a friends house, when it struck me what I was doing lol.

I didn't know the sin/cos functions require radians; I'd never have thought of that.

Thanks again, I really, really, appreciate your help.