Joseph
Joseph

Reputation: 11

Multidimensional Array Java

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

                }
        }

Upvotes: 0

Views: 229

Answers (6)

Hamid Waezi
Hamid Waezi

Reputation: 887

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

Upvotes: 0

Dipen Adroja
Dipen Adroja

Reputation: 425

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

Upvotes: 0

Sky
Sky

Reputation: 709

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.

Upvotes: 0

Bruce
Bruce

Reputation: 8849

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

Upvotes: 0

Tobias
Tobias

Reputation: 7771

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

Upvotes: 0

Chris
Chris

Reputation: 66

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

Upvotes: 1

Related Questions