title: Why does my matrix dawing algrorithm not work?
body: <p>I am attempting to make my own matrix writing algorith that writes a matrix to the C console using a Run Length Encoding. My idea is that instead of printing every single value in a matrix we could clump up the matrix values in a row that are the same into one line and print that line, then do the same for the next group of values that are the same and continue doing this till we reach the end of the row, this would allow me do use the<code>SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE))</code> for coloured ASCII graphics as I could just colour each group. As an example if we had <code>01110000</code> as a row in our matrix we would use three print commands only: one for the initial zero, another one for the 3 ones and a finall one for the 4 zeroes. We could do this to avoid wasting proccesing time printing all the values to the console individually, the problem is that there is a lot of undefined behaviour in my code and this causes the console output to be very different to the expected one.</p>
  <p>code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;windows.h&gt;
  
  const int sizeX = 20;
  const int sizeY = 20;
  char line[20];
  
  int grid[20][20] = {{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},               
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                       
                      {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},                               
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                       
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                                   
                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
                                                                                  
  void clearScreen(){ 
   COORD cursorPosition;  cursorPosition.X = 0;   cursorPosition.Y = 0;   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
  }
  
  void draw_screen(){
   clearScreen();
   int y, x;
   for(y=0;y&lt;sizeY;y++){
    for(x=0;x&lt;sizeX;x++){
      if(grid[y][x] == 1){
          line[x] = '1';
          if(grid[y][x+1] != 1){
              fputs(line, stdout);
              if(grid[y][x+1] &gt; sizeX){
                  putc('\n', stdout);
              }
          }
      }
      else if(grid[y][x] == 2){
          line[x] = '2';
          if(grid[y][x+1] != 2){
              fputs(line, stdout);
              if(grid[y][x+1] &gt; sizeX){
                  putc('\n', stdout);
              }
          }
      }
      else if(grid[y][x] == 0){
          line[x] = '0';
          if(grid[y][x+1] != 0){
              fputs(line, stdout);
              if(grid[y][x+1] &gt; sizeX){
                  putc('\n', stdout);
              }
          }
      }
    }
    putc('\n', stdout);
   }
  } 
  
  int main(void) {
   int x, y;
   float frameTime, FPS;
  
   while (1) {
    clock_t start = clock();
    draw_screen();
    clock_t stop = clock();
    frameTime = (float)(stop - start) / CLOCKS_PER_SEC;
    FPS = 1.0 / frameTime;
    printf(&quot;FPS: %f\n&quot;, FPS);
   }
  }
  </code></pre>
  <p>output:</p>
  <pre><code>100000000000000000001000000000000000000010000000000000001110
  
  
  
  0000000000000000000000000000010000000000
  
  
  
  00000000000000000000000100000000000000000001000000000000000000010000010000000000
  
  
  
  0000000000000000000000000000000200000000
  
  
  
  
  
  
  00000000000000000000
  </code></pre>
  <p>expected output:</p>
  <pre><code>10000000000000001110
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000010000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00010000010000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000000200000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  00000000000000000000
  </code></pre>
  <p>expected FPS:     100-500</p>