1

So I'm trying to come up with this method that will display the index of the maximum number in the 2D array. I was able to do it for a single D array, but I'm having trouble doing it for the 2D.

public static int findMaximumValue(int[ ][ ] a)
{
   int maxVal = a [0][0];
   int i = 0;

   for(i = 0; i < a.length; i++)
   {
      for(int j = 0; j < a[0].length; j++)
      {
         if(a[i][j] > maxVal)
         {
            maxVal = a[i][j];
         }
      }
   }
   return(maxVal);
 }   

2D ARRAY

public static int [][] findMaximumIndex(int[ ][ ] a)
{
   int maxVal = a[0][0];
   int [][] maxIndex = new int [1][2];
   int row = 0, col = 0; 

   for(row = 0; row < a.length; row++)
   {
      for(col = 0; col < a[row].length; col++)
      {
         if(a[row][col] > maxVal)
         {
           maxVal = a[row][col];
           maxIndex [1] [2] = a[row][col];
         }
      }
   }
   return(maxIndex); 
}
2
  • in 2d array you return one element but your return type is integer 2d array Commented Jul 22, 2015 at 4:15
  • maxVal = a[row][col]; maxIndex [1] [2] = a[row][col]; you doing same thing in two steps you assign maxvalue Commented Jul 22, 2015 at 4:25

1 Answer 1

1

First, you are returning a 2d int array when you want to return the index of the maxValue which will be a row index and a column index. Perhaps you want to return an array of size 2 to represent this? more info would be helpful. Second, I'm not sure what your doing with the maxIndex 2d array. Each time you iterate through the 2d array, just comepare the maxvalue to the current element. havent got an IDE near to debug but should go something like this:

  public static int[] findMaximumIndex(int[ ][ ] a)
{
    int maxVal = -99999
    int[] answerArray = new int[2];
    for(int row = 0; row < a.length; row++)
    {
        for(int col = 0; col < a[row].length; col++)
        {
            if(a[row][col] > maxVal)
            {
                maxVal = a[row][col];
                answerArray[0] = row;
                answerArray[1] = col;
            }
        }
    }
    return answerArray;
}

here answerArray[0] will have the row index for the max element and answerArray[1] will have the column index for the max element

Sign up to request clarification or add additional context in comments.

6 Comments

That will give me the maximum value, but I'am trying to output the index of the maximum value in the 2D array.
sorry for the misunderstanding, there will be two indexes since you have rows and columns. so do you want to print out the row and col? or you can throw them in an array of size 2 you can return but that would be very strange
Thats what I'am trying to do, I want to return it rather than print out. I'm just not sure how to do it exactly.
answer updated again. Is this what youre looking for?
I don't think maxVal would be necessary.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.