Question

I've looked all over for a good answer, but I was surprised I couldn't find one that accomplished quite what I'm trying to do. I want to create a method that finds a columns sum in a jagged 2D array, regardless of the size, whether it's jagged, etc. Here is my code:

public static int addColumn(int[][] arr, int x) {
    int columnSum = 0;
    int i = 0;
    int j = 0;
    int numRows = arr.length;
    //add column values to find sum

    while (i < numRows) {
        while (j < arr.length) {
            columnSum = columnSum + arr[i][x];
            j++;
            i++;
        }
    }//end while loop
    return columSum;
}

So, for example, consider I have the following array:

int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};

I want to be able to pass x as 2, and find the sum for Column 3 which would be 9. Or pass x as 3 to find Column 4, which would simply be 8. My code is flawed as I have it now, and I've tried about a hundred things to make it work. This is a homework assignment, so I'm looking for help to understand the logic. I of course keep getting an Out of Bounds Exception when I run the method. I think I'm overthinking it at this point since I don't think this should be too complicated. Can anyone help me out?

Was it helpful?

Solution

I think that your second while loop is making your sum too big. You should only have to iterate over the rows.

while (i < numRows) {
    if (x < arr[i].length) {
        columnSum += arr[i][x];
    }
    ++i;
}

OTHER TIPS

In Java 8, you can use IntStream for this purpose:

public static int addColumn(int[][] arr, int x) {
    // negative column index is passed
    if (x < 0) return 0;
    // iteration over the rows of a 2d array
    return Arrays.stream(arr)
            // value in the column, if exists, otherwise 0
            .mapToInt(row -> x < row.length ? row[x] : 0)
            // sum of the values in the column
            .sum();
}
public static void main(String[] args) {
    int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};
    System.out.println(addColumn(arr, 2)); // 9
    System.out.println(addColumn(arr, 3)); // 8
    System.out.println(addColumn(arr, 4)); // 0
    System.out.println(addColumn(arr, -1));// 0
}

I saw the codes above. None of it is the adequate answer since posting here the code which meets the requirement. Code might not look arranged or optimized just because of the lack of time. Thanks.... :)

package LearningJava;

import java.util.Scanner;

public class JaggedSum {
    public static void main(String[] args) {
        int ik = 0;
        int ij = 0;
        Scanner scan = new Scanner(System.in);
        int p = 0;
        int q = 0;
        int[][] arr = new int[2][];
        System.out.println("Enter the value of column in Row 0 ");
        p = scan.nextInt();
        System.out.println("Enter the value of column in Row 1 ");
        q = scan.nextInt();
        arr[0] = new int[p];
        arr[1] = new int[q];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        if (arr[1].length > arr[0].length) {
            int[] columnSum = new int[arr[1].length];
            int x;
            for (x = 0; x < arr[0].length; x++) {
                columnSum[x] = arr[0][x] + arr[1][x];
                System.out.println(columnSum[x]);
            }
            ik = arr[0].length;
            for (int j = ik; j < arr[1].length; j++) {
                columnSum[j] = arr[1][j];
                System.out.println(columnSum[j]);
            }
        } else {
            int[] columnSum = new int[arr[0].length];
            int x;
            for (x = 0; x < arr[1].length; x++) {
                columnSum[x] = arr[0][x] + arr[1][x];
                System.out.println(columnSum[x]);
            }
            ij = arr[1].length;
            for (int j = ij; j < arr[0].length; j++) {
                columnSum[j] = arr[0][j];
                System.out.println(columnSum[j]);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top