title: Sierpinski Carpet with recursion in C using SVG
body: <p>I am trying to create a Sierpinski carpet using the C language. I successfully created the first iteration. When the second iteration begins, the function correctly generates squares just around the first square. One more problem is that my code generates the middle square too (normally it should be 8 squares, not 9).</p>
  <p>Below is a picture and the code. I'm just out of ideas and I can't find where I went wrong.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  void draw(FILE *file, int size, int x, int y);
  void sierpenski(FILE *file,int iterations,int size, int x, int y);
  int main( int argc , char *argv[] ) {
      if( argc == 1)
      {
          printf(&quot;Not enough input arguments..\n&quot;);\
          exit(EXIT_FAILURE);
      }
      FILE *file;
      if(!(file = fopen(&quot;my.svg&quot;,&quot;w&quot;) ) )
      {
          printf(&quot;File has not been opened\n&quot;);
          exit(EXIT_FAILURE);
      }
      fprintf(file,&quot;&lt;svg height=\&quot;1000\&quot; width=\&quot;1000\&quot; fill=\&quot;blue\&quot;  xmlns=\&quot;http://www.w3.org/2000/svg\&quot; version=\&quot;1.1\&quot;&gt;\n&quot;);
      fprintf(file,&quot;&lt;rect x=\&quot;400\&quot; y=\&quot;400\&quot; height=\&quot;200\&quot; width=\&quot;200\&quot; /&gt;\n&quot;);
      int iterations = atoi(argv[1]);
      sierpenski(file,iterations,200,400,400);
      fprintf(file,&quot;&lt;/svg&gt;\n&quot;);
      return 0;
  }
  void draw(FILE *file, int size , int x , int y)
  {   int aux_size = size;
      size /= 3;
      x /= 3;
      y /= 3;
      for( int i = 0; i &lt; 9; i ++) {
          printf(&quot;%i\n&quot;,i);
  
              if( i == 1){
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;, x, y, size, size);
              }
              else if( i == 2)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;, x+2*aux_size-(aux_size/3), y, size, size);
              }
              else if( i == 3)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;, x+4*aux_size-2*(aux_size/3), y, size, size);
              }
              else if( i == 4)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;, x+4*aux_size-2*(aux_size/3), y+2*aux_size-(aux_size/3), size, size);
              }
              else if( i == 5)
              {
              fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;, x+4*aux_size-2*(aux_size/3), y+4*aux_size-2*(aux_size/3), size, size);
              }
              else if( i == 6)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;,x + 2 * aux_size -  (aux_size / 3), y + 4* aux_size - 2*(aux_size / 3), size, size);
              }
              else if( i == 7)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;,x, y+4*aux_size-2*(aux_size/3), size, size);
              }
              else if( i == 8)
              {
                  fprintf(file, &quot;&lt;rect x=\&quot;%i\&quot; y=\&quot;%i\&quot; height=\&quot;%i\&quot; width=\&quot;%i\&quot; /&gt;\n&quot;,x, y+2*aux_size-(aux_size/3), size, size);
              }
          }
      }
  
  
  void sierpenski( FILE *file,int iterations,int size, int x, int y) {
      if (iterations == 0)
          return;
      int aux_size = size;
  
      draw(file, size, x, y);
              for( int i = 1 ; i &lt; 9 ; i++) {
                  printf(&quot;%iiteratii sie\n&quot;,i);
              if( i == 1)
              {
                  sierpenski(file, iterations - 1, size / 3, x / 3, y / 3);
              }
              else if( i == 2)
              {
                  sierpenski(file, iterations - 1, size / 3, x+4*aux_size-(aux_size/3), y / 3);
              }
              else if( i == 3)
              {
                  sierpenski(file, iterations - 1, size / 3, x+9*aux_size-aux_size/3, y / 3);
              }
              else if( i == 4)
              {
                  sierpenski(file, iterations - 1, size / 3, x+9*aux_size-aux_size/3, y + 4*aux_size-(aux_size/3));
              }
              else if( i == 5)
              {
                  sierpenski(file, iterations - 1, size / 3, x+9*aux_size-aux_size/3, y+9*aux_size-aux_size/3);
              }
              else if( i == 6)
              {
                  sierpenski(file, iterations - 1, size / 3, x+4*aux_size-aux_size/3, y+9*aux_size-aux_size/3);
              }
              else if( i == 7)
              {
                  sierpenski(file, iterations - 1, size / 3, x/3, y+9*aux_size-aux_size/3);
              }
              else if( i == 8 )
              {
                  sierpenski(file, iterations - 1, size / 3, x/3, y+4*aux_size-aux_size/3);
              }
              }
  
  
  }
  </code></pre>