Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

** MUST BE USING JAVA Write the following method that returns the location of th

ID: 3665863 • Letter: #

Question

** MUST BE USING JAVA

Write the following method that returns the location of the largest element in a two-dimensional array:

public static int [] locateLargest(double [] [] a)

The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array. Write a test program (a main method) that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class LargestIndex {

   /**
   * @param args
   */
   public static void main(String[] args) {

       double a[][] = { { 1, 2 }, { 10, 4 }, { 5, 6 }, { 7, 8 } };

       int maxRowColIndex[] = locateLargest(a);

       System.out.println("Maximum Number Index is Row " + maxRowColIndex[0]
               + " Column " + maxRowColIndex[1]);
   }

   /**
   * method to find index of largest number
   *
   * @param a
   * @return
   */
   public static int[] locateLargest(double[][] a) {
       int[] rowCol = { 0, 0 };
       double max = a[0][0];

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

           for (int j = 0; j < a[i].length; j++) {
               if (max < a[i][j]) {
                   max = a[i][j];
                   rowCol[0] = i;
                   rowCol[1] = j;

               }

           }

       }
       return rowCol;

   }
}

OUTPUT:

Maximum Number Index is Row 1 Column 0