1

The program will ask the user to enter the real and imaginary parts of two complex numbers. The program should output the sum, difference, product, quotient, and absolute value of the complex numbers formatted to two decimal places.

Everything has worked, except for the abs value part of the code, whenever I run it, I get really weird numbers where the abs value should be, e.g -1.644534634 or -1.4363465.

So as I was looking, I noticed I forgot to add #include <math.h> to the top, however, when I do this, everything gets messed up and I get 61 errors about how .real and .img are not part of complex.

If possible, could you look at my abs value method and see if I wrote that correctly as well, thank you.

#include <stdio.h>
//#include <math.h>

struct complex
{
    float real;
    float img;
};

struct complex add_complex(struct complex c1, struct complex c2);
struct complex subtract_complex(struct complex c1, struct complex c2);
struct complex multiply_complex(struct complex c1, struct complex c2);
struct complex divide_complex(struct complex c1, struct complex c2);
double abs_complex(struct complex c);

void main()
{
    struct complex c, c1, c2, tempa, temps, tempm, tempd;
    double abs1, abs2;

    printf("Enter the real part of the 1st complex number:\n");
    scanf("%f", &c1.real);
    printf("Enter the imaginary part of the 1st complex number:\n");
    scanf("%f", &c1.img);
    printf("Enter the real part of the 2nd complex number:\n");
    scanf("%f", &c2.real);
    printf("Enter the imaginary part of the 2nd complex number:\n");
    scanf("%f", &c2.img);

    tempa = add_complex(c1, c2);
    temps = subtract_complex(c1, c2);
    tempm = multiply_complex(c1, c2);
    tempd = divide_complex(c1, c2);
    abs1 = abs_complex(c1);
    abs2 = abs_complex(c2);

    printf("\n");

    if (tempa.real == 0)
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempa.img);
    else if (tempa.img == 0)
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2f)\n", c1.real, c1.img, c2.real, c2.img, tempa.real);
    else
        printf("(%.2f + %.2fi) + (%.2f + %.2fi) = (%.2f + %.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempa.real, tempa.img);

    if (temps.real == 0)
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2fi)\n", c1.real, c1.img, c2.real, c2.img, temps.img);
    else if (temps.img == 0)
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2f)\n", c1.real, c1.img, c2.real, c2.img, temps.real);
    else
        printf("(%.2f + %.2fi) - (%.2f + %.2fi) = (%.2f + %.2fi)\n", c1.real, c1.img, c2.real, c2.img, temps.real, temps.img);

    if (tempm.real == 0)
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempm.img);
    else if (tempm.img == 0)
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2f)\n", c1.real, c1.img, c2.real, c2.img, tempm.real);
    else
        printf("(%.2f + %.2fi) * (%.2f + %.2fi) = (%.2f + %.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempm.real, tempm.img);

    if (tempd.real == 0)
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempd.img);
    else if (tempd.img == 0)
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2f)\n", c1.real, c1.img, c2.real, c2.img, tempd.real);
    else
        printf("(%.2f + %.2fi) / (%.2f + %.2fi) = (%.2f + %.2fi)\n", c1.real, c1.img, c2.real, c2.img, tempd.real, tempd.img);

    printf("|(%.2f + %.2fi)| = (%.2d)\n", c1.real, c1.img, abs1);
    printf("|(%.2f + %.2fi)| = (%.2d)\n", c2.real, c2.img, abs2);

    printf("\n");
}

struct complex add_complex(struct complex c1, struct complex c2)
{
    struct complex tempa;
    tempa.real = c1.real + c2.real;
    tempa.img = c1.img + c2.img;
    return(tempa);
}

struct complex subtract_complex(struct complex c1, struct complex c2)
{
    struct complex temps;
    temps.real = c1.real - c2.real;
    temps.img = c1.img - c2.img;
    return(temps);
}

struct complex multiply_complex(struct complex c1, struct complex c2)
{
    struct complex tempm;
    tempm.real = c1.real*c2.real - c1.img*c2.img;
    tempm.img = c1.real*c2.img + c1.img*c2.real;
    return(tempm);
}

struct complex divide_complex(struct complex c1, struct complex c2)
{
    struct complex tempd, temp1, temp2;
    temp1.real = c1.real*c2.real + c1.img*c2.img;
    temp2.real = c2.real*c2.real + c2.img*c2.img;
    temp1.img = c1.img*c2.real - c1.real*c2.img;
    temp2.img = c2.real*c2.real + c2.img*c2.img;
    tempd.real = temp1.real / temp2.real;
    tempd.img = temp1.img / temp2.img;
    return(tempd);
}

double abs_complex(struct complex c)
{
    double temp1, temp2;
    double abs;
    temp1 = c.real*c.real;
    temp2 = c.img*c.img;
    abs = sqrt(temp1 + temp2);
    return(abs);

}
Sean
  • 47
  • 5

1 Answers1

5

Part 1:

It's possible that math.h is including complex.h which would create this macro:

#define complex _Complex

I'd suggest renaming your complex type, or use the builtin one described here.

You could also get away with doing #undef complex after #include <main.h>, but for large programs that's probably not sustainable.

Part 2:

You're using the wrong format specifier when you print the absolute values. Here are the warnings from clang that clearly explain the issue and provide a solution:

foo.c:68:60: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
    printf("|(%.2f + %.2fi)| = (%.2d)\n", c1.real, c1.img, abs1);
                                ~~~~                       ^~~~
                                %.2f
foo.c:69:60: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
    printf("|(%.2f + %.2fi)| = (%.2d)\n", c2.real, c2.img, abs2);
                                ~~~~                       ^~~~
                                %.2f
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • This solved my 61 error problem! Thank you very much. Could you now help me with the abs value function? I'm still getting really large answers. It almost seems like the sqrt function isn't doing its job. – Sean Sep 30 '14 at 18:08
  • 2
    The problem with `math.h` defining `complex` seems to be a Microsoft C problem. The `math.h` header shouldn't be doing that (there's a comment in MSVC's `math.h` that says, "Non-ANSI name for compatibility" where it does the define). – Michael Burr Sep 30 '14 at 18:14
  • Ohhh, I forgot %d is another way to include an int, not a double. Thank you for that! – Sean Sep 30 '14 at 18:28