Largest and smallest value of each row of the two-dimensional array

Asked

Viewed 138 times

2

I’m trying to get the lowest and highest value of each line in a two-dimensional array with java, but I’m getting the incorrect values. Follows the code:

int[][] arrayValues = {
    { 21, 33, 70, 16, 70, 80, 67, 21 },
    { 54, 93, 36, 80, 48, 41, 14, 5 },
    { 6, 91, 81, 14, 37, 91, 98, 35 },
    { 51, 20, 54, 46, 59, 72, 65, 79 },
    { 4, 34, 95, 74, 14, 61, 94, 68 }
};

int min = arrayValues[0][0];
int max = arrayValues[0][0];
int[] minValue = new int[arrayValues.length];
int[] maxValue = new int[arrayValues.length];

for (int i = 0; i < arrayValues.length; i++) {
    for (int j = 0; j < arrayValues[i].length; j++) {
        if (arrayValues[i][j] < min) {
            minValue[i] = arrayValues[i][j];
        }

        if (arrayValues[i][j] > max) {
            maxValue[i] = arrayValues[i][j];
        }
    }
    System.out.println("Min: " + minValue[i] + " Max: " + maxValue[i]);
}

The minimum returned values are: 16, 14, 14, 20 and 14 and the maximum ones: 67, 50, 35, 79 and 68, but the minimum ones should be: 16, 14, 6, 20 and 14 and the maximum ones: 80, 93, 98, 79 and 95. I don’t know what I’m doing wrong, since in some lines it gets the right value.

  • You want the biggest and smallest of each line use for (int i = 0; i < arrayValues.length; i++) { int min = arrayValues[i][0]; int max = arrayValues[i][0]; for (int j = 0; j < arrayValues[i].length; j++) { and not once for any array.

2 answers

6


The anonymous, in his comment, killed the riddle of what is happening. You need to isolate the minimum and maximum for each index i. It is also necessary to update the values of max and min us loops internal (see the response of Bernardo).

That said, you can use streams to achieve the same result, which greatly simplifies the code:

Arrays.stream(arrayValues)
    .map(a -> Arrays.stream(a))
    .map(s -> s.summaryStatistics())
    .forEach(e -> System.out.printf("Min: %02d Max: %02d\n", e.getMin(), e.getMax()));

See it working on ideone.com

The first line converts the matrix to a stream of arrays whole. The second line converts each array of integers (i.e., each row of the matrix) in a IntStream. The third line gets an object IntSummaryStatistics containing statistics of each stream internal. Finally the last line prints the minimum and maximum of each IntSummaryStatistics.

Exit:

Min: 16 Max: 80
Min: 14 Max: 93
Min: 06 Max: 98
Min: 20 Max: 79
Min: 04 Max: 95

This is one of many occasions when the functional version is more succinct and readable than the mandatory version. The class IntSummaryStatistics certainly is encapsulated part of the complexity of maximum and minimum operations, however, even if IntSummaryStatistics and the methods IntStream.min() / IntStream.max() did not exist yet it would be trivial to reduce the stream and get the results manually (e. g., .reduce(Math::min)). In my view, even a programmer who is a layman in functional programming will be able to hit the above code and quickly understand that the intention is to calculate minimums and maximums per line. The imperative version may be a little more difficult to understand.

4

public class Strack {
    public static void main(String[] args) {
       int[][] arrayValues = {
            { 21, 33, 70, 16, 70, 80, 67, 21 },
            { 54, 93, 36, 80, 48, 41, 14, 5 },
            { 6, 91, 81, 14, 37, 91, 98, 35 },
            { 51, 20, 54, 46, 59, 72, 65, 79 },
            { 4, 34, 95, 74, 14, 61, 94, 68 }
        };

        int min;
        int max;
        int[] minValue = new int[arrayValues.length];
        int[] maxValue = new int[arrayValues.length];

        for (int i = 0; i < arrayValues.length; i++) {
            min = arrayValues[i][0];
            max = arrayValues[i][0];
            for (int j = 0; j < arrayValues[i].length; j++) {
                if (arrayValues[i][j] < min) {
                    min = arrayValues[i][j];
                }
                if (arrayValues[i][j] > max) {
                    max = arrayValues[i][j];
                }
            }
            minValue[i] = min;
            maxValue[i] = max;
            System.out.println("Min: " + minValue[i] + " Max: " + maxValue[i]);
        }
    }
}

Initial version issues that have been fixed:

  1. The values of min and max should be initialized with the first value of each subvector. Thus we avoid that these variables store values of subvectors other than the current one.
  2. In the internal loop the comparison should be made with the whole subvector, and if the value in [i][j] is less than min, min is updated with the new value.
  3. Analogously to compute the maximum, if the value in [i][j] is greater than max, max becomes the new value.
  4. At the end of loop intern, min and max shall have the minimum and maximum values on each line and shall be assigned respectively to minValue[i] and maxValue[i].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.