user3214454
user3214454

Reputation: 3

Determine the Highest and Lowest per row in array

Please help me on this, I already have a code and it runs perfectly but the thing is I need to determine the Highest and Lowest per row. I don't know how to start, please help me and also please explain to me. here is the code:

int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}};
int row, col;
for (row=0;row<3;row++){
    int sum = 0;
    for (col=0;col<5;col++){      
        System.out.print(num[row][col]+"|");
        sum =sum+num[row][col];
    }
    System.out.println("sum = "+sum); 
}

Upvotes: 0

Views: 1075

Answers (6)

Hugo S. Mendes
Hugo S. Mendes

Reputation: 1086

You can do something like this:

int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}};
int row, col;
for (row=0;row<3;row++){
colcount = 0; //count if is the first column
mincol = 0;
maxcol = 0;
for (col=0;col<5;col++){    

    if(colcount == 0){ //if first time at the loop
        mincol = num[row][col]; //mincol will be the first column
        maxcol = num[row][col]; //maxcol will be the first column
        colcount++;
    }else{
        mincol = (mincol < num[row][col]) ? mincol : num[row][col]); //will verify if the mincol is really the lowest. If true, will maintain the mincol value.. else .. will get the current column.
        maxcol = (maxcol > num[row][col]) ? maxcol : num[row][col]); //same of mincol, but using maxcol.
    }
}

}

you can put the max and min col into an array.. it's up to you.

Hope it helps.

Upvotes: 0

Myth
Myth

Reputation: 446

try this....

  • Use a temporary array to sort each row.
  • Use that array to find the max and min values of each row.

    int[][] num = {{1, 4, 3, 0, 5}, {2, 4, 7, 8, 10}, {3, 9, 60, 20, 4}};

    int row, col;        
    for (row = 0; row < 3; row++) {
        int temp[]=num[row];        
        int sum = 0;
        for (col = 0; col < temp.length; col++) {
            for(int j=0;j<temp.length;j++){
                 if(temp[col]<temp[j]){
                     int a=temp[col];
                     temp[col]=temp[j];
                     temp[j]=a;                     
                 }
    
            }
    
        }
       System.out.println("ROW"+row+": min="+temp[0]+" max="+temp[temp.length-1]);
    }
    

Upvotes: 0

Lijo
Lijo

Reputation: 6788

 public static void main(String args[]) {
   int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}};

int row, col;

        for (row=0;row<3;row++){
            int sum = 0;
            int high=0,low=999999;
            for (col=0;col<5;col++){      
        System.out.print(num[row][col]+"|");
        sum =sum+num[row][col];
        if(num[row][col]>high)
        {
        high=num[row][col];
        }
        if(num[row][col]<low)
        {
        low=num[row][col];
        }
        }
        System.out.println("sum = "+sum); 
            System.out.println("higest:"+high+" and lowest="+low);
        }
    }

on each iteration just check each item and store it in two variables high AND low. for each loop we compare the values with high and low and change.

Upvotes: 0

Gorbles
Gorbles

Reputation: 1168

Creating low and high outside of the loop so they can be accessed throughout (i.e. later on in the program).

int low = -1;
int high = -1;

for(int n = 0; n < 3; n++) {
    int[] temp = num[1];
    for(int m : temp) {
        if(low == -1) {
            low = m;
        }

        if(high == -1) {
            high = m;
        }

        if(m < low) {
            low = m;
        } else if(m > high) {
            high = m;
        }
    }
}

Not sure if it can be optimised further, but hey.

Upvotes: 0

pisek
pisek

Reputation: 1448

Try:

    int [][] num = {{1,4,3,0,5},{2,4,7,8,10},{3,9,60,20,4}};
    for (int[] row : num) {
        int lowest=row[0], highest=row[0];
        for (int i : row) {
            if (i<lowest) {
                lowest=i;
            }
            if (i>highest) {
                highest=i;
            }
        }
        System.out.println("Lowest:"+lowest+"; Highest:"+highest);
    }

Hope it helps!

Upvotes: 0

Keppil
Keppil

Reputation: 46239

It is really similar to what you already have.

  1. Create variables to hold your min and max values instead of sum.
  2. Assign the first item in the row to both min and max (since that is the highest and lowest number so far).
  3. For each new number after the first, check if it is lower than min or higher than max, and in that case save that instead.

Upvotes: 2

Related Questions