1

I am trying to print out the largest number in a 2D array. My problem is that my output are three numbers instead of one - the largest. Why?

Here is my code:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    int maxRows = 3;
    int maxCols = 4;

    int [] onedArray = new int [maxRows];
        for (int i = 0; i < maxRows; i++){
        onedArray[i] = (int) ((Math.random() * 100) * maxCols);
    }

    int [][] twodArray = new int[maxRows][];
        for (int i = 0; i < maxRows; i++){
        twodArray[i] = new int[maxCols];
    }

        for (int i = 0; i < twodArray.length; i++){
        for (int j = 0; j < twodArray[i].length; j++){
            twodArray[i][j] = (int) (Math.random() * 100);
        }
    }

    System.out.println("2 - The 2D array: ");
    for (int i = 0; i < twodArray.length; i++){
        for (int j = 0; j < twodArray[i].length; j++){
            System.out.print(twodArray[i][j] + " ");
        }
        System.out.println("");
    }
    int maxValue = 1;
    System.out.println("\nMax values in 2D array: ");
    for (int i = 0; i < twodArray.length; i++) {
        for (int j = 0; j < twodArray.length; j++)
        if (twodArray[i][j] > maxValue) {
        maxValue = twodArray[i][j];
        }
            System.out.println(maxValue);
        }



}

}

Morten
  • 47
  • 1
  • 6
  • 12

5 Answers5

5

Everything up until the last sequence of instructions is correct (although poorly formatted).

Here is original:

int maxValue = 1;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
    for (int j = 0; j < twodArray.length; j++)
    if (twodArray[i][j] > maxValue) {
    maxValue = twodArray[i][j];
    }
        System.out.println(maxValue);
    }

Here is better version:

int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
    for (int j = 0; j < twodArray[i].length; j++) {
        if (twodArray[i][j] > maxValue) {
           maxValue = twodArray[i][j];
        }
    }
    System.out.println("Max value of row " + i + ": " + maxValue);
}

Look carefully and you'll see that I added the { character after the second for-loop.

If you wanted to find total max, and minimize open and close curly-braces here is another version:

int maxValue = 0;

System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++)
    for (int j = 0; j < twodArray[i].length; j++)
        if (twodArray[i][j] > maxValue)
           maxValue = twodArray[i][j];

System.out.println("Maximum value: " + maxValue);

Good luck.

Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
2
    int m,n,max;
    int a[][]=new int[10][10];
    Scanner S=new Scanner(System.in);
    System.out.println("Enter m*n matrix");
    m=S.nextInt();
    n=S.nextInt();
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i][j]=S.nextInt();
        }
    }
    max=a[0][0];
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]>max)
            {
                max=a[i][j];
            }
        }
    }
    System.out.println(max);
Padma
  • 17
  • 2
1

Your line System.out.println(maxValue); needs to come out of the loop over the variable i. It's being printed 3 times because it's inside this loop.

This would be easier to see if your code was indented properly; this is a good habit to get into anyway.

Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
1

The answer is in your code once it's indented correctly:

for (int i = 0; i < twodArray.length; i++) {
    for (int j = 0; j < twodArray.length; j++)
        if (twodArray[i][j] > maxValue) {
            maxValue = twodArray[i][j];
        }
        System.out.println(maxValue);
    }
}

Don't underestimate how useful good indentation can be for catching this kind of bug :)

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0
    int max;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of rows : ");
    int n = sc.nextInt();
    System.out.println("Enter number of columns : ");
    int m = sc.nextInt();
    int[][] array = new int[n][m];
    System.out.println("Enter the elements of array : ");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            System.out.print("X[" + i + "," + j + "]" + "=");
            array[i][j] = sc.nextInt();
        }
    }
    max = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (array[i][j] > max) {
                max = array[i][j];
            }
        }
    }
    System.out.println("Max value of the array is " + max);
}
  • Hi, welcome to Stack Overflow. When answering a question that already has many answers, please be sure to add some additional insight into why the response you're providing is somehow substantive and not simply echoing what's already been vetted by the original poster. Anybody can paste code into a text box: why is this code special? – chb Jan 24 '19 at 00:16