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:
- you counting cell (1,1). makes count 1 high. introduce
if
avoid counting (r,c) location itself. - you stopping in
j
for
loop, before getsc + 1
. makes count 2 low (missing 2 matches). change conditionj<=c+1
, consistenti
for
loop condition.
the combined effects of 2 errors (+1 , -2) explain why count low 1
.
Comments
Post a Comment