0

I am writing a program using a method that returns the location of the largest element in a two dimensional array.

Example:

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)

My code is working, but I'm getting the output wrong. Instead of numbers I am getting some weird output with letters and numbers. How can I fix it? Thanks!

My code

import java.util.Scanner;
public class homework1a {
public static int[] locateLargest(double[][] a)
{
 int total = 0;
 int maxRow = 0;
 int maxColumn = 0;
 for (int i = 0; i < a.length; i++)
 {
 for (int j = 0; j < a[i].length; j++)
 {
 maxRow = i;
 maxColumn = j;
 }
 }
 int[] largest = new int[2];
 largest[0] = maxRow;
 largest[1] = maxColumn;
 return largest;
}
public static void main(String[] args)
 {
 //Create Scanner
 Scanner input = new Scanner(System.in);
 double b = 0;
 //User input rows and columns
 System.out.println("Enter the number of rows and columns in the array: ");
 int numberOfRows = input.nextInt();
 int numberOfColumns = input.nextInt();
 //User input data in array
 System.out.println("Enter numbers into array: ");
 //Create array
 double[][] a = new double[numberOfRows][numberOfColumns];
 for (int i = 0; i < a.length; i++)
 {
 for (int j = 0; j < a[i].length; j++)
 {
 a[i][j] = input.nextDouble();
 }
 }
 System.out.println("The location of the largest element is at "+ locateLargest(a));
 }
 }
asked Feb 14, 2015 at 15:15
4
  • possible duplicate of How to use the toString method in Java? Commented Feb 14, 2015 at 15:19
  • Even there is a problem in your logic of finding maximum. You haven't checked for maximum value in inner for loop so you will never get actual maximun value. Commented Feb 14, 2015 at 15:24
  • @ Dipen_a I tried adding an if statement but I think it's also wrong. if (a[maxRow][maxColumn] > a.length) Commented Feb 14, 2015 at 16:40
  • @joseph: have posted solution for the same as answer check it Commented Feb 14, 2015 at 17:06

6 Answers 6

1

Your method locateLargest() returns an int-array, which you are implicitly converting to string while printing it.

I think you want to display the row and cell numbers inside the array:

int[] largest = locateLargest(a);
System.out.println(String.format("The location of the largest element is at %d,%d", largest[0], largest[1]));
answered Feb 14, 2015 at 15:20
Sign up to request clarification or add additional context in comments.

Comments

0

locateLargest(a) returns an int[2]. Arrays cannot be converted to strings natively, so the default toString() implementation is invoked on the array. The returned string representation does not contain the array elements.

This question might help you to print a helpful representation of the array. You might also want to print both values independently, not the array as a whole, e.g. like this:

int[] pos = locateLargest(a);
System.out.println("The location of the largest element is at " + pos[0] + ":" + pos[1]);
answered Feb 14, 2015 at 15:22

Comments

0

To print Arrays use Arrays.toString(array) output will be like [x,y]

System.out.println("The location of the largest element is at "+ Arrays.toString(locateLargest(a)));
answered Feb 14, 2015 at 15:22

Comments

0

Your method locateLargest returns an int[] which will not be printed out nicely.

If you want to keep the signature of locateLargest as it is, you could change your code in main like this:

int[] positionOfLargest = locateLargest(a);
System.out.println("The location of the largest element is at " +
 positionOfLargest[0] + "/" + positionOfLargest[1]);

This stores the result in positionOfLargest and then prints out x/y coordinates the way you want them.

answered Feb 14, 2015 at 15:24

Comments

0

Hey for finding largest you can have your method like this.

public static int[] locateLargest(double[][] a)
{
 int maxValue = 0;
 int maxRow = 0;
 int maxColumn = 0;
 for (int i = 0; i < a.length; i++)
 {
 for (int j = 0; j < a[i].length; j++)
 {
 If(a[i][j] > maxValue)
 {
 maxValue = a[i][j] ;
 maxRow = i;
 maxColumn = j;
 }
 }
 }
 int[] largest = new int[2];
 largest[0] = maxRow;
 largest[1] = maxColumn;
 return largest;
}
answered Feb 14, 2015 at 17:05

Comments

0

u should edit your locateLargest as this:

public static int[] locateLargest(double[][] a)
{
 //may be ur array is contain negative 
 //so u can not use zero as MAX it's better to use first array element
 int MAX = a[0][0];
 int maxRow = 0;
 int maxColumn = 0;
 for (int i = 0; i < a.length; i++)
 {
 for (int j = 0; j < a[i].length; j++)
 {
 if(MAx < a[i][j]){
 maxRow = i;
 maxColumn = j;
 }
 }
 }
 int[] largest = new int[2];
 largest[0] = maxRow;
 largest[1] = maxColumn;
 String result="location of largest num =a["+maxRow+"]["+maxColumn+"]";
 return largest;
}
answered Jul 27, 2017 at 13:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.