Link to home
Start Free TrialLog in
Avatar of dhatlas
dhatlasFlag for United States of America

asked on

Find minimum and maximum value for two dimensional array

Hi, I have been beating my head against the wall trying to figure this out.  I am trying to figure out the minimum and maximum value for a single row.  I have added the values from three rows of a two dimensional array into a fourth row and I want to get the minimum and maximum value from the row but I can't seem to figure it out.  The code below gives me weird results.  I set my min and max at [3][0] because I only want to scan row 3.  Thanks for the help.

int min = sales[3][0];

      for (int row = 3; row < sales.length; row++)
            {
                  for (int column = 0; column < grades.length; column++)
                  {
                        if (sales[row][column] < min)
                        min = sales[row][column];
                  }
            }

      //Maximum
      int max = grades[3][0];

      for (int row = 0; row < sales.length; row++)
      {
            for (int column = 0; column < sales.length; column++)
            {
                  if (sales[row][column] > max)
                  max = sales[row][column];
            }
      }
Avatar of phoffric
phoffric

>>    for (int row = 3; row < sales.length; row++)>>            {>>                  for (int column = 0; column < grades.length; column++)Why are you mixing sales and grades matrices in this double for loop?
[3][0] would essentially  give you an empty matrix
Addressing the matrix is done theoretically as

m[row][col]

so row iteration is done using the first dimension. To give you at least one element (column) in the row, see that col >= 1 when the matrix is created
Based on the question title, I would assume you need the min and max values for the individual elements in the whole array.  If so, then why two seperate loops for min and max, and why the extra dimension?

int min = sales[0][0];
int max = sales[0][0];
for( row = 0; row < ROW_COUNT; row++ )
  for( col = 0; col < COLUMN_COUNT; col++)
    if( sales[row][col] < min )
      min = sales[row][col]
    if( sales[row][col] > max )
      max = sales[row][col]
Avatar of dhatlas

ASKER

I would have an array like this for example:

0 {1,2,3,4,5,6,7,8,9,10}
1 {1,2,3,4,5,6,7,8,9,10}
2 {1,2,3,4,5,6,7,8,9,10}
3 {3,6,9,12,15,18,21,24,27,30) Totals row

I want to get the minimum and the maximum value from the total sales in row 3.  I don't care about searching the values in rows 0 - 2.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhatlas

ASKER

Interesting, so how would I call that method?  I mean to display the results:
For example:

System.out.println("Lowest total sales for the month are  " + method call?)
System.out.println("Lowest total sales for the month are  " + getMinMax(array)[0]);
Avatar of dhatlas

ASKER

Great.  Thanks.
:)