sum of columns in a 2 dimensional array

47,715

Solution 1

Your outer for loop condition is giving you problems. Here's your loop: -

for (int i = 0; i < array[i].length; i++)

Now, when i reaches the value 3, you are trying to access array[3].length. This will throw you IndexOutOfBounds exception.


Since the size of every internal arrays are same, you can change your loop to: -

for (int i = 0; i < array[0].length; i++)

Or, even better, just store the array[0].length in some variable before hand. But that will not make much of a difference.


I would also suggest you to use a better way to calculate the sum of columns. Avoid iterating over rows first. Keep the iteration a normal one probably like this: -

public double[] columnSum(double [][] array){

    int size = array[0].length; // Replace it with the size of maximum length inner array
    double temp[] = new double[size];

    for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++){
            temp[j] += array[i][j];  // Note that, I am adding to `temp[j]`.
        }
    }

    System.out.println(Arrays.toString(temp));
    return temp;  // Note you are not using this return value in the calling method
}

So, you can see that how your problem is highly simplified. What I did is, rather than assigning the value to the array, I added the new value of array[i][j] to the existing value of temp[j]. So, gradually, the value of array[i][j] for all i's (rows) gets summed up in temp[j]. This way you don't have to use confusing iteration. So, just add the above code to your method, and remove the old one.

This method will also work fine, even if you have jagged-array, i.e., you inner arrays are not of same size. But just remember to define the size of temp array carefully.

Also note that, I have used Arrays.toString(temp) method to print the array.

Solution 2

Problem with your code is when it tries to fetch arr[3].length as there does not exist simple solution like sum = sum+arr[i][j] where i refers to row and j refers to column.

int row = arr.length;
int col = arr[0].length;
for(int j = 0; j < cols; j++)
{
            int sum = 0;
            for(int i = 0; i < rows; i++)
            {
                sum = sum + input[i][j];
            }

        }
Share:
47,715
user1706295
Author by

user1706295

Updated on July 19, 2022

Comments

  • user1706295
    user1706295 almost 2 years
    static double [][] initialArray = {{7.432, 8.541, 23.398, 3.981}, {721.859, 6.9211, 29.7505, 53.6483}, {87.901, 455.72, 91.567, 57.988}};
    
    public double[] columnSum(double [][] array){
        int index = 0;
        double temp[] = new double[array[index].length];
    
        for (int i = 0; i < array[i].length; i++){
            double sum = 0;
    
            for (int j = 0; j < array.length; j++){
                sum += array[j][i];
    
            }
            temp[index] = sum;
            System.out.println("Index is: " + index + " Sum is: "+sum);
            index++;
    
        }
    
        return temp;
    }
    
    
    public static void main(String[] args) {
        arrayq test = new arrayq();
        test.columnSum(initialArray);
    
    }
    

    I want to get the sum of all the columns, but I keep getting an outofbounds exception. This is the output I get:

    Index is: 0 Sum is: 817.192
    Index is: 1 Sum is: 471.18210000000005
    Index is: 2 Sum is: 144.7155
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at NewExam.arrayq.columnSum(arrayq.java:11)