title: Wrapping around a grid with modulo
body: <p>I am trying to write a program that implements Conway's Game of Life on a grid of 20x60 cells. The grid should wrap around so that the left side is connected to the right side and the top is connected to the bottom. </p>
  
  <p>Thus, any cell with position <code>(0, col)</code>, will have a neighbour at <code>(maxRow, col)</code>. Any cell with position <code>(row, 0)</code> will have a neighbour at <code>(row, maxCol)</code>.</p>
  
  <p>The following function is supposed to count the number of neighbouring cells. It works for coordinates not on the edges, but not for ones that are. For instance, if there are points at <code>(0, 10)</code>, <code>(0, 11)</code>, <code>(0, 12)</code>, and <code>(0, 10)</code> is passed into the function, it will return a high number as neighbour count instead of <code>1</code>. I know that the mod operator <code>%</code> would be helpful, but I don't understand how to use it.</p>
  
  <pre><code>{
      int i, j;
      int count = 0;
      for (i = row - 1; i &lt;= row + 1; i++)
         for (j = col - 1; j &lt;= col + 1; j++) 
             count += grid[i][j]; }
  
      if (row==maxrow-1 || row==0)
           count = count+ grid [(row-(maxrow-1))*-1][col-1]+grid[(row-(maxrow-1))*-1][col]+grid[(row-(maxrow-1))*-1][col+1];
  
      if (col==0 || col==maxcol-1)
           count=count +grid[row-1][(col-(maxcol-1))*-1]+grid[row][(col-(maxcol-1))*-1]+grid[row+1][(col-(maxcol-1))*-1];
  
  
  
      count -= grid[row][col];
      return count;
      } 
  </code></pre>