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

Exercise 3: Design and implement a Java program for programming exercise 8.13, p

ID: 3681904 • Letter: E

Question

Exercise 3: Design and implement a Java program for programming exercise 8.13, page 310 (name it LocateLargestElement) as described in the problem statement. Write method locateLargest) as specified Notice that this method returns 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. To test this method, the main method of your program prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Use the sample run to format your output. Design the main method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable

Explanation / Answer

import java.util.Scanner;

public class LocateLargestElement {

   public static int[] locateLargest(double[][] a){
      
       if(a==null) // if matrix is null
           return null;
      
       int location[] = new int[2];
      
       int row = a.length; // number of rows
       int col = a[0].length; // number of columns
      
       double largest = a[0][0]; // initializing max with first element
       location[0] = 0; // row index
       location[1] = 0; // column index
      
       // iterating through matrix and finding max
       for(int i=0; i<row; i++)
           for(int j=0; j<col; j++){
               if(a[i][j] > largest){ // if current element is larger than largest value
                   largest = a[i][j];
                   location[0] = i;
                   location[1] = j;
               }
           }
      
       return location;
   }
  
public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the number of rows and columns of array: ");
       String rowCol = sc.nextLine();
       String dimension[] = rowCol.split("\s+"); // splitting by space
      
       int row = Integer.parseInt(dimension[0].trim());
       int col = Integer.parseInt(dimension[1].trim());
      
       // creating array
       double arr[][] = new double[row][col];
      
       // getting inputs for array row by row
       String rowInputs;
      
       System.out.println("Enter a "+row+"x"+col+" matrix row by row: ");
      
       for(int i=0; i<row; i++){
           rowInputs = sc.nextLine(); // taking a line as string
           String entry[] = rowInputs.split("\s+"); // splitting input string by space
           for(int j=0; j<entry.length; j++){
               // converting each string entry into double and storing in array
               arr[i][j] = Double.parseDouble(entry[j].trim());
           }
          
       }
      
       // calling locateLargest
       int location[] = locateLargest(arr);
       System.out.println("The location of largest element is at ("+location[0]+", "+location[1]+")");
   }
}


/*

Sample run:

Enter the number of rows and columns of array: 3 4
Enter a 3x4 matrix row by row:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of largest element is at (1, 2)

*/