Question: C PROGRAMING! My code does not match the output very well and I don't know how to fix it. Help would be appreciated! [Example of

C PROGRAMING! My code does not match the output very well and I don't know how to fix it. Help would be appreciated!

[Example of input] 2 3.08 -2.04 5.06 [Output example] (2.0+3.1i) + (-2.0+5.1i) = 8.1i (2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i (2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i (2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i

My code:

#include typedef struct complex { float real; float imag; } complex;

complex add(complex n1, complex n2); complex sub(complex n1, complex n2); complex mul(complex n1, complex n2); complex divi( complex n1, complex n2); int main() { complex n1, n2, result;

scanf("%f %f %f %f", &n1.real, &n1.imag,&n2.real, &n2.imag);

result = add(n1, n2); printf("(%.1f+%.1fi)+(%.1f+%.1fi) = %.1f + %.1fi ", n1.real, n1.imag, n2.real, n2.imag, result.real, result.imag); result= sub(n1,n2); printf("(%.1f+%.1fi)-(%.1f+%.1fi) = %.1f + %.1fi ", n1.real, n1.imag, n2.real, n2.imag,result.real, result.imag); result = mul(n1,n2); printf("(%.1f+%.1fi)*(%.1f+%.1fi) = %.1f + %.1fi ", n1.real, n1.imag, n2.real, n2.imag,result.real, result.imag); result = divi(n1,n2); printf("(%.1f+%.1fi)/(%.1f+%.1fi) = %.1f + %.1fi ",n1.real, n1.imag, n2.real, n2.imag, result.real, result.imag); return 0; }

complex add(complex n1, complex n2) { complex temp; temp.real = n1.real + n2.real; temp.imag = n1.imag + n2.imag; return (temp); } complex sub(complex n1, complex n2){ complex temp; temp.real=n1.real-n2.real; temp.imag=n1.imag-n2.imag; return(temp); } complex mul(complex n1, complex n2){ complex temp; temp.real=(n1.real*n2.real)-(n1.imag*n2.imag); temp.imag=(n1.imag*n2.real)+(n1.real*n2.imag); return(temp);}

complex divi( complex n1, complex n2){ complex temp; temp.real=((n1.real*n2.real)+(n1.imag*n2.imag))/(n2.real*n2.real+n2.imag*n2.imag); temp.imag=((n1.imag*n2.real)-(n1.real*n2.imag))/(n2.real*n2.real+n2.imag*n2.imag); return(temp);}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!