java - How To Check For Diagonal Neighbors in a 2D Array and Set Current Value Equal to Number of Neighbors -



java - How To Check For Diagonal Neighbors in a 2D Array and Set Current Value Equal to Number of Neighbors -

i trying walk through 2d array , count diagonal neighbors equal 0, set current position equal number of neighbors. example:

0 5 0 9 0 5 0 3 1 9 4 6 7 0 0 9

should alter to

0 2 0 1 0 3 1 2 1 3 1 2 0 1 1 1

i using next code (i grab exceptions because there index out of bounds exceptions parameter numbers) :

int row, col, count; count = 0; // standard for-loop used in walking through 2d array (row = 0; row < num; row++) { (col = 0; col < num; col++) { // check top left neighbour seek { if (grid[row - 1][col - 1] == 0) { count++; } } grab (indexoutofboundsexception e) { } // check bottom left neighbour seek { if (grid[row - 1][col + 1] == 0) { count++; } } grab (indexoutofboundsexception e) { } // check top right neighbour seek { if (grid[row + 1][col - 1] == 0) { count++; } } grab (indexoutofboundsexception e) { } // check bottom right neighbour seek { if (grid[row + 1][col + 1] == 0) { count++; } } grab (indexoutofboundsexception e) { } // set current place in array equal number of 0 neighbors grid[row][col]=count; count = 0; } }

the issue output wrong. instead of supposed code, changes following:

0 2 0 1 0 3 1 2 1 2 1 1 0 0 0 0

so first 2 lines work , 3rd line has several obo errors. not sure what's wrong lastly line, , i'm not sure going wrong.

summary

original is:

0 5 0 9 0 5 0 3 1 9 4 6 7 0 0 9

that should alter to:

0 2 0 1 0 3 1 2 1 3 1 2 0 1 1 1

but i'm getting:

0 2 0 1 0 3 1 2 1 2 1 1 0 0 0 0

another illustration be:

original:

5 0 0 3 9 5 0 0 9 5 3 0 0 0 0 9 7 3 7 0 5 0 9 5 0 0 3 0 0 0 9 5 0 3 7 0

updated:

1 1 1 0 1 0 1 2 2 1 2 0 1 0 2 0 2 0 2 1 4 1 4 1 0 1 0 1 1 0 0 2 0 1 1 0

any suggestions much appreciated.

you start with

0 5 0 9 0 5 0 3 1 9 4 6 7 0 0 9

you start traversing top left and simultaneously modifying matrix. after 2 rows have been modified, intermediate matrix is

0 2 0 1 0 3 1 2 1 9 4 6 7 0 0 9

now take consideration element @ index [2][1]. in original matrix, had 3 0 neighbors, matrix has 2, hence difference in expected , obtained output.

make separate matrix store modified values.

java arrays multidimensional-array

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -