title: Conway's Game Of Life In C
body: <p>I have made a version of conways game of life in C, using a 2d array which should wrap around the sides. Unfortunately all that happens is the numbers flick back and forth between 1 and 0 with no clear pattern. Here is the code:</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  int main(){
      int const WIDTH = 100;
      int const HEIGHT = 100;
      int const CYCLES = 1000;
      int grid[HEIGHT][WIDTH];
      int temp[HEIGHT][WIDTH];
  
      int row;
      int col;
      for(row = 0; row &lt; HEIGHT; row++){
          for(col = 0; col &lt; WIDTH; col++){
              grid[row][col] = 0;
          }
      }
  
      int i;
      int x;
      int y;
      int neighbours;
      for(i = 0; i &lt; CYCLES; i++){
          for(row = 0; row &lt; HEIGHT; row++){
              for(col = 0; col &lt; WIDTH; col++){
                  temp[row][col] = 0;
              }
          }
          for(row = 0; row &lt; HEIGHT; row++){
              for(col = 0; col &lt; WIDTH; col++){
                  neighbours = 0;
                  for(y = -1; y &lt; 2; y++){
                      for(x = -1; x &lt; 2; x++){
                          if(x != 0 &amp;&amp; y != 0 &amp;&amp; grid[(row + y) % HEIGHT][(col + x) % WIDTH] == 1){
                              neighbours++;
                          }
                      }
                  }
                  if(grid[row][col] == 1){
                      if(neighbours &lt; 2 || neighbours &gt; 3){
                          temp[row][col] = 0;
                      }else{
                          temp[row][col] = 1;
                      }
                  }else if(grid[row][col] == 0){
                      if(neighbours == 3){
                          temp[row][col] = 0;
                      }else{
                          temp[row][col] = 1;
                      }
                  }
              }
          }
          for(row = 0; row &lt; HEIGHT; row++){
              for(col = 0; col &lt; WIDTH; col++){
                  grid[row][col] = temp[row][col];
                  printf(&quot;%d&quot;, grid[row][col]);
              }
              printf(&quot;\n&quot;);
          }
          printf(&quot;\n&quot;);
      }
  }
  </code></pre>