1. Write a function using C language for finding the magnitude orphase of an ima
ID: 3619117 • Letter: 1
Question
1.
Write a function using C language for finding the magnitude orphase of an imaginary number. The function should have astructure input parameter and an integer input parameter. Thestructure input parameter will contain two doubles, real and imag,that contain the real and imaginary part of the number. The integer input will be a flag to determine if the magnitude orphase is to be calculated. A zero will represent that themagnitude is desired, otherwise the phase should becalculated. The phase should be returned as short integerthat takes a value between 0 and 359. The magnitude should bereturned as a double. A union should be used so that themagnitude or phase may be returned under their respective type.
Explanation / Answer
please rate - thanks hope the math is right #include #include #include struct number {double real; double imag; }; union value {short phase; double mag; }; union value calculate(struct number,int); int main() { struct number n; union value result; int find; printf("Enter an imaginary number Enter the real part: "); scanf("%lf",&n.real); printf("Enter the imaginary part: "); scanf("%lf",&n.imag); printf("What do you want to find?(0 for magnitude, 1 forphase): "); scanf("%d",&find); result=calculate(n,find); if(find==0) printf("the magnitude is %f ",result.mag); else printf("the phase is%d ",result.phase); getch(); return 0; } union value calculate(struct number m,int n) {union value i; if(n==0) i.mag= sqrt(m.real*m.real+m.imag*m.imag); else i.phase=atan(m.imag/m.real); return i; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.