Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 16, 2006, 10:25 PM   #1
Josh
macrumors 68000
 
Josh's Avatar
 
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 <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;

// 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
Josh is offline   0 Reply With Quote
Old Feb 16, 2006, 10:42 PM   #2
zimv20
macrumors 601
 
zimv20's Avatar
 
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
zimv20 is offline   0 Reply With Quote
Old Feb 16, 2006, 10:53 PM   #3
HexMonkey
Administrator
 
HexMonkey's Avatar
 
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 <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;

// 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]
HexMonkey is offline   0 Reply With Quote
Old Feb 16, 2006, 11:38 PM   #4
Josh
Thread Starter
macrumors 68000
 
Josh's Avatar
 
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
Josh is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Cannon iP90v not work with new Retina 4mylifetime OS X Mavericks (10.9) 0 Oct 31, 2013 06:44 PM
Ball-Hop Anniversary Edition - FREE Skee-ball game conedog iPhone and iPod touch Apps 0 Jun 25, 2013 05:59 PM
Now that's a BIG gun. Cannon more like. PracticalMac Wasteland 1 Feb 4, 2013 01:50 PM
Rode VideoMic Issues With Cannon t3i. Help Please! videomaker12 Digital Video 2 Sep 10, 2012 10:43 AM
Cannon MP600 moninskl Mac Peripherals 3 Jun 19, 2012 05:50 AM

Forum Jump

All times are GMT -5. The time now is 08:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC