Help For Vector Addition with magnitude and direction.

hey everyone, i new to c++ programming. i have a program due soon in vector addition.
i have done some work that I've shared below. the program that i am to design should be able to add vectors with certain magnitude and direction and give its resultant magnitude and direction (i know how to do this mathematically but programming is not working. the user should be able to select how many vector he/she wants (i don't know how to do this, so i added 3 vectors). here's my work - i did this purely algebraically. it's still incomplete but it compiles. my "if else" statement also doesn't respond correctly.
any help will be greatly appreciated.

#include <iostream>
#include <string>
#include <vector>
#define PI 3.14159
#include <math.h>
std::vector<int> v; // declares a vector of integers
using namespace std;


int main ()
{
float i, i1, i2;
float x, x1, x2;
float result;
cout << "enter the 1st magnitude ";
cin >> i;
cout << endl;
cout << "enter the 2nd magnitude ";
cin >> i1;
cout << endl;
cout << "enter the 3rd magnitude ";
cin >> i2;
cout << endl;

cout << "enter the 1st angle ";
cin >> x;
cout << endl;
cout << "enter the 2nd angle ";
cin >> x1;
cout << endl;
cout << "enter the 3rd angle ";
cin >> x2;
cout << endl;

x = (x*PI)/180;
x1 = (x1*PI)/180;
x2 = (x2*PI)/180;

float X1, X2, X3;
if (((x, x1, x2) > 90), ((x, x1, x2) < 270)) {

X1 = i*sin(-x);
X2 = i1*sin(-x1);
X3 = i2*sin(-x2);
}
else (((x, x1, x2) < 90), ((mx, x1, x2) > 270)); {
X1 = i*sin(x);
X2 = i1*sin(x1);
X3 = i2*sin(x2);
}




float Y1, Y2, Y3;
Y1 = i*cos(x);
Y2 = i1*cos(x1);
Y3 = i2*cos(x2);


float result_x, result_y;
result_x = X1 + X2 + X3;
result_y = Y1 + Y2 + Y3;

float Fr, Fx, Fy;
Fx = result_x*result_x;
Fy = result_y*result_y;
Fr = sqrt(Fx + Fy);

result = X1 + X2 + X3;

cout << X2;




cout << "the sum of the magnitude is ";
cout << result << endl;









system ("pause");
return 0;

}
Not sure if you've learned objects yet, but this makes it very easy to use a variable number of vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>
using namespace std;

class Vector2d
{
    float x, y;
public:
    Vector2d() : x(0), y(0) {}
    Vector2d(float mag, float ang) { setMagAng(mag, ang); }

    void setMagAng(float in_magnitude, float in_angle)
    {
        x = in_magnitude * cos(in_angle);
        y = in_magnitude * sin(in_angle);
    }

    float getMag() { return sqrt(x*x + y*y);  }
    float getAng() { return atan2(y ,x);      }

    void operator+= (Vector2d& rhs)
    {
        this->x += rhs.x;
        this->y += rhs.y;
    }
};

int main()
{
    Vector2d output;
    int num_to_add;

    cout << "How many vectors do you want to add?";
    cin >> num_to_add;

    for (int i = 0; i < num_to_add; ++i)
    {
        float mag, ang;
        cout << "Vector" << i << ": What's the magnitude?";
        cin >> mag;
        cout << "What's the angle?";
        cin >> ang;

        Vector2d temp(mag, ang);
        output += temp;
    }

    cout << "Sum of magnitude is: " << output.getMag() << endl;
    cout << "Sum of angles is: "    << output.getAng() << endl;
}



I'll look through your code and make some changes, though I already see some issues:
else (((x, x1, x2) < 90), ((mx, x1, x2) > 270)); {

first off, else ifs look like:
else if (condition) {, not else (condition); {
second:
((x, x1, x2)< 90),. I'm not entirely sure what you're doing here. WHat is your intention. I'm guessing you mean to do something like:
(x < 90 || x1 < 90 || x2 < 90) &&
But you really don't want to do that either because though the syntax is correct, the three angles may be in different quadrants and the signs may change for each one. Right now you are batching them all togeather.
Last edited on
This is a quick way of doing what you were trying to do.

Instead of messing with nasty if-else conditions to check for the angle's quadrant, you can use the atan2 function. It's very handy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#define PI 3.14159f
#include <cmath> 
using namespace std;

int main ()
{
    int num_angles;
    cout << "How many angles do you want to enter? ";
    cin >> num_angles;
    
    float x = 0, y = 0;
    
    for (int i = 0; i < num_angles; ++i)
    {
        float mag, angle;
        cout << "Enter magnitude " << i+1 << ": " << endl;
        cin >> mag;
        
        cout << "Enter angle " << i+1 << ": " << endl;
        cin >> angle;
        
        angle *= PI/180.f;
        x += mag * cos(angle);
        y += mag * sin(angle);
    }
    
    cout << "Total angle: " << atan2(y,x)*(180.f / PI) << endl;
    cout << "Total magnitude: " << sqrt(x*x + y*y) << endl;

    return 0;
}
Last edited on
wow... just wow...
i spend a day trying to figure this out... and you helped me in the fraction of that time..
thank you stewbond...thank you very much!!!

The fact that you spent an entire day on this is good. No pain, no gain. It meant that you are really looking deep into it, running into dead-ends, and learning what doesn't work. That's essential for learning. As you figure out what doesn't work, you'll be more familiar with way of making it really work.
Topic archived. No new replies allowed.