java - Why is my count that is being returned a wrong value? -


    public static int countneighbors(boolean[][] bb, int r, int c) {     int countalive = 0;     (int = r -1;i<=r+1; i++) {         (int j = c -1; j<c+1;j++) {              if (bb[i][j]) {                 countalive++;              }          }     }     return countalive; } 

matrix being read.

oooooooo o###oooo o####o#o ooo##o#o o#o#o##o oooooooo 

i noticed wrong printed out portion of code. when ran specifications

countneighbors(mynewmatrix,1,1) 

i returned value of 2, when should in face 3.

it counting number of tiles true(#) around it.

this "game of life" assignment.

there 3 neighbors of (1,1) @ (1,2), (2,1), , (2,2). code wrong on 2 accounts:

  1. you counting cell (1,1). makes count 1 high. introduce if avoid counting (r,c) location itself.
  2. you stopping in j for loop, before gets c + 1. makes count 2 low (missing 2 matches). change condition j<=c+1, consistent i for loop condition.

the combined effects of 2 errors (+1 , -2) explain why count low 1.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -