2

I am trying to create a program, which counts the minimum of each dimension in a two dimensional array. So for ex. if i had an array:

int[][] test = {{1,2,3},{2,3,4},{4,5,6}}

the program would display: [1,2,4] - the minimum of each dimension. For that I've created a method called minimum, which looks like this

static int[] minimum(int[][] arr) {
        int[] result = new int [arr.length];
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                int  min = arr[i][0];
                if(arr[i][j] < min) {
                    min = arr [i][j];
                    result [i] = min;
                } else{

                }
            }
        }
        return result;
    }

But when i call out this method in my main, with a sample array

public static void main(String[] args) {
            int[][] arr = {{1,2,3,},{3,4,5},{6,6,6}};
        System.out.println(Arrays.toString(minimum(arr)));


    }

The program displays [0,0,0,]. Do You have any clue where is the problem and how to fix it?

6
  • 1
    Well, int min = arr[i][0]; needs to move up one line, to outside the inner loop. But I can't see why you're getting [0,0,0]. Commented Jul 24, 2018 at 22:49
  • 2
    return Arrays.stream(arr).mapToInt(a -> Arrays.stream(a).min().getAsInt()).toArray(); Commented Jul 25, 2018 at 0:28
  • 1
    @DawoodibnKareem Result is [0,0,0] because if(arr[i][j] < min) is never true given that min = arr[i][0] and first element of each sub-array is the smallest, so nothing is ever assigned to result array. Commented Jul 25, 2018 at 0:32
  • Of course. How silly of me. Commented Jul 25, 2018 at 0:53
  • @shmosel nice Java 8 implementation. Commented Jul 25, 2018 at 1:10

2 Answers 2

3

The problem is that if the first element in the array is min, it never gets recorded to the result array. Try:

static int[] minimum(int[][] arr) {
    int[] result = new int[arr.length];

    for (int i = 0; i < arr.length; i++) {
        result[i] = arr[i][0];

        for (int j = 1; j < arr[i].length; j++) {
            if (arr[i][j] < result[i]) {
                result[i] = arr[i][j];
            }
        }
    }

    return result;
}

Note that there needs to be at least one element per row in the input matrix for the above function; add a conditional or use Integer.MIN_VALUE to handle empty rows if you wish.

Sign up to request clarification or add additional context in comments.

Comments

0

This should work. You reset the min to the first element every time. So you are basically comparing if there is any value smaller than the first one.

    static int[] minimum(int[][] arr){
        int[] result = new int [arr.length];
        for (int i = 0; i < arr.length; i++){
            result[i] = Integer.MAX_VALUE;
            for(int j = 0; j < arr[i].length; j++){
                if(arr[i][j] < result[i]) {         
                    result [i] = arr[i][j];
                }
            }
        }
        return result;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.