What's new

What's the problem with this code?

pradipna

New member
What's wrong with this C++ code? :plain: . It's a code to find time and height using fomulas:
time=distance/ velocity*cos(theta)
height=velocity*sin(theta)*time-G*time^2/2

Code:
#include <iostream>
#include <stdio.h>
#include <math.h>
#define G 32.17
using namespace std;
int main()
{
    int theta;
    int distance;
    int velocity;
    int time;
    int height;
    
    cout<<"Enter an angle for theta: ";
    cin>> theta;
    cin.ignore();
    cout<<"Enter a distance: ";
    cin>> distance;
    cin.ignore();
    cout<<"Enter value for velocity: ";
    cin>> velocity;
    cin.ignore();
    time = distance/ velocity*cos(theta);
    height = velocity*sin(theta)*time-G*time^2/2;
    cout<<"time is: ";
    cout<< time;
    cout<<" height is: ";
    cout<< height;    
    cin.get();
}
 

refraction

PCSX2 Coder
try using some brackets to clarify your equasions to the compiler, it might be interpreting the equasions incorrectly, apart from that i dont know exactly what your problem is with it..
 

Doomulation

?????????????????????????
That formula is unfamiliar to me, but it might suggest that the operations are not done in the order you think they are.
To easily fix these problems, you should try to do these values in your watch window. Break it up into smaller parts and see what might go wrong. Then also use paranthesis to specify which operations should take predesence.
 

TRS

Member
You could also try to use floats in stead of integers :whistling
 
Last edited:

Doomulation

?????????????????????????
That's also a good point. When it comes to calculations that is floating point, then you should add (float) or (double) to make sure the floating result is not discarded. AFTER the calculation you convert back to integer.
 

TRS

Member
Doomulation said:
AFTER the calculation you convert back to integer.
Why would you want to convert the result of this calculation back to an integer? I seriously doubt he wants an integer as a result.
 

euphoria

Emutalk Member
IIRC, in C expressions are executed from right to left.
Also ^ isn't power. Its binary XOR.
sin() and cos() etc take degree in radians so you have to change it if it's given in degrees.

It's too long from my physics classes to remember the correct formulas, so you have to place brackets to correct places yourself.

So try this:
Code:
#include <iostream>
#include <stdio.h>
#include <math.h>
#define G 32.17

using namespace std;

int main()
{
	double theta;
	double distance;
	double velocity;
	double time;
	double height;
	
	cout<<"Enter an angle for theta: ";
	cin>> theta;
	cin.ignore();
	cout<<"Enter a distance: ";
	cin>> distance;
	cin.ignore();
	cout<<"Enter value for velocity: ";
	cin>> velocity;
	cin.ignore();
	time = distance / (velocity*cos(theta*PI/180));
	height = velocity*sin(theta*PI/180)*time - (G*pow(time,2))/2;
	cout<<"time is: ";
	cout<< time;
	cout<<" height is: ";
	cout<< height;    
	cin.get();
}

Hopefully you're not trying to do your homework on us... :plain:
 

zAlbee

Keeper of The Iron Tail
Yep, euphoria's version looks correct. The formulas look correct as well.

It is just another version of

d = vt + 0.5 at^2

with the initial velocity going at an angle.
 

Cyberman

Moderator
Moderator
TRS said:
G = 32.17?? Where are you from? I don't know a place on earth with that much gravity!
That's English units 32.17 ft/s^2 is the aceleration of gravity in those units. 9.81 m/s^2 is KGS system.

Cyb
 
OP
P

pradipna

New member
Doesn't work. ANd this is not my project either. I am trying to help someone else in this.:bouncy:
 
Last edited:

Top