Skip to main content

Report the location of the largest element in this 2D array...... HELP PLEASE!!!!

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
mjd90us
Offline
Joined: 2011-11-04

<span class="Apple-style-span" style="font-family: arial, helvetica, clean, sans-serif; font-size: 13px; line-height: 16px; background-color: rgb(255, 255, 255); ">My partner and I have pretty much written all the code. We are just stuck in the very last method 'locateLargest'. We cannot figure out how to set the max = location, and bring down where the largest element is located.  Here is the actual question, followed by our code. Please someone help us finish, our homework is due tonight  and we don't want our work to go to waste and get a 0.</span>


<strong>Question: </strong>(Locating the Largest element) Write the following method that returns the location of the largest element in a two-dimensional array.

public static int[] locate Largest(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 the
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 &lt;---enter
Enter the Array: &lt;--- enter
23.5 35 2 10 &lt;--- enter
4.5 3 45 3.5 &lt;--- enter
35 44 5.5 9.6 &lt;--- enter
The location of the largest element is at (1,2)


<strong>Our code: </strong>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package locatelargestelement;

import java.util.Scanner;

public class LocateLargestElement {

/**
* Finds the location in a two-dimensional matrix of the largest element.
* @param a A matrix containing values
* @return A 1 dimensional array with 2 elements. Element 0 is the row
* of the largest element. Element 1 is the column of the largest
* element.
*/
public static int[] locateLargest(double[][] a) {
int[] location = {0, 0};

int max = 0;

// what comes after my for statements???
for (int row = 0 ; row &lt; a.length ; row++ ){
for ( int column = 0 ; column &lt; a[row].length ; column++){
if ( a[row][column] &gt; max) {
a[row][column] = max;
}
}
}

return location;
}


/**
* Gets the number of rows and columns for a new array, and creates
* and returns an array with that number of rows and columns.
* @return An array, the size of which is determined by the user entering
* the number of rows and columns.
*/
public static double[][] createUserDefined2DArray() {
Scanner input = new Scanner(System.in);



<br />System.out.println(&quot;Enter the number of rows and columns of &quot; +<br />&quot;the array: &quot;);<br />int row = input.nextInt();<br />int cols = input.nextInt();<br />double[][] userInput = new double [row][cols];<br /><br />return userInput;<br />}<br /><br /><br />/**<br />* Prompts the user to enter the array values, then gets a number from the<br />* user for every element in a two dimensional array which is passed to<br />* the method. Returns the same array that the method is passed, but with<br />* the newly entered user values.<br />* @param a Array to put user-entered values into<br />* @return an array with the user values entered<br />*/<br />public static double[][] getUserValues(double[][] array) {<br />Scanner input = new Scanner(System.in);<br /><br />System.out.println(&quot;Enter the array: &quot;);<br /><br />for (int row = 0; row &lt; array.length ; row ++) {<br />for ( int column = 0; column &lt; array[row].length ; column++) {<br />array[row][column] = input.nextDouble();<br />}<br />}<br />return array;<br />}<br /><br /><br />public static void main(String[] args) {<br /><br />double[][] userVals = getUserValues( createUserDefined2DArray() );<br /><br />int[] largestLocation = locateLargest(userVals);<br /><br />System.out.printf(&quot;The location of the largest &quot;<br />+ &quot;element is at (%d, %d)\n&quot;, largestLocation[0],<br />largestLocation[1]);<br />}<br />}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mjd90us
Offline
Joined: 2011-11-04

Also, here is a copy of my output:
<strong> </strong><strong>Output: </strong><strong> <br /></strong>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 (0, 0)

BUILD SUCCESSFUL (total time: 1 minute 57 seconds)