2

When the column count and row count are the same, this code works fine. When they differ it throws an exception inside the columnSum method.

Here's the error message.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

And here's my code

public static void columnSum(int[][] anArray)
{
    int sum = 0;

    for (int col = 0; col < anArray.length; col++) 
    {
        for (int r = 0; r < anArray[col].length; r++) 
        {
            sum += anArray[r][col];
        }

        System.out.println("Sum of column " + col + " = " + sum);
     }

     System.out.println();
}

I have no idea why my code doesn't work when I have more columns than rows or vice versa.

Does anyone have idea?

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
spyair
  • 57
  • 4
  • 1
    ``sum += anArray[col][r]`` I think. – sashaaero Apr 25 '15 at 04:24
  • nope nope~ My program doesn't give me the sum of column 3 when I input 2 for rows and 3 for columns – spyair Apr 25 '15 at 04:29
  • my comment is fixing ``ArrayOutOfBoundException`` btw. Because when you input 2 rows and 3 columns, you are calculating sum for 3 rows and 2 columns. Change loops. – sashaaero Apr 25 '15 at 04:32
  • In Java there's compilation and runtime. I think you mean this is a runtime problem, not a compiler problem. – avgvstvs Apr 25 '15 at 04:35
  • possible duplicate of [sum of columns in a 2 dimensional array](http://stackoverflow.com/questions/14302971/sum-of-columns-in-a-2-dimensional-array) – mbsingh Apr 25 '15 at 04:56

2 Answers2

0

Change sum += anArray[r][col]

to sum += anArray[col][r]

This is the reason why it runs when columns & rows are of the same size. When they're of different length, for instance 5 cols & 3 rows, your code will try to access anArray[3][5] in one of the iterations and that elements does not exist, hence the exception.

mbsingh
  • 499
  • 10
  • 26
  • nope nope~ My program doesn't give me the sum of column 3 but up to sum of column 2 when I input 2 for rows and 3 for columns. And the calculation is even wrong. – spyair Apr 25 '15 at 04:36
  • You're trying to get sum of columns? – mbsingh Apr 25 '15 at 04:38
  • Yes Sir. my method code that gets sum of rows works fine but columnSum method is giving me headache... – spyair Apr 25 '15 at 04:43
0

Your loop is in the wrong order. In the external loop you are iterating over the array length and in the internal loop you're iterating over the subarray. Thus, you should access anArray[col][r]; instead of what you're doing. Your code should be like this:

public static void columnSum(int[][] anArray) {
    int sum = 0;
    for (int row = 0; row < anArray.length; row++) {

        for (int col = 0; col < anArray[row].length; col++) {
            sum += anArray[row][col];

        }

        System.out.println("Sum of row " + row + " = " + sum);
    }
    System.out.println();
}

If you really want to sum the column, you should modify your code to:

public static void columnSum(int[][] anArray) {
    int sum;
    // Assuming anArray is not empty
    for (int col = 0; col < anArray[0].length; col++) {
        sum = 0;
        for (int row = 0; row < anArray.length; row++) {   
            sum += anArray[row][col];
        }
         System.out.println("Sum of column " + col + " = " + sum);
    }

    System.out.println();
}
Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
  • Yes I have this for rowSum method and it works perfectly of course. However, I'm trying to get columnSum, not rowSum – spyair Apr 25 '15 at 04:33
  • Thanks!! I'm really close to the answer now. Your code seems beautiful and works without errors but it gives me the wrong calculation from sum of column2. It gives me the correct calculation for sum of column 1 though. For example, when my 2D array is [[1, 2, 3], [4, 5, 6]] (2 rows, 3 columns). The output is Sum of column 1 = 5, Sum of column 2 = 12, Sum of column 3 = 21. How come do I get this result..? It doesn't make sense to me. – spyair Apr 25 '15 at 05:08
  • my bad. you have to set the value of sum to 0 in each iteration. I edited my answer to do so. – Amaury Medeiros Apr 25 '15 at 12:18