Skip to main content

Trying to get the max/min in array



Im trying to get the min value in a column in the row of the max value. Im trying to using for loops but its not working.





Let say a matrix A:





3 4 5


2 3 4


1 2 3





I want the program to find the max value in the [0]th row then find the min value in the colunm of the max. So result should be : max of row [0] = 5, min of column [2] = 3. I then want it to do the same of all the rows, that why I ysed a while loop.





Here is the matrix:







public int[][] createMatrix(int a, int b){

Scanner inputm = new Scanner(System.in);

A = new int[a][b];

System.out.println("Enter elements for matrix A : ");

for (int i=0 ; i < A.length ; i++){

System.out.println("Enter numbers for " + i +"th row");

for (int j=0 ; j < A[i].length ; j++){

A[i][j] = inputm.nextInt();



}



}

return A;

}



public int[][] displayMatrix(){

System.out.println("Matrix A: ");



for (int i=0 ; i < A.length ; i++)

{ System.out.println();

for (int j=0 ; j < A[i].length ; j++){

System.out.print(A[i][j]+" ");

}

}





return A;



}





public int getMaximumOfEveryRow (int c){

a=c;

int i= 0;

int j;

while(i < A[a].length){



max = Integer.MIN_VALUE;

for ( j = 0; j < A [ i ].length; j++ )

if ( A [ i ] [ j ] > max ){

max = A [ i ] [ j ];

}







for ( i = 0; i < A [ i ].length; i++ )// e

if ( A [ i ] [ j ] < min ){

min = A [ i ] [ j ];

}



System.out.println( "\n Maximum of row " + j + " = " + max );

System.out.println( "Minimum of column " + i + " = " + min );

if(max == min){

System.out.println( min+ " = " + max );

System.out.println( "This is a saddle point. ");

}









i++;



}

return max;





}







and this is what I have so far:







public int getMaximumOfEveryRow (int c){

a=c;

int i= 0;

int j;

while(i < A[a].length){



max = Integer.MIN_VALUE;

for ( j = 0; j < A [ i ].length; j++ )

if ( A [ i ] [ j ] > max ){

max = A [ i ] [ j ];

}





int e = j;

int r;

for ( i = 0; i < A [ i ].length; i++ )// e

if ( A [ i ] [ j ] < min ){

min = A [ i ] [ j ];

}



System.out.println( "\n Maximum of row " + j + " = " + max );

System.out.println( "Minimum of column " + i + " = " + min );

if(max == min){

System.out.println( min+ " = " + max );

System.out.println( "This is a saddle point. ");

}









i++;



}

return max;





}

public int getMaximumOfEveryColumn ()

{

for ( int i = 0; i < A.length; i++ )

{

maxc = Integer.MIN_VALUE;

for ( int j = 0; j < A [ i ].length; j++ )

if ( A [ j ] [ i ] > maxc )

maxc = A [ j ] [ i ];

System.out.println( "Maximum of column " + i + " = " + maxc );

}

return maxc;

}



public int getMinimumOfEveryColumn_(){

for ( int i = 0; i < A.length; i++ )

{

minc = Integer.MAX_VALUE;

for ( int j = 0; j < A [ i ].length; j++ )

if ( A [ j ] [ i ] < minc )

minc = A [ j ] [ i ];

System.out.println( "Minimum of column " + i + " = " + minc );

}

return minc;

}

public int getMaximumOfEveryRow ()

{

for ( int i = 0; i < A.length; i++ )

{

maxr = Integer.MIN_VALUE;

for ( int j = 0; j < A [ i ].length; j++ )

if ( A [ i ] [ j ] > maxr )

maxr = A [ i ] [ j ];

System.out.println( "Maximum of row " + i + " = " + maxr );

}

return maxr;

}




Comments

  1. This is the simplest implementation.

    for(int i=0; i<arr.length ; i++){
    int columnNumber = getMaxElementsColumnNumber(arr,i); // i will represent row number
    int minElement = getMinimumElementInColumn(arr,columnNumber);
    }


    getMaxElementsColumnNumber() will print the maximum number in current row and will return the column number of that element

    getMinimumElementInColumn() will traverse column of matrix and return the minimum element in that column.

    You can implement code in the same for loop. In this way you can also check for the saddle point.

    ReplyDelete
  2. Here is the solution:

    public void get_minimum_of_the_column_of_local_maximum ()
    {
    for ( int i = 0; i < A.length; i++ )
    {
    min = Integer.MAX_VALUE;
    max = Integer.MIN_VALUE;
    int index_of_maximum_in_its_row = 0;

    for ( int j = 0; j < A [ i ].length; j++ )
    if ( A [ i ] [ j ] > max )
    {
    max = A [ i ] [ j ];
    index_of_maximum_in_its_row = j;
    }

    for ( int j = 0; j < A [ index_of_maximum_in_its_row ].length; j++ )
    if ( A [ j ] [ index_of_maximum_in_its_row ] < min )
    min = A [ j ] [ index_of_maximum_in_its_row ];

    System.out.print( " Maximum of row [" + i + "] = " + max);
    System.out.println( " Minimum of column [" + index_of_maximum_in_its_row + "] = " + min );
    }
    }


    What does this code snippet do?

    You have min = Integer.MAX_VALUE, max = Integer.MIN_VALUE variables. The reason we assign these values to this numbers is to make finding max/min possible. How? Initially min has the greatest integer value but as far as we find a value less than it we update our minimum so the min's value decreases. Same technique with max variable but of course it goes the other direction by increasing in value after comparisons.

    First inner for loop determines the maximum of row i, then marks the index of maximum in that row by using variable index_of_maximum_in_its_row. It is required to be used later on in the second inner loop.

    Second inner loop determines the minimum of column number index_of_maximum_in_its_row with one iteration over that column. Than the method prints the results.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex