My Works | Matrix Problem 02


In a given matrix all the cells will contain a value of 1 or 0. Change the value of a cell to 1 if the summation of its 8 adjacent cells are 3 or greater than 3, otherwise change the value to 0. Boundary cells can have 3 or 5 or less adjacent cells depending on the size of the matrix, so for boundary cells you will have to calculate over them. The number of rows and columns will be equal. For example, you might be given a 8x8 matrix. The values will be given by the user. You can keep the number of cells 8x8 or you can make it variable or user inputted. 


Before getting in, first see what are adjacent cells. Cells which are surrounding a cell are called adjacent cells to that particular cell. For a better explanation look at the following picture.

Now look at the following pictures and their corresponding explanations, [Click the image to enlarge]

In this program we are having a 5x5 matrix as input [First part of the image]. Here the cell we are at is (1,1) value of which is 1 [Cell marked with GREEN color]. It is a boundary cell and it is having 3 adjacent cells which are (1,2),(2,2),(2,1) value of which are 0,1,0 respectively [cells marked with red color]. So the summation of the values of its adjacent cells are 0+1+0 = 1 which is smaller than 3. Thats why we are changing the value of the cell to 0 from 1 [cell marked with BLUE color in the 2nd part of the image].


Now we are at the cell (1,2) value of which is 0 [Cell marked with GREEN color in the 1st part of the image]. It is also a boundary cell and having 5 adjacent cells (1,1),(1,3),(2,3),(2,2),(2,1) value of which are 1,1,1,1,0 respectively [Cells marked with RED color, in the 1st part of the image], so the summation is 1+1+1+1+0=4 which is greater than 3. Thats why we can change its value to 1 from 0 [2nd part of the image, cell marked with BLUE color.]

So, similarly understand the following picture,

To test some input-output you can download the EXE from this link. [Click to Download Pixel-Operation-Demo]

If you need further explanation, ask me using the comments option.

Happy Coding.........


Never Miss What"s New !

No SPAM, only email notification if new posts were published.

Recommended Recommends


Contact Us