Write a program in C++ to calculate the magnitude and angle of a vector given po
ID: 669304 • Letter: W
Question
Write a program in C++ to calculate the magnitude and angle of a vector given point x and point y in the x-y plane. The angle should be expressed in both radians and degrees, measured in positive standard trigonometry designation, starting at the x axis and rotating CCW.
The program must output the x and y points, vector magnitude, angle in degrees and angle in radians, to the screen. Each item must be specifically identified.
For this assignment, calculate the first-quadrant angle using the absolute values of x and y. Then determine the quadrant in which the actual angle would lie by using the signs of the input x and y values. For example, if the x and y values were -3 and 2, the angle would lie in quadrant 2. Use trigonometric relationships to determine the proper positive angle.
The program must use if/switch statements to determine quadrants.
Procedure: Read in the x and y coordinates from the keyboard. User will be prompted to enter the data in the form needed, i.e., a single point value or a pair of values. Then, for each x and y entry, calculate the data, and print the result to the screen. The next set of x and y will then be entered.
Verification input data for the coordinate point sets will be as follows:
3, 1 (meaning x=3 and y=1)
2, 2
1, 3
-1, 3
-2, 2
-3, 1
-3, -1
-2, -2
-1, -3
1, -3
2, -2
3, -1
Other additional input data points may be used for grading.
Note::: Only the digits and spacce are entered via the keyboard, not the letters or commas.
e.g. 2 2 , use space between numeric inputs.
3 2
etc.
Explanation / Answer
#include<iostream.h>
#include<tgmath.h> //This headerfile is specifically used to calculate the inverse tan() value to evaluate the angle.
#include<math.h>
#include<conio.h>
#define PI 3.14 //This is the prepocessor directive which use the value of Pi=3.14.
void main()
{
double a=0.0,b=0.0; //The values will be input by the user.
double c=0.0,d=0.0; //This are used as the temporary variable to store the absolute values.
float mag=0.0;
float deg=0.0;
float rad=0.0;
clrscr();
//User prompt to enter the value of the co-ordinates
cout<<"Enter the space separeted values of the co-ordinates ";
cin>>a>>b;
/* The cin>> command in the C++ takes the space separated values by default.
Hence it will be stored in the variable 'a' and 'b'.
Now we need to find the magnitide of the co-ordinates which will be calculated by the formula
sqrt(a^2+b^2) */
mag=sqrt((a*a)+(b*b));
//Calculating the absolute value as per the requirement to calculate the value of angle.
c=abs(a);
d=abs(b);
//atan2() is used to calculate the angle which takes the input of the co-ordinates.
deg=atan2(d,c)* 180 / PI;;
rad=atan2(d,c);
/* Now we check whether the quadrant in which the particular co-ordinate lies.
We check the sign of the co-ordinates value. */
if(a>0&&b>0)
{
cout<<"The co-ordinates input from the user are "<<a<<"and "<<b<<endl;
cout<<"The co-ordinates is in the first quadrant"<<endl;
cout<<"The magnitude if the co-ordinates is"<<" " <<mag<<endl;
cout<<"The angle in the degree is "<<" " <<deg<<endl;
cout<<The degrees in the radian is<<" " <<rad<<endl;
}
else if(a<0&&b>0)
{
cout<<"The co-ordinates input from the user are "<<a<<"and "<<b<<endl;
cout<<"The co-ordinates is in the second quadrant"<<endl;
cout<<"The magnitude if the co-ordinates is"<<" " <<mag<<endl;
cout<<"The angle in the degree is "<<" " <<deg<<endl;
cout<<The degrees in the radian is<<" " <<rad<<endl;
}
else if(a<0&&b<0)
{
cout<<"The co-ordinates input from the user are "<<a<<"and "<<b<<endl;
cout<<"The co-ordinates is in the third quadrant"<<endl;
cout<<"The magnitude if the co-ordinates is"<<" " <<mag<<endl;
cout<<"The angle in the degree is "<<" " <<deg<<endl;
cout<<The degrees in the radian is<<" " <<rad<<endl;
}
else
{
cout<<"The co-ordinates input from the user are "<<a<<"and "<<b<<endl;
cout<<"The co-ordinates is in the fourth quadrant"<<endl;
cout<<"The magnitude if the co-ordinates is"<<" " <<mag<<endl;
cout<<"The angle in the degree is "<<" " <<deg<<endl;
cout<<The degrees in the radian is<<" " <<rad<<endl;
}
getch();
}
/* The values are calculated purely in the mathematical formula based on the co-ordinate geometry ,Hence the math.h directive have been used to make the program to compute the values easily *.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.