[Solved] Smallest element array higher than average


if (averageElements > array[i][j]) means that you’re only looking at values less than the average, exactly opposite of what you want.

tmp1 = 0 and if (array[i][j] > tmp1) means that you are looking for the largest value above zero, also exactly opposite of what you want. And it wouldn’t work if all values were negative.

Instead, try this:

int minValue = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length; j++) {
        int value = array[i][j];
        if (averageElements < value && value < minValue) {
            minValue = value;
        }
    }
}
System.out.println("Smallest element array higher than average " + minValue);

solved Smallest element array higher than average