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));

                }
        }
  • possible duplicate of [How to use the toString method in Java?](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Jens Feb 14 '15 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. – Dipen Adroja Feb 14 '15 at 15:24
  • @ Dipen_a I tried adding an if statement but I think it's also wrong. if (a[maxRow][maxColumn] > a.length) – Joseph Feb 14 '15 at 16:40
  • @joseph: have posted solution for the same as answer check it – Dipen Adroja Feb 14 '15 at 17:06

6 Answers6

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]));
Chris
  • 66
  • 2
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]);
Community
  • 1
  • 1
Tobias
  • 7,723
  • 1
  • 27
  • 44
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)));
Bruce
  • 8,609
  • 8
  • 54
  • 83
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.

Sky
  • 674
  • 8
  • 22
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;
}
Dipen Adroja
  • 415
  • 5
  • 15
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;
}
Hamid Waezi
  • 878
  • 1
  • 7
  • 14