How to find the location of the maximum and minimum values ​​of a 2d array

I don’t know if I am clear with this, but I already have the minimum and maximum printout, but I can’t figure out how to tell the exact rows and columns in which they are located. this is what I still have;

double max = m[0][0];
       double min =  m[0][0];
    System.out.println("The matrix is : ");

        for(int i = 0; i < m.length; i++)
        {
            for ( int j = 0; j < m[i].length; j++ )
            {
                System.out.printf("   " + "%6.1f " , m[i][j]);
                if (m[i][j] > max)
                    max = m [i][j];     

                else if
                (m[i][j] < min)
                    min = m [i][j];

How can I make an expression about my places? for example: ("Maximum number in row 1, column 2"), something like this ... I would really appreciate any help.

+4
source share
2 answers

. . min max. maxIndex1, maxIndex2, minIndex1 minIndex2.

double max = m[0][0];
double min =  m[0][0];

//declare variables to track the indices of the min and max
int maxIndex1 = -1;
int maxIndex2 = -1;
int minIndex1 = -1;
int minIndex2 = -1;

System.out.println("The matrix is : ");
for(int i = 0; i < m.length; i++)
{
    for ( int j = 0; j < m[i].length; j++ )
    {
        System.out.printf("   " + "%6.1f " , m[i][j]);
        if (m[i][j] > max)
        {
            max = m [i][j]; 
            //record the indices of the new max
            maxIndex1 = i;
            maxIndex2 = j;  
        }  
        else if (m[i][j] < min)
        {
            min = m [i][j];
            //record the indices of the new min
            minIndex1 = i;
            minIndex2 = j;
        }

, , , . min/max, , .

+3

! 2 x y. , ( if else!), !

+1

Source: https://habr.com/ru/post/1656738/


All Articles