Structs and functions help
I am working on a function for my project that formats the output of complex(imaginary) numbers, but when I run it, its not being formatted as i thought it would be.
the struct being used is:
Code:
typedef struct {
double re;
double im;
} complex;
the function i have written for this so far is:
Code:
void output_complex(FILE *outf,complex z)
{
if(z.im == 0)
{
fprintf(outf,"%d", z.re);
}
if(z.re == 0)
{
if(z.im == 1)
{
fprintf(outf,"i");
}
fprintf(outf,"%di", z.im);
}
if(z.im == 1)
{
fprintf(outf,"%d + i", z.re);
}
if(z.im < 0)
{
if(z.im == -1)
{
z.im *= -1;
fprintf(outf,"%d - i", z.re);
}
z.im *= -1;
fprintf(outf,"%d - %di", z.re, z.im);
}
fprintf(outf,"%d + %di", z.re, z.im);
}
and there is this program that checks to see if the expected output is the same as the output of the function and this is what I got when I ran it for this function:
Output Function Errors:
expected: 1 + 2i
actual: 0 + 1072693248i
expected: 1 + 2i
actual: 0 + 1072693248i
what am I doing wrong?