MacRumors Forums C++ help: height/distance of cannon ball?

 Feb 16, 2006, 09:25 PM #1 Josh macrumors 68000     Join Date: Mar 2004 Location: State College, PA C++ help: height/distance of cannon ball? 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: Code: ```#include #include #include 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; // output column headers 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. Thanks in advance! __________________ "Many go fishing all their lives without knowing that it is not fish they are after." - Thoreau 0
 Feb 16, 2006, 09:42 PM #2 zimv20 macrumors 601     Join Date: Jul 2002 Location: chicago are you asking if you're using the correct formulas or if you've programmed those formulas correctly? __________________ Oct 2011: check out my band's first album @ boxsetauthentic.com 0
 Feb 16, 2006, 09:53 PM #3 HexMonkey Administrator     Join Date: Feb 2004 Location: New Zealand 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: Code: ```#include #include #include 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; // output column headers 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] 0
 Feb 16, 2006, 10:38 PM #4 Josh Thread Starter macrumors 68000     Join Date: Mar 2004 Location: State College, PA 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. __________________ "Many go fishing all their lives without knowing that it is not fish they are after." - Thoreau 0

 MacRumors Forums