Java Programming Write your own source code with comments Lo

Java Programming. Write your own source code with comments.

(Locate the largest element) 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 that prompts the user to enter a two- dimensional array and displays the location of the largest element in the array.
Here is a sample run:

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

Console sterminated ProgrammingEx8 13 Java cation] Flec ava\\jdk1.8.0 45Mbi Enter the number of rows and columns of the array:3 Enter the array: 23.5 35 2 10 4.5 345 3.5 35 44 5.5 9.6 The location of the largest element is at (1, 2)

Solution

//java program with main and method to find largestindex in array

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be \"Main\" only if the class is public. */
class Codechef
{

//method to find largest indext of an element in the array
public static int[] locateLargest(double[][] a,int r, int c)
{
double max = a[0][0];
int []largest = new int[2];
int k = 0;
//find max number
for(int i = 0; i < r ; i++)
       {
       for(int j = 0; j < c ; j++)
       {
       if( max < a[i][j])
       {
       max = a[i][j];
      
       }
       }
       }
      
       //now find index of max number
       for(int i = 0; i < r ; i++)
       {
       for(int j = 0; j < c ; j++)
       {
       if(max == a[i][j])
       {
       largest[k] = i;
       largest[k+1] =j ;
       }
       }
       }
      
       return largest;
      
}
public static void main (String[] args) throws java.lang.Exception
   {
       Scanner in = new Scanner(System.in);
       //declare int variables to hold rows and cols
       int rows,cols;
       //take user input for rows and cols
       System.out.printf(\" Enter the number of rows and columns of the array: \");
       rows = in.nextInt();
       cols = in.nextInt();
       // declare float array
       double[][]array =new double[rows][cols];
       //take user input for array elements
       System.out.printf(\"\ Enter the array: \");
       for(int i = 0; i < rows ; i++)
       {
       for(int j = 0; j < cols ; j++)
       {
       array[i][j] = in.nextFloat();
       }
       }
       //call the function locateLargest function
       int []largest = locateLargest(array,rows,cols);
      
       //print indext of largest element in array
       System.out.printf(\"\ The location of the largest element is at (%d %d)\ \",largest[0],largest[1]);
   }
}

-------------------------------------------------------------------------------------------------

//output same as mentioned

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

Java Programming. Write your own source code with comments. (Locate the largest element) Write the following method that returns the location of the largest ele
Java Programming. Write your own source code with comments. (Locate the largest element) Write the following method that returns the location of the largest ele

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site