### hello everyone, i need some quick help

hello everyone im doing this assignment for my class. i need to find the height, distance, and time of a projectile thrown. the user has to input the angle and initial velocity. i have nearly got the code finished. however when i debug my code i keep receiving the height as a wrong value. the distance and time are both correct however not the height.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````double time2 (double v, double a) { double t; v=v*2; t = v*sin (a)/9.8;// 9.8 equals gravity return t; } double distance2 (double v, double a) { double d; a=a*2; d = pow(v,2)*sin (a)/9.8; return d; } double height2 (double v, double a) { double h; v=pow(v,2); h = v*sin(a)/9.8; return h; } int main() { double v=0, a=0, t=0, h=0, d=0; const float PI = 3.145; // const of pi cout<<"This program will calculate the distance, time, and height\n"; cout<<"of a projectile fired.\n"; cout<<"Please enter the angle and speed of the projectile to\n"; cout<<"calculate the values of distance, time, and height.\n"; cout<<"Angle: "; cin>>a; cout<<"Speed: "; cin>>v; a=a*(PI/180); //degrees into radian t = time2(v,a); cout<<"Time that the projectile stayed in the air is: " << t << endl; d = distance2(v,a); cout<<"the distance the projectile traveled is: " << d << endl; h = height2(v,a); cout<<"the height that the projectile reached is: " << h << endl; system("pause"); }``````
closed account (3CXz8vqX)
Bodmas....

Make sure you're operations are in appropriate ( )'s.

which operations do you mean exactly?
i received this formula for height from my prof.... h = v^2 sin (α) /g.
a being the angle
Last edited on
closed account (3CXz8vqX)
line 4, for instance are you sure it's not supposed to be

`t = ( v*sin(a) ) / 9.8 `

I have no idea how your professor wrote the formulae and I'm clueless in Mechanics ^-^;

Just check them all where you have different operators competing with each other. eg Line 13.
yea i thought about that earlier and had changed it. but it makes no difference on the output.. its still the same.
line 13 is the same as 20 only difference i made was to do the pow b4 the function.. lol i have no idea why i even did that cause it make no difference
closed account (3CXz8vqX)
Right, gimme some data to work with, what inputs are you using and what outputs do you expect?
ok so far i have done these:
angle 37 and velocity 190.. i should get the distance as 3540.97, time as 23.34, and height as 667.08

another one i checked with is:
angle 45 and velocity 150.. i should get the distance as 2295.92, time as 21.65, and height as 573.98
closed account (3CXz8vqX)
Formula angles into radian is wrong.

.... are your formula right because a quick google is uh... not confirming.
yea i checked the formula and its correct (as to my knowledge based on a google search) and this what it says the formula is radian = degrees * (pi/180).. which would be a=a*(PI/180); //degrees into radian

and thats what i have in the code, which is why the other two(distance, time) are coming out correct
Last edited on
closed account (3CXz8vqX)
Okay, I'm admitting defeat here. >.<; I don't know my physics well enough to be able to know what goes where at this point.
@Ravenshade thanks for trying tho :)

im just hoping someone here will have the answer to my problem.. b4 i joined i did my research on this site and it has lots of knowledgeable people on the forums.. so im hoping somebody might be able to point out the error in my code
The formula for height is wrong.
It should be v2 / 2g
where v is the vertical component of the velocity, v*sin(a)
and g is the acceleration due to gravity.

 ``1234`` ``````double height2 (double v, double a) { return v*sin(a) * v*sin(a) / (2*9.8); }``````

Also the value of PI is incorrect, meaning all the figures are slightly wrong.
@Chervil thank you so much you just saved me another whole day of hitting my head against the wall :)

i cant believe my prof actually gave us the wrong formula smh.. if you hadn't pointed this out i would have never figured it out cause i wasn't gonna question his formula.. thanks
Last edited on
Topic archived. No new replies allowed.