title: What's wrong with my Mandelbrot set code?
body: <p>I'm trying to implement the Mandelbrot set in C, but I'm having a weird problem. My code is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;complex.h&gt;
  
  int iterate_pt(complex c);
  
  int main() {
  FILE *fp;
  fp = fopen("mand.ppm", "w+");
  
  
  double crmin = -.75;
  double crmax = -.74;
  double cimin = -.138;
  double cimax = -.75; //Changing this value to -.127 fixed my problem.
  
  int ncols = 256;
  int nrows = 256;
  int mand[ncols][nrows];
  int x, y, color;
  double complex c;
  
  double dx = (crmax-crmin)/ncols;
  double dy = (cimax-cimin)/nrows;
  
  for (x = 0; x &lt; ncols; x++){
      for (y = 0; y &lt; nrows; y++){
          double complex imaginary = 0+1.0i;
          c = crmin+(x*dx) + (cimin+(y*dy)) * imaginary;
          mand[x][y] = iterate_pt(c);
      }
  }
  
  printf("Printing ppm header.");
  fprintf(fp, "P3\n");
  fprintf(fp, "%d %d\n255\n\n", ncols, nrows);
  
  for (x = 0; x &lt; ncols; x++) {
      for (y = 0; y &lt; nrows; y++){
          color = mand[x][y];
          fprintf(fp, "%d\n", color);
          fprintf(fp, "%d\n", color);
          fprintf(fp, "%d\n\n", color); //Extra new line added, telling the ppm to go to next pixel.
      }
  }
  fclose(fp);
  
  return 0;
  }
  
  int iterate_pt(double complex c){
  double complex z = 0+0.0i;
  int iterations = 0;
  int k;
  for (k = 1; k &lt;= 255; k++) {
      z = z*z + c;
      if (sqrt( z*conj(z) ) &gt; 50){
          break;
      }
      else
          ++iterations;
  }
  return iterations;
  }
  </code></pre>
  
  <p>However, the output of this program, which is stored as a ppm file looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/iNLaP.gif" alt="Converted to a GIF using GIMP. I can confirm that the GIF and original PPM look exactly the same as a PPM and GIF"></p>
  
  <p>Thanks for your help!</p>