Question: In this lab, we are going to build a two-dimensional array in which we will store double values. We will prompt the user to enter

In this lab, we are going to build a two-dimensional array in which we will store double values. We will prompt the user to enter the size of the array, then prompt the user to enter the values that will be stored in each element of the array. We will then create a method, locateLargest, to search find the element that has the largest value in the array. This method will return a single-dimensional array that indicates the x,y coordinates of the element with the largest value.

my code runs and displays the array fine, but it just assigns largest values to the last row and column.

public class FindElement {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

//declare variables

Scanner input = new Scanner (System.in);

int numberOfRows;

int numberOfColumns;

double [] [] arrayElements;

int[] largestIndex;

System.out.print("Enter Desired Rows");

numberOfRows = input.nextInt();

System.out.print("Enter Desired Columns");

numberOfColumns = input.nextInt();

arrayElements = new double[numberOfRows][numberOfColumns];

for ( int row=0; row

{

for ( int column=0; column

{

System.out.println("Row [" + row + "]: Column " + column + " :");

arrayElements[row][column] = input.nextDouble();

}

}

System.out.print("The Array is : ");

for ( int row=0; row

{

for ( int column=0; column

{

System.out.print(arrayElements[row][column] + " ");

}

System.out.println();

}

largestIndex = locateLargest(arrayElements);

System.out.printf( "The largest number is located at { %d, %d } ", largestIndex[0],

largestIndex[1] );

}

public static int[] locateLargest ( double arrayx2[][] ){

int row= 0; int column = 0;

int[] indexArray=new int[2];

indexArray[0] = arrayx2[0].length;

indexArray[1] =arrayx2[1].length ;

double max = 0;

for(int i=0; i

{

for(int j=0; j

{

if(arrayx2[i][j] > max )

{

max = arrayx2[i][j];

indexArray[0] =i;

indexArray[1] = j;

}

}

}

return indexArray;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!