Vector Addition Calculator Help

Hi I am studying C++ programming and I am trying to make a Vector Addition Calculator. I get it to work for only 2 quadrants but the rest don't seem to work. Here is the code that I have written. It might be hard to follow but I would appreciate any help thats.


#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<math.h>

//#define PI 3.14159265

using namespace std;

int main()
{
    int magvec1;
    int angvec1;
    int magvec2;
    int angvec2;
    double vector1x, vector2y, vector3x, vector4y, Rx, Ry ,R3, R4, R5, angtheta;
    float ang;
    float angl;
    float mag;
    float angle;
    float Rangle;
    float TempAngle1,TempAngle2;
    float radang1, radang2;
    const float rad = 0.017453293;
    const float pi=3.14;
  

    cout<< "Hello! Welcome to Ramiro's Vector Addition Calculator." <<endl;
    cout<< "Please input Magnitude: ";
    cin >> magvec1;
    cout<< "Please input Angle: ";
    cin >> angvec1;
    cout<< "Chose second Magnitude: ";
    cin >> magvec2;
    cout<< "Please second Angle: ";
    cin >> angvec2;
    float ANGCOS1,ANGSIN1,ANGCOS2,ANGSIN2;
    
        radang1 = angvec1 * rad;
        radang2 = angvec2 * rad;
        TempAngle1=radang1;
        TempAngle2=radang2;
        cout<<"the rad angle1 is " <<TempAngle1<<endl;
        cout<<"The rad angle2 is " <<TempAngle2<<endl;
        ANGCOS1=cos(radang1);
        ANGSIN1=sin(radang1);
        ANGCOS2=cos(radang2);
        ANGSIN2=sin(radang2);
        cout<<"The cos1 is "<<ANGCOS1<<endl;
        cout<<"The sin1 is "<<ANGSIN1<<endl;
        cout<<"The cos2 is "<<ANGCOS2<<endl;
        cout<<"The sin2 is "<<ANGSIN2<<endl;
        
        
        //radang2 = angvec2 * rad;
        vector1x = ANGCOS1*magvec1;
        cout<<" x componet of vector 1 is "<<vector1x<<endl;
        vector2y = ANGSIN1*magvec1;
        cout<<"y component of vector 1 is "<<vector2y<<endl;
        vector3x = ANGCOS2*magvec2;
        cout<<"x component of vector 2 is  "<<vector3x<<endl;
        vector4y = ANGSIN2*magvec2;
        cout<<"y component of vector 2 is "<<vector4y<<endl; 
        Rx = vector1x + vector3x; 
        cout<<"Resultant Vector x "<<Rx<<endl;
        Ry = vector2y + vector4y;
        cout<<"Resultant Vector y "<<Ry<<endl;
        R3 = Rx*Rx;
        R4 = Ry*Ry;
        R5 = R3 + R4;
        angtheta= (Ry/Rx);
        cout<<"AngTheta is "<<angtheta<<endl;
        angl= atan(angtheta);
        cout<<"The Arc angle is "<<angl<<endl;
        angle= (angl*180)/ pi;
        cout<<"The Angle is "<<angle<<endl;
        Rangle= angle+180;
        cout<<"Angle= "<<Rangle<<endl;
        if (Rangle > 90 && Rangle <180)
             Rangle = Rangle + 180;
             else (
        else (angvec1>90 && angvec2<180);
           Rangle = Rangle - 180;
             
                       
        mag = sqrt(R5);
        /*if (Rx>0&& Ry>0)
        
           angl = atan(angtheta);
        if  (Rx<0&&Ry<0)
         */    angl=(atan(angtheta)* pi);
        cout<<"The Resultant Angle is "<<Rangle<<endl;
        cout<<"The Resultant Magnitude is "<<mag<<endl;
             

    system("pause");
    return(0);

}

1
2
3
4
5
if (Rangle > 90 && Rangle <180)
             Rangle = Rangle + 180;
             else (
        else (angvec1>90 && angvec2<180);
           Rangle = Rangle - 180;


Is this a mistake perhaps?
I commented it out but it only works in quadrant 2 and 3 only.
I made some minor changes and now it work in quadrants 3 and 4 only. Here is the new code.

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<math.h>

//#define PI 3.14159265

using namespace std;

int main()
{
    int magvec1;
    int angvec1;
    int magvec2;
    int angvec2;
    double vector1x, vector2y, vector3x, vector4y, Rx, Ry ,R3, R4, R5, angtheta;
    float ang;
    float angl;
    float mag;
    float angle;
    float Rangle;
    float TempAngle1,TempAngle2;
    float radang1, radang2;
    const float rad = 0.017453293;
    const float pi=3.14;
  

    cout<< "Hello! Welcome to Ramiro's Vector Addition Calculator." <<endl;
    cout<< "Please input Magnitude: ";
    cin >> magvec1;
    cout<< "Please input Angle: ";
    cin >> angvec1;
    cout<< "Chose second Magnitude: ";
    cin >> magvec2;
    cout<< "Please second Angle: ";
    cin >> angvec2;
    float ANGCOS1,ANGSIN1,ANGCOS2,ANGSIN2;
    
        radang1 = angvec1 * rad;
        radang2 = angvec2 * rad;
        TempAngle1=radang1;
        TempAngle2=radang2;
        cout<<"the rad angle1 is " <<TempAngle1<<endl;
        cout<<"The rad angle2 is " <<TempAngle2<<endl;
        ANGCOS1=cos(radang1);
        ANGSIN1=sin(radang1);
        ANGCOS2=cos(radang2);
        ANGSIN2=sin(radang2);
        cout<<"The cos1 is "<<ANGCOS1<<endl;
        cout<<"The sin1 is "<<ANGSIN1<<endl;
        cout<<"The cos2 is "<<ANGCOS2<<endl;
        cout<<"The sin2 is "<<ANGSIN2<<endl;
        
        
        //radang2 = angvec2 * rad;
        vector1x = ANGCOS1*magvec1;
        cout<<" x componet of vector 1 is "<<vector1x<<endl;
        vector2y = ANGSIN1*magvec1;
        cout<<"y component of vector 1 is "<<vector2y<<endl;
        vector3x = ANGCOS2*magvec2;
        cout<<"x component of vector 2 is  "<<vector3x<<endl;
        vector4y = ANGSIN2*magvec2;
        cout<<"y component of vector 2 is "<<vector4y<<endl; 
        Rx = vector1x + vector3x; 
        cout<<"Resultant Vector x "<<Rx<<endl;
        Ry = vector2y + vector4y;
        cout<<"Resultant Vector y "<<Ry<<endl;
        R3 = Rx*Rx;
        R4 = Ry*Ry;
        R5 = R3 + R4;
        angtheta= (Ry/Rx);
        cout<<"AngTheta is "<<angtheta<<endl;
        angl= atan(angtheta);
        cout<<"The Arc angle is "<<angl<<endl;
        angle= (angl*180)/ pi;
        cout<<"The Angle is "<<angle<<endl;
        Rangle= angle+180;
        cout<<"Angle= "<<Rangle<<endl;
        if (Rangle > 90 && Rangle <180)
             Rangle = Rangle + 180;
                      
        mag = sqrt(R5);
        /*if (Rx>0&& Ry>0)
        
           angl = atan(angtheta);
        if  (Rx<0&&Ry<0)
         */    angl=(atan(angtheta)* pi);
        cout<<"The Resultant Angle is "<<Rangle<<endl;
        cout<<"The Resultant Magnitude is "<<mag<<endl;
             

    system("pause");
    return(0);

}
Last edited on
Topic archived. No new replies allowed.