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

Write the following func.ion that, returns the location of the largest element i

ID: 3776795 • Letter: W

Question

Write the following func.ion that, returns the location of the largest element in a two-dimensional list. The return value is a one-dimensional list that contains two elements. These two elements indicate the row and column indexes of the largest clement in the two-dimensional list. Write a test program that prompts the user to enter a two dimensional list and displays the location of the largest element in the list. Here is a sample run: Enter the number of rows in the list: 3 Enter a row: 23.5 35 2 10 Enter a row: 4.5 3 45 3.5 Enter a row: 35 44 5.5 11.6 The location of the largest element is at (1, 2)

Explanation / Answer

LocationTest.java

import java.util.Scanner;


public class LocationTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the number of rows in the list: ");
       int rows = scan.nextInt();
       double d[][] = new double[rows][4];
       for(int i=0; i<d.length; i++){
           System.out.print("Enter a row: ");
           for(int j=0; j<d[i].length; j++){
               d[i][j] = scan.nextDouble();
           }
       }
       Location l = locateLargest(d);
       System.out.println("The location of the largest element is "+l.maxValue+" at ("+l.row+", "+l.column+")");
      
   }
   public static Location locateLargest(double[][] a){
       double max = a[0][0];
       int row =0 , col=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];
                   row = i;
                   col = j;
               }
           }
       }
       Location l = new Location(row,col,max);
       return l;
   }
}

Output:

Enter the number of rows in the list: 3
Enter a row: 23.5 35 2 10
Enter a row: 4.5 3 45 3.5
Enter a row: 35 44 5.5 11.6
The location of the largest element is 45.0 at (1, 2)