0

I'm supposed to write a program in C to create the Mandelbrot. I have specific instructions to follow and I'm pretty sure I have followed them correctly. The question I have is that when I try to compile my code with gcc (using the make command) I get an error which says:

mason> make
gcc -c main.c
main.c: In function ‘main’:
main.c:13:40: error: invalid operands to binary < (have ‘complex_t’ and ‘int’)

I have been having a lot of issues with my complex_t in order to make it a double for the Mandelbrot to print. If you could please see if YOU can find the error that I keep missing. Here is my code for all my files:

main.c (where the error is):

#include <stdio.h>
#include "complex.h"
#include "mandelbrot.h"

int main (void)

    {
          complex_t c;
             for (c.imag = -1.2; c.imag < 2.8; c.imag+= 0.05)        {
                 for (c.real = -2.1; c.real < -0.66; c.real+= 0.032)
                 {
                    if (abs_complex(mandelbrot(15,c)) > 100)
                    {
                        printf("-");
                    }
                    else
                        printf("#");
                 }
                 printf("\n");       }  
          return (0); }

mandelbrot.c:

#include "complex.h"
#include "mandelbrot.h"

complex_t mandelbrot(int n, complex_t c)
{

complex_t m = {100,100};
complex_t tmp;

    if (n == 0)
    {
        return c;
    }

    else

    if (abs_complex(mandelbrot(n-1,c) > 1000))
    {
        return m;
    }

    else
    {

    tmp = mandelbrot(n-1,c);        
    tmp = cmult(tmp, tmp);
    tmp = cadd(tmp,c);

    }
        return tmp;
}

mandelbrot.h:

complex_t mandelbrot(int n, complex_t c);

complex.c:

#include <stdio.h>
#include <math.h>
#include "complex.h"


/*
 *  Complex number input function returns standard scanning error code
 *    1 => valid scan, 0 => error, negative EOF value => end of file
 */
int
scan_complex(complex_t *c) /* output - address of complex variable to 
       fill     */
{
      int status;

      status = scanf("%lf%lf", &c->real, &c->imag);
      if (status == 2)
            status = 1;
      else if (status != EOF)
            status = 0;

      return (status);
}

/*
 *  Complex output function displays value as (a + bi) or (a - bi),
 *  dropping a or b if they round to 0 unless both round to 0
 */
void
print_complex(complex_t c) /* input - complex number to display   */
{
      double a, b;
      char   sign;

      a = c.real;
      b = c.imag;

      printf("(");

      if (fabs(a) < .005  &&  fabs(b) < .005) {
            printf("%.2f", 0.0);
      } else if (fabs(b) < .005) {
            printf("%.2f", a);
      } else if (fabs(a) < .005) {
            printf("%.2fi", b);
      } else {
            if (b < 0)
                  sign = '-';
            else
                  sign = '+';
            printf("%.2f %c %.2fi", a, sign, fabs(b));
      }

      printf(")");
}

/*
 *  Returns sum of complex values c1 and c2
 */
complex_t
add_complex(complex_t c1, complex_t c2) /* input - values to add    */
{
      complex_t csum;

      csum.real = c1.real + c2.real;
      csum.imag = c1.imag + c2.imag;

      return (csum);
}

/*
 *  Returns difference c1 - c2
 */
complex_t
subtract_complex(complex_t c1, complex_t c2) /* input parameters    */
{
      complex_t cdiff;
      cdiff.real = c1.real - c2.real;
      cdiff.imag = c1.imag - c2.imag;

      return (cdiff);
}

/*  ** Stub **
 *  Returns product of complex values c1 and c2
 */
complex_t
multiply_complex(complex_t c1, complex_t c2) /* input parameters    */
{
      complex_t cmul;
      double a, b, c, d;
      a = c1.real;
      b = c1.imag;
      c = c2.real;
      d = c2.imag;

      if (( b > 0 && d < 0) || (b < 0 && d > 0))
      {
          cmul.real - (a*c) + (fabs(b)*fabs(d));
          cmul.imag = (a*d) + (b*c);
      }
      else if (( b>0 && d>0) || (b<0 && d<0))
      {
      cmul.real = (a*c) - (b*d);
      cmul.imag = (a*d) + (b*c);
  }
      return (cmul);
}

/*  ** Stub **
 *  Returns quotient of complex values (c1 / c2)
 */
complex_t
divide_complex(complex_t c1, complex_t c2) /* input parameters     */
{
      complex_t cdiv;
      double a, b, c, d;
      a = c1.real;
      b = c1.imag;
      c = c2.real;
      d = c2.imag;

      if ( b > 0 && d < 0)
      {
          cdiv.real = (a*c) - (fabs(b)*fabs(d)) / ((c*c) + (d*d));
          cdiv.imag = (a*d) + (b*c) / ((c*c) + (d*d));
      }
      else if ( b>0 && d>0)
      {
          cdiv.real = (a*c) - (fabs(b)*fabs(d)) / ((c*c) + (d*d));
          cdiv.imag = ((-1*a*d) + (b*c)) / ((c*c) + (d*d));
  }
      else if (b<0 && d<0)
  {
         cdiv.real = (a*c) + (fabs(b)*fabs(d)) / ((c*c) + (d*d));
         cdiv.imag = ((-1*a*d) + (b*c)) / ((c*c) + (d*d));
  }
  else if (b<0 && d<0)
  {
        cdiv.real = (a*c) + (fabs(b)*fabs(d)) / ((c*c) + (d*d));
        cdiv.imag = ((a*fabs(d)) + (b*c)) / ((c*c) + (d*d));
  }
      return (cdiv);
}
/*
 *  Returns absolute value of complex number c
 */
double
abs_complex(complex_t c) /* input parameter                        */
{
      complex_t cabs;

      cabs.real = sqrt(c.real * c.real + c.imag * c.imag);

      return (cabs.real);
}

complex.h:

typedef struct {
      double real, imag;
} complex_t;

int scan_complex(complex_t *c);
void print_complex(complex_t c);
complex_t add_complex(complex_t c1, complex_t c2);
complex_t subtract_complex(complex_t c1, complex_t c2);
complex_t multiply_complex(complex_t c1, complex_t c2);
complex_t divide_complex(complex_t c1, complex_t c2);
complex_t abs_complex(complex_t c);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I haven't yet spotted the error gcc is complaining about, but you do appear to have an error in `complex.c`, at `cmul.real - (a*c) + (fabs(b)*fabs(d));`. That should be an assignment, not a subtraction. – John Bollinger Apr 25 '15 at 01:18
  • 1
    On the other hand, I don't see why the multiplication has different cases depending on whether the signs of `b` and `d` are the same. The same multiplication formula applies regardless of the signs. – John Bollinger Apr 25 '15 at 01:21
  • perhaps you fixed it yourself, given that your code does not have < binary operator as compiler complains at line 13 – Sarang Apr 25 '15 at 01:23
  • 2
    As @Sarang said, the code you posted does not correspond to the error message you posted. For that matter, the compilation command you posted looks wrong, too. We cannot help you very well if you give us an inaccurate description of your problem. – John Bollinger Apr 25 '15 at 01:29
  • I haven't fixed it yet. – DeadWalker01 Apr 25 '15 at 01:29
  • possibly include your code in a single file to make sense of things – Sarang Apr 25 '15 at 01:31
  • Sorry for the misunderstanding. When i copied my code it may have bumped up a line. The actual line 13 in main.c is : if (abs_complex(mandelbrot(15,c)) > 100) – DeadWalker01 Apr 25 '15 at 01:40
  • Note that there is a standard header `` in C99 and C11. However, it does not define `complex_t` so you are probably OK. – Jonathan Leffler Apr 25 '15 at 01:47

2 Answers2

1

Your header complex.h declares:

complex_t abs_complex(complex_t c);

Your implementation complex.c defines:

double abs_complex(complex_t c) {

So what's happening is that your code won't compile because the abs_complex in the header file is saying that it returns a complex_t and not a double.

Presumably complex.c also doesn't compile because of the mismatched definition/declaration.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

You have a line:

if (abs_complex(mandelbrot(15,c)) > 100)

You are trying to compare an int with a complex_t, and that simply isn't allowed. You'll need to rethink your logic. You can't simply compare complex numbers for > (or < or …). You will either need to convert the complex result from abs_complex() into a scalar (double or int) or convert the 100 into a complex_t and call a custom comparison function.

There's a similar error in mandelbrot.c.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278