I'm having a problem with the output of my division. When I enter both real parts of 5 and 3 and imaginary parts of 4 and 2, I get 2.000000 + -2.000000i instead of 1.769231 + 0.153846i.
Here's the case for dividing in the main program:
case 4:
num1=Read_Complex();
num2=Read_Complex();
if ((num2.real=0)&&(num2.img=0));
printf("Error. Cannot divide by 0.\n");
}
else{
ans=Divide_Complex(num1,num2);
}
Here's where Read_Complex is called:
complex Read_Complex(void)
{
complex num;
printf("Enter real part:");
scanf("%d", &num.real);
printf("Enter img part:");
scanf("%d", &num.img);
return num;
}
And here's the division:
complex numer, denom;
float ans_real ans_img;
complex Divide_Complex(complex num1, complex num2)
{
numer=Multiply_Complex(num1, Conjugate_Complex(num2));
denom=Multiply_Complex(num2, Conjugate_Complex(num2));
ans_real=numer.real/denom.real;
ans_img=numer.img/denom.img;
printf("%f + %fi\n", ans_real, ans_img);
}
Any help would be great.
Complex.h:
#ifndef COMPLEX_H_
#define COMPLEX_H_
typedef struct {
int real;
int img;
} complex;
#endif