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:
(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!
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!