Let's image there's a 3x3 array. I want each element in the array be the average of it's neighbor. How to do it with just ONE nested for loop.
1 2 3 4 5 6 7 8
int[][] arr = newint[3][3];
// suppose I already initialized each element from 1 to 9 and made a copy of the array call cpyarr.
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
int a = cpyarr[i-1][j-1] + cpyarr[i][j-1] + cpyarr[i+1][j-1] +...+cpyarr[i+1][j+1];
arr[i][j] = a;}}
This code is going to cause error because it will go out of bound for cases near the boundary and corner. I know how to solve the problem but I don't know how to solve the problem with ONE nested for loop.