![tdahna0403](/avatars/27266.jpg)
tdahna0403
04.04.2020 •
Computers and Technology
G (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 (the main method) that prompts the user to enter a two-dimensional array and displays the location of the largest element of the array.
Solved
Show answers
More tips
- H Health and Medicine What are Electronic Cigarettes? The Ultimate Guide...
- C Computers and Internet How to Get Rid of 3pic Infector: Everything You Need to Know...
- A Auto and Moto Experience the World of the Most Expensive Cars on the Planet...
- S Style and Beauty How to Get Rid of a Double Chin?...
- F Food and Cooking How to Cook Julienne? Recipes and Tips...
- D Dating, Love, Relationships 10 Useful Tips on How to Survive a Breakup?...
- F Food and Cooking Apple Cider Vinegar: The Ultimate Health and Beauty Solution...
- C Computers and Internet Е-head: How it Simplifies Life for Users?...
- F Family and Home How to Choose the Best Diapers for Your Baby?...
- F Family and Home Parquet or laminate, which is better?...
Answers on questions: Computers and Technology
- M Mathematics Please can you help a gal out paha...
- M Mathematics When x = 2 and y = 10 Write a formula for in terms of ....
- M Mathematics 3 of all the trees planted by a landscaping firm, 90 % survive, what is the probability that it or more of the 16 trees just planted will sorvire? une table) of binomial prob....
- M Mathematics 6. Id scores in a certain population are normally distributed with a mean of 101 and a standard deviation of is. (4 decimal places)...
- M Mathematics Lol please help me i really don’t get this...
- C Chemistry N ka Parmanu dravyaman kya hain ka Parmanu dravyaman kya hai science question ...
- M Mathematics Answer for 40 points!!!...
- M Mathematics debe tomar 4 fotos de una carrera. Cada vez que un corredor pasa a su lado, hay un 50% de probabilidad de que tome una foto de ese corredor. Además, para dos corredores cualesquiera,...
- B Business Consider the following information. First Quarter Second Quarter Unit Selling Price $15.00 $17.00 Total Units Sold 12,000 11,500 Labor Hours 10,000 9,500 Labor Cost/Hour $9.00...
- B Biology Consider two pure-breeding lines of grapes, P1 and P2. The average diameter of P1 grapes is 1.2 cm and the variance is 0.30 cm2. The average diameter of P2 grapes is 1.6 cm...
Ответ:
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().
Ответ: