title: Having issues finding the bug with implementing Conway's Game of Life. Below is the source code
body: <p>The rules of Conway's Game of life is each cell on a grid has two states: living or dead. 1. If a living cell has fewer than 2 or more than 3 neighbors, the cell dies. 2.If a living cell has 2 or 3 neighbors, it lives on. 3.If a dead cell has exactly 3 neighbors, it is born in the next generation.</p>
  <p>I first created two small 5 by 5 grid with arrays to contain the cells. One array is for the previous configuration and another array is for the updated new array. &quot;-&quot; symbolizes a dead cell and &quot;O&quot; symbolizes an alive cell. I set up a loop that prints out the initial/old configuration, update the configuration and store it in the new array, and rewrote the old array with the updated array. Then the terminal would be emptied out. The next iteration would then print the updated world. However, the second generation is incorrect. I'm not sure where the problem lies.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  void update_world(void);
  void print_world(void);
  int check(int width, int height);
  
  //create a 5 by 5 array which would be the &quot;world&quot;
  char cell[5][5]; 
  char new_cell[5][5];
  
  int main(void) {
    
    //initialize array to &quot;-&quot;s (dead)
    for (int i = 0; i &lt; 5; i++)
    {
      for (int j = 0; j &lt; 5; j++)
      {
        cell[i][j] = '-';
      }
    }
    
    //initialize configuration ie. put some life into the world 
    cell[0][1] = 'O';
    cell[4][0] = 'O';
    cell[1][2] = 'O';
    cell[1][4] = 'O';
    cell[4][4] = 'O';
    cell[0][2] = 'O';
  
    //outer loop for iteration of each cycle
    for (int i = 0; i &lt; 10; i++) //goes for 10 generations
    {
      print_world(); 
      update_world();
      //write new cell into old cell before the new cell updates
      for (int i = 0; i &lt; 5; i++)
      {
        for (int j = 0; j &lt; 5; j++)
        {
          cell[i][j] = new_cell[i][j];
        }
      }
      sleep(1);
      printf(&quot;\033[2J\033[H&quot;); //clear terminal 
    }
  }
  
  //print out the world onto terminal
  void print_world(void)
  {
    for (int column = 0; column &lt; 5; column++)
    {
      for (int row = 0; row &lt; 5; row++)
      {
        if (row % 5 == 0)
          printf(&quot;\n&quot;);
        printf(&quot;%c&quot;, cell[row][column]);
      }
    }
    printf(&quot;\n&quot;);
  }
  
  //update world
  void update_world(void)
  {
    //traverse world
    for (int i = 0; i &lt; 5; i++)
    {
      for (int j = 0; j &lt; 5; j++)
      {
        //checks the conditions to decide whether the cell is alive or dead in the next generation
        if (check(i, j) == 1) 
          new_cell[i][j] = '-'; //cell dies
        else if (check(i,j) == 0){
          new_cell[i][j] = 'O'; //cell is born
        }
        else 
          new_cell[i][j] = cell[i][j]; //cell stays the same
      }
    } 
  }
  
  //check if cell should die, be born, or stay as is
  int check(int width, int height)
  {
    int alive = 0;
  
    //traverse cell's neightboring cells (including itself)
    for (int h = height - 1; h &lt;= height + 1; h++)
    {
      for (int w = width - 1; w &lt;= width + 1; w++)
      {
        //if cell (h,w) is a surrounding cell, within bounds of the grid, and is alive 
        if (!(h == height &amp;&amp; w == width) &amp;&amp; h &gt;= 0 &amp;&amp; h &lt; 5
         &amp;&amp; w &gt;= 0 &amp;&amp; w &lt; 5 &amp;&amp; cell[h][w] == 79)
          alive++;//increment alive count
      }
    } 
  
    //check conditions 
    if (alive &gt; 3 || alive &lt; 2)
      return 1; //cell dies
    else if (alive == 3)
      return 0; //cell is born or lives on to next generation
    return 2; //alive count = 2, nothing changes
  }
  </code></pre>