Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find minimum and maximum value for two dimensional array

Posted on 2010-11-10
10
Medium Priority
?
4,036 Views
Last Modified: 2012-05-10
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];
            }
      }
0
Comment
Question by:dhatlas
10 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 34106779
>>    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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34107200
[3][0] would essentially  give you an empty matrix
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34107258
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34107307
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]
0
 

Author Comment

by:dhatlas
ID: 34107607
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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 34107670
You can do something like
    public static int[] getMinMax(int[] a) {
	int max = Integer.MIN_VALUE;
	int min = Integer.MAX_VALUE;
	for(int i = 0;i < a.length;i++) {	
	    min = Math.min(min, a[i]);
	    max = Math.max(max, a[i]);
	}
	return new int[] { min, max };
    }

Open in new window

0
 

Author Comment

by:dhatlas
ID: 34107768
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?)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34107778
System.out.println("Lowest total sales for the month are  " + getMinMax(array)[0]);
0
 

Author Comment

by:dhatlas
ID: 34107878
Great.  Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34110231
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question