Java Array - Calculates the average of surrounding numbers

Ok, so I have an array like this:

1 2 3
4 5 6
7 8 9

It doesn't have to be any particular size, just showing it so you guys have something to see. So I have to go through each number and calculate the average number that surrounds it. For example, for the number 1, I have to calculate the average of 2, 5 and 4. I have to compare this average with the number itself, but this is not my problem. My problem is running through an array of any size (user defined) and exploring the numbers around it. Now I can program a separate method for each section, which means a separate method for each corner, a separate method for each side and a method for the center. But that sounds a lot bigger and much less effective. So, I decided to do something like an external border. This boundary will be filled with 0, assuming 0 is never an input value. It means,that I won’t get out of my array, but I can run one encoded function inside this border, so that it calculates the average of 8 surrounding numbers.

So, I am wondering how I can work with an array and ignore any 0 it encounters, but take the average of the other numbers around it?

Example:
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 7 8 9 0
0 0 0 0 0

therefore, in the first place it will take the average number of numbers 2, 4 and 5, but ignores 0. the reason why I need to ignore them is as follows:
1. I can not include them in the number, because although adding 0 will not affect on the total number, this will affect the number of numbers that need to be divided when getting the average value of 2. In fact, I will not use 0, since 0 is a possible input, it will actually be -1. I put 0 here for ease of typing and reading.

Recommended best way around this?

EDIT2:

, , , . , , .

public static int calculate(int[][] array, int row, int col){
    double num = 0;//Used to find the total of real numbers to be divided by
    double total = 0;
    double average;

    for (int a = -1; a <= 1; a++) 
    {
        for (int b = -1; b <= 1; b++) 
        {
            if(row + a < 0 || col + b < 0)
            {
                continue;
            }
            if(a == 0 && b == 0)//if(row + a == row || col + b == col)
            {
                continue;
            }
            if(row + a >= r || col + b >= c)
            {
                continue;
            }
            else
            {                   
                num+=1;
                total+= array[row + a][b + col];
                System.out.println("row+a = " + (row + a) + " and col+b = " + (col + b));
            }
        }
     }

    average = (double)(total/num);
    count++;
    System.out.print(count + ". Average = " + total + " divided by " + num + " = " + average + "\n");

    if (average < array[row][col])
        return 255;
    else
        return 0;
}

( ) 0 255. -, : 0 255 0 255 0

0 0 255 0 0

255 0 255 0 255

255 0 0 0 0

0 255 0 0 255

0 255 0 255 0

0 0 0 0 0

255 0 255 0 255

0 0 0 0 0

255 0 255 0 255

, , , .

+3
3

, - .

Trivia - (Spacken?) , 30 .

, , . ,

1 2 3
4 5 6
7 8 9

, # 1 [0] [0], 8 , # 2?

+3

, , , . , , .

int sum = 0;
int amount = 0;
for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
        // check that cell (i0 + di, j0 + dj) is inside the matrix
        // and not the original cell (i0, j0)
        // increase 'sum' and 'amount' if necessary
    }
}

return (double) sum / amount;


, .

+3

, , , .

private static boolean lives(Point p, byte[][] squares) {
    int liveNeighboors = 0;
    for (int i = p.x - 1; i < p.x + 2; i++) {
        for (int j = p.y - 1; j < p.y + 2; j++) {
            if (new Point(i, j).equals(p))
                continue;
            try {
                liveNeighboors += squares[i][j];
            } catch (Exception e) {
                // this happens if you try to sum an edge space
            }
        }
    }
    if (squares[p.x][p.y] == 0) {
        return liveNeighboors == 3;
    } else {
        return liveNeighboors == 2 || liveNeighboors == 3;
    }
}

, , .

:

for(int i = 0;  i < arr.length; i++) {
    for (int j = 0;j < arr[0].length; j++) {
        lives(new Point(i,j), arr);
    }
}
+3

Source: https://habr.com/ru/post/1767816/


All Articles