- Aug 9, 2000
- 18,378
- 2
- 0
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?
/*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?
