title: Search for the most populated area in a game of life matrix
body: <p>I programmed a game of life in C and it works quiet well, the only problem the game board is much bigger than can be displayed. I know the number of rows and and the columns the that the monitor can display and I thought it would be a great idea following the action so to say (searching for the most populated area). Therefor I wrote a little function to receive the 4 indexes of the corners of the part of the  field I should display, but somehow the code seems to give random sections, which sometimes are obliviously false, the calculated worth of the living cells (max) is completely false (sometimes even bigger than the whole matrix it self). Here is the code:</p>
  
  
  
  <pre><code>int* most_populated_area(int m, int n, bool a[m][n], int r, int c){
      int * rr=malloc(sizeof(int)*4); //return value
      rr[0]=0;
      rr[1]=r;
      rr[2]=0;
      rr[3]=c; //we first assume that the most populated arrea is upper left corner
      int summe=0;
      int max=0; //to controll wheater or not we found a more populated area we need a summe and the max we founded until now
      for(int isearch=0; isearch&lt;m; isearch++){ //we search the matrix row for row
          for(int jsearch=0; jsearch&lt;n; jsearch++){ // and column for column
              for(int ik=0; ik&lt;=r; ik++){ // now from the current flied we go as many rows we are allowed
                  for(int jl=0; jl&lt;=c; jl++){ //and also as many columns we are allowed to go
                      int iks=isearch+ik; //we looking on the field is 0 to r
                      int jls=jsearch+jl; //and 0 to c flieds away from our current field
                      if(iks&gt;=m){ //if the field is outside the matrix we have to go back  
                          iks%=r; // iks is clearly bigger than r (because m bigger than r) but iks%r can be r-1 at most,
                          //so we have the rest from iks through r over the edge 
                      }
                      if(jls&gt;=n){ //analog to iks
                          jls%=c;
                      }
                      summe+=a[iks][jls];
                  }
              }
              if(summe&gt;max){// the summe is greater than the max we found a more populated area and we save the the results
                  max=summe;
                  rr[0]=isearch;
                  rr[1]=(isearch+r&gt;=m)?(isearch+r)%r:isearch+r;
                  rr[2]=jsearch;
                  rr[3]=(jsearch+c&gt;=n)?(jsearch+c)%c:jsearch+c;
              }
              summe=0; //anyway we reset the summe to search accurate
          }
      }
      printf("Lifing in this area %d \n", max);
      return rr;
  }
  </code></pre>