140k views
5 votes
I 2D-Array Basics 1. Write amethod int locateLargest(int[][] A) that returns the location (row and column) of the largest element in the array A. Write amain method to test your method.

1 Answer

3 votes

Answer:

Step-by-step explanation:

Here is the main method to return the largest element in the array:-

public class ArrayUtils {

public static int[] locateLargest(int[][] A) {

int max = A[0][0];

int maxRow = 0;

int maxCol = 0;

for (int i = 0; i < A.length; i++) {

for (int j = 0; j < A[i].length; j++) {

if (A[i][j] > max) {

max = A[i][j];

maxRow = i;

maxCol = j;

}

}

}

int[] location = {maxRow, maxCol};

return location;

}

public static void main(String[] args) {

int[][] array = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

int[] largestLocation = locateLargest(array);

int maxRow = largestLocation[0];

int maxCol = largestLocation[1];

System.out.println("The largest element is located at row " + maxRow + ", column " + maxCol);

}

}

User Jayprakash Singh
by
8.3k points