0

I want to find the max element of row's by checking row by row and the same for columns!

Example 2D Array:

3 7 2 5 1 4 6 9 8

By checking every row element by element it should go like this: on the first row 7 is max, then on the second row there's no element greater than 7 so max is still 7, then on the third row 9 is greater than 7 so max element of rows is 9!

Then the same for columns: 6 is max on the first column, then on the second column 9 is max and on the third column there's no element greater then 9, therefore max of columns is 9!

By theory the max element of rows is also the max element of columns!

So, I need to make a Java program that does that so the result of max rows and max columns will be equal, which means that my program runs correctly!

On my code I have stored my 2D Array on bigMatrix[][], but I don't understand how is it possible to take the whole row with bigMatrix[i], it works but I don't understand how, and I don't know how to do the same to take every column and pass them as array to my function getMax() to find the max of that column!

**The below code works fine for finding the max element of Rows, but I don't know how to find the max element of Columns!

int rowMax = Integer.MIN_VALUE;

for(int i = 0; i < 3; i++) {
    if(rowMax < getMax(bigMatrix[i])) {
        rowMax = getMax(bigMatrix[i]);
    }
}

public static int getMax(int[] ourArray) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < ourArray.length; i++) {
        max = Math.max(max, ourArray[i]);
    }
    return max;
}
3
  • Is it a n*n matrix? i.e, row count is equal to column count? Commented Mar 15, 2018 at 17:30
  • So TL;DR, you need a way to get an array of each column, right? Commented Mar 15, 2018 at 17:32
  • Possible duplicate of get columns from two dimensional array in java Commented Mar 15, 2018 at 17:34

3 Answers 3

0

Simply change the type of argument of the getMax(int[] ourArray)function to getMax(int[][] ourArray) and then just pass your 2D array as an argument when calling the function.

In the body you need to go over each item of the array so you need double for cycle

for(int i = 0; i < ourArray.length;i++){
 for(int j = 0; j < ourArray[i].length;j++){
  max = Math.max(max, ourArray[i][j];
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

for this create a loop which parses each column into the findMaxMethod like this one :

public int method(int column)
{
 int result = Integer.MIN_VALUE;
 for(int x = 0; x<array[column].length; x++)
   {
    result = Math.max(result, array[column][x]);
   }
 return result;
}

hope this helps....

Comments

0

Here's a solution to your problem, assuming that each row in the array has the same number of columns (note that the following cases need to be considered: (1) when the array is null, and (2) when the array is not null and there are no items in it; both of these situations are appropriately handled below):

public static void findAndPrintMaximumValues(int[][] arrayToTraverse) {
        if(arrayToTraverse == null || arrayToTraverse.length == 0 || arrayToTraverse[0].length == 0) {  
            throw new IllegalArgumentException("Either the array is null or it doesn't contain any elements.");
        }

        int maximumValueGoingRowByRow = Integer.MIN_VALUE;
        int maximumValueGoingColumnByColumn = Integer.MIN_VALUE;

        // Traverse the array in row-major order.
        for(int currentRowIndex = 0; currentRowIndex < arrayToTraverse.length; currentRowIndex++) {
            for(int currentColumnIndex = 0; currentColumnIndex < arrayToTraverse[0].length; currentColumnIndex++) {
                maximumValueGoingRowByRow = Math.max(maximumValueGoingRowByRow, arrayToTraverse[currentRowIndex][currentColumnIndex]);
            }
        }

        // Traverse the array in column-major order.
        for(int currentColumnIndex = 0; currentColumnIndex < arrayToTraverse[0].length; currentColumnIndex++) {
            for(int currentRowIndex = 0; currentRowIndex < arrayToTraverse.length; currentRowIndex++) {
                maximumValueGoingColumnByColumn = Math.max(maximumValueGoingColumnByColumn, arrayToTraverse[currentRowIndex][currentColumnIndex]);
            }
        }

        System.out.format("The maximum value with traversal in row-major order is %d.%n", maximumValueGoingRowByRow);
        System.out.format("The maximum value with traversal in column-major order is %d.", maximumValueGoingColumnByColumn);
    }

Comments

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.