• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ help

Gibson486

Lifer
Any of you guys help me with my c++ program? The problem is that I have to add two vectors. It works fine when all the vectors lie with in the first quadrant, but once I get passed thta, the numbers come out weird. For example, I told it to find the sum of vectors 3@300 and 5@288 and it gives me an a negtive angle. I think the problem lies in my conversion from degrees to radians, but I do not see it.

/*this program adds two vectors that are specified by the user*/

#include <iostream>
#include <fstream>
#include <cmath>
#define pi 3.14
using namespace std;

int main()
{
double mag, v1, v2, v1x, v2x, v1y, v2y, vrx, vry, angle1, angle2, angle1r, angle2r, theta, thetad;

cout <<"Enter length and angle for vector 1" << endl;
cin >> v1>> angle1;
angle1r = (pi/180) * angle1;
cout <<"Enter length and angle for vector 2" << endl;
cin >> v2>> angle2;
angle2r = (pi/180) * angle2;

v1y = v1*sin(angle1r);
v1x = v1*cos(angle1r);

cout << "v1x"<<v1x<<""<<endl;
cout << "v1y"<<v1y<<""<<endl;

v2y = v2*sin(angle2r);
v2x = v2*cos(angle2r);

cout << "v2x"<<v2x<<""<<endl;

cout << "v2y"<<v2y<<""<<endl;

vrx = v1x + v2x;
vry = v1y + v2y;

theta = tan(vry/vrx);

cout << "theta"<<theta<<""<<endl;

thetad = (180/pi) * theta;


mag = sqrt((v1 * v1) + (v2 * v2));

cout << "Your added vectors add up to be "<<mag<<" with an angle of "<<thetad<<" degrees"<<endl;

if (thetad<90)
cout << "your added vector lands in the 1st quadrant"<<endl;
else if (thetad<180)
cout << "your added vector lands in the 2nd quadrant"<<endl;
else if (thetad<270)
cout << "your added vector lands in the 3rd quadrant"<<endl;
else if (thetad<360)
cout << "your added vector lands in the 4th quadrant"<<endl;

return 0;
}


edit: I just notice dthat we had a programming forum, i never knew that. hmmmm...ANyways....mods, can you move it there?
 
Back
Top