Respuesta :
Answer:
The method in JAVA is shown below.
static double largest = 0.0;
static int[] idx = new int[2];
public static int r = 20;
public static int c = 20;
public static int[] locateLargest(double[][] a)
{
for(int j=0; j<c; j++)
{
for(int k=0; k<r; k++)
{
if(largest<a[j][k])
{
largest=a[j][k];
idx[0]=k;
idx[1]=j;
}
}
}
return idx;
}
The JAVA program is shown below.
import java.util.Scanner;
import java.lang.*;
class program
{
//static variables declared and initialized as required
static double largest = 0.0;
static int[] idx = new int[2];
public static int r = 20;
public static int c = 20;
public static int[] locateLargest(double[][] a)
{
for(int j=0; j<c; j++)
{
for(int k=0; k<r; k++)
{
if(largest<a[j][k])
{
largest=a[j][k];
idx[0]=k;
idx[1]=j;
}
}
}
return idx;
}
}
public class Main
{
static double[][] arr;
static double input;
public static void main(String[] args){
program ob = new program();
arr = new double[ob.r][ob.c];
Scanner sc = new Scanner(System.in);
for(int j=0; j<ob.c; j++)
{
for(int k=0; k<ob.r; k++)
{
arr[j][k]=0;
}
}
System.out.println("Enter the elements of two dimensional array ");
for(int j=0; j<ob.c; j++)
{
for(int k=0; k<ob.r; k++)
{
input = sc.nextDouble();
if(input>0)
{ arr[j][k] = input;
//System.out.println(arr[j][k]);
}
else
break;
}
break;
}
int[] large_idx = ob.locateLargest(arr);
int row = large_idx[0];
int col = large_idx[1];
double l = arr[col][row];
System.out.println("The largest element in the user entered array is " + l);
}
}
OUTPUT
Enter the elements of two dimensional array
1
2
3
4
5
6
7
8
9
0
The largest element in the user entered array is 9.0
Explanation:
1. The class program contains the locateLargest() method as mentioned in the question.
2. The public class Main contains the main() method.
3. User input for array is taken inside main().
4. This array is passed to the locateLargest() method.
5. This method returns the one dimensional array having row and column indices of the largest element in the array.
6. The indices are used to display the largest element in the main().