Update the following code so that Update the following code
Update the following code so that Update the following code so it uses a loop structure (any type) and it will run multiple times. The user chooses whether to repeat or exit the program by entering “Y” or “N” at the prompt.
#include <stdio.h>
#include <math.h>
double find_x_comp(double magnitude, double angle);
double find_y_comp(double magnitude, double angle);
int main(void)
{
double mag1,angle1,mag2,angle2;
double x1,x2,y1,y2,x,y,R,Ra;
//Declares variables
printf(\"Enter magnitude of vector 1 and angle 1,\ Enter magnitude of vector 2 and angle 2\ \");
//prompts user to input vectors and angles
scanf(\"%lf%lf%lf%lf\", &mag1, &angle1, &mag2, &angle2);
//Gets inputs
printf(\"Vector 1 is %.2f , angle 1 is %.2f\ Vector 2 is %.2f , angle 2 is %.2f\ \", mag1, angle1, mag2, angle2);
//Displays inputs
x1 = find_x_comp(mag1,angle1);
x2 = find_x_comp(mag2,angle2);
y1 = find_y_comp(mag1,angle1);
y2 = find_y_comp(mag2,angle2);
//Calls for user defined functions
x = (x1 +x2); //calculates x components
y = (y1 +y2); //calculates y components
R = sqrt(x*x + y*y); //calculates resultant
if(x>0)
{
Ra = (atan(y/x))*(180/3.14)+180;
}
else if (x>0,y>0)
{
Ra = (atan(y/x))*(180/3.14);
}
else
{
Ra = (atan(y/x))*(180/3.14)+360;
}
//Calculates the resultant angle from 0-359.9 degrees
printf(\"Resultant vector is %.2f at an angle of %.2f degrees\ \", R, Ra);
//displays results
return 0;
}
//User Defined Functions
double find_x_comp(double magnitude, double angle) //calculates x-component
{
double x_comp;
x_comp = ((magnitude)*(cos((angle)*3.14 / 180)));
return(x_comp);
}
double find_y_comp(double magnitude, double angle) //calculates y-component
{
double y_comp;
y_comp = (magnitude)*(sin((angle)*3.14 / 180));
return(y_comp);
}
Solution
#include <stdio.h>
#include <math.h>
double find_x_comp(double magnitude, double angle);
double find_y_comp(double magnitude, double angle);
int main(void)
{
calculate();
printf(\"want to calculate again\ \");
if(getchar()==\'Y\') {
calculate();
printf(\"want to calculate again\ \");
}
return 0;
}
void calculation(double mag1,double angle1,double mag2,double angle2) {
double x1,x2,y1,y2,x,y,R,Ra;
double mag1,angle1,mag2,angle2;
//Declares variables
printf(\"Enter magnitude of vector 1 and angle 1,\ Enter magnitude of vector 2 and angle 2\ \");
//prompts user to input vectors and angles
scanf(\"%lf%lf%lf%lf\", &mag1, &angle1, &mag2, &angle2);
//Gets inputs
printf(\"Vector 1 is %.2f , angle 1 is %.2f\ Vector 2 is %.2f , angle 2 is %.2f\ \", mag1, angle1, mag2, angle2);
//Displays inputs
x1 = find_x_comp(mag1,angle1);
x2 = find_x_comp(mag2,angle2);
y1 = find_y_comp(mag1,angle1);
y2 = find_y_comp(mag2,angle2);
//Calls for user defined functions
x = (x1 +x2); //calculates x components
y = (y1 +y2); //calculates y components
R = sqrt(x*x + y*y); //calculates resultant
if(x>0)
{
Ra = (atan(y/x))*(180/3.14)+180;
}
else if (x>0,y>0)
{
Ra = (atan(y/x))*(180/3.14);
}
else
{
Ra = (atan(y/x))*(180/3.14)+360;
}
//Calculates the resultant angle from 0-359.9 degrees
printf(\"Resultant vector is %.2f at an angle of %.2f degrees\ \", R, Ra);
//displays results
}
//User Defined Functions
double find_x_comp(double magnitude, double angle) //calculates x-component
{
double x_comp;
x_comp = ((magnitude)*(cos((angle)*3.14 / 180)));
return(x_comp);
}
double find_y_comp(double magnitude, double angle) //calculates y-component
{
double y_comp;
y_comp = (magnitude)*(sin((angle)*3.14 / 180));
return(y_comp);
}



