jasper93
jasper93

Reputation: 69

Finding min and max value in each column in array

I have a problem with my program. I want to print min and max in each column but It doesn't work properly. I think everything should be ok. When the loop comes to end I restart min and max value.


public class tablice2 {
  public static void main(String[] args){
      int t [][] = new int [5][5];
      int n [] = new int [5];
      int x [] = new int [5];
      Random r = new Random();
      int min  = t[0][0];
      int max = t[0][0];


      for (int i = 0; i <t.length ;i++){
          min = 0;
          max = 0;
          for(int j = 0; j < t[i].length ;j++){
            t[i][j] = r.nextInt(6)-5;
              System.out.print(t[i][j] + " ");

              if (t[j][i] < min){
                  min = t[j][i];
              }

              if (t[j][i] > max){
                  max = t[j][i];
              }

          }
          n[i]=min;
          x[i]=max;


          System.out.println(" ");
      }

      for(int p=0;p<x.length;p++){
          System.out.println("Max Column "+p + ": " +x[p] );
      }

      for(int k=0;k<n.length;k++){
          System.out.println("Min Column "+k + ": " +n[k]);
      }



  }
}

Upvotes: 3

Views: 278

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79005

Do not input and sort at the same time as the elements may still be initialized with the default values (i.e 0). Also, in the outer loop, reset max and min to the first element of the column.

Do it as follows:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        int t[][] = new int[5][5];
        int n[] = new int[5];
        int x[] = new int[5];
        Random r = new Random();
        int min;
        int max;

        for (int i = 0; i < t.length; i++) {
            for (int j = 0; j < t[i].length; j++) {
                t[i][j] = r.nextInt(10) - 5;
                System.out.printf("%4d", t[i][j]);
            }
            System.out.println();
        }

        for (int i = 0; i < t.length; i++) {
            min = t[0][i];
            max = t[0][i];
            for (int j = 0; j < t[i].length; j++) {
                if (t[j][i] < min) {
                    min = t[j][i];
                }
                if (t[j][i] > max) {
                    max = t[j][i];
                }
            }
            n[i] = min;
            x[i] = max;
        }

        for (int p = 0; p < x.length; p++) {
            System.out.println("Max Column " + p + ": " + x[p]);
        }

        for (int k = 0; k < n.length; k++) {
            System.out.println("Min Column " + k + ": " + n[k]);
        }
    }
}

A sample run:

   3  -4   2   0   1
  -2  -2   4  -1  -2
  -3   1   4  -1   0
  -4   4  -2  -5   2
  -5  -3  -3  -4  -1
Max Column 0: 3
Max Column 1: 4
Max Column 2: 4
Max Column 3: 0
Max Column 4: 2
Min Column 0: -5
Min Column 1: -4
Min Column 2: -3
Min Column 3: -5
Min Column 4: -2

Notes:

  1. I have changed r.nextInt(6)-5 to r.nextInt(10) - 5 in order to produce a mix of negative, 0 and positive numbers so that you can quickly validate the result. You can change it back to r.nextInt(6)-5 as per your requirement.
  2. I have also used printf instead of print to print each number with a space of 4 units. You can change it back to print if you wish so.
  3. Use of Integer.MAX_VALUE and/or Integer.MIN_VALUE is not required at all to solve this problem.

Feel free to comment in case of any doubt.

Upvotes: 2

Andrew
Andrew

Reputation: 88

First in loop set your min to min = Integer.MAX_VALUE and similarly with max = Integer.MIN_VALUE. That will provide us that every random number will be (at least) smaller than initialized min (and similar for max)

In this case, you are draw numbers from -5 to 0 so you can set min as for example -6 and max 1 and then you are sure the drawed number will be higher than -6 and less than 1 and will update correctly

In your code you mixed i and j - it should be

if (t[i][j] < min){
    min = t[i][j];
}

and similarly in the rest of the code

Also when you create an array in Java it is automatically initialized with 0's. So

int t [][] = new int [5][5];
int min  = t[0][0];

means min = 0 because all array is filled by 0's so you don't have to duplicate your code

Upvotes: 0

Tix
Tix

Reputation: 614

Your first loop must be over columns(j).

t[0].length

Upvotes: 0

Darkwyng
Darkwyng

Reputation: 140

You have initialized min as zero, I'd suggest Integer.MIN_VALUE. Otherwise positive values cannot be "found".

This is also the kind of problem that somebody has already solved for you, e.g. via streams:

assertThat(
        IntStream.of(new int[] { 1, 2, 3, 4, 5 }).max().getAsInt(), 
        is(5));

Upvotes: 2

Related Questions