0

I have been recently working with 2D and Jagged Arrays and am trying to add each element in a row and each element inside a column. I got the code to print out the sum of each row, but am having difficulty with adding each column in the array.

Note: I tried doing sum += numbers[c][r]; but it is not working

    import java.util.Arrays;
    import java.io.*;

    public class Homework{
       public static void main(String[] args) {
          int[][] numbers = {  {3, 2, 5,},
                               {1, 4, 4, 8, 13},
                               {9, 1, 0, 2},
                               {0, 2, 6, 3, -1, -8} };

//Adding each row in the array          
int sum = 0;
          for(int r = 0; r < numbers.length; r++) {
             sum = 0;
             for(int c = 0; c < numbers[r].length; c++) {
                sum += numbers[r][c];
             }
             System.out.println("Sum of row: " + sum);
          }

//Adding each column in the row          
int sum2 = 0;
          for(int r = 0; r < numbers.length; r++) {
             sum2 = 0;
             for(int c = 0; c < numbers[r].length; c++) {
                sum2 += numbers[c][r];
             }
             System.out.println("Sum of column: " + sum2);
          }
       }
    }

1 Answers1

0

Okay so you were sort of on the right track. Let me explain a way of thinking before I get into the code.

You have a non-square 2d Array and you want the total of each row and column. Row is simple, you've accomplished that already. Column is a little more difficult because since its non-square, you will run into ArrayOutOfBoundsExceptions because it tries to access a art of the array that doesn't exist.

To combat this, you need to know the max number of columns there are and then iterate through the array. BUT, you also need to be careful about that ArrayOutOfBoundsException, making sure to account for when the current column iteration is greater than the row length.

I added an int max to your row sum section, this way I don't have to iterate over the same info more than once.

After that, I replace c < numbers[r].length with 'max', since we need to iterate over X columns.

Then I iterate over the rows, since they are uniform and easily found by numbers.length.

So, a simple change to your code yields the results you need. See below.

        int[][] numbers = {
                {3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}
        };

        int max = 0;


//Adding each row in the array
        int sum;
        for(int r = 0; r < numbers.length; r++) {
            sum = 0;
            for(int c = 0; c < numbers[r].length; c++) {
                sum += numbers[r][c];
                if(numbers[r].length > max){
                    max = numbers[r].length;
                }
            }
            System.out.println("Sum of row: " + sum);
        }
        System.out.println("Max " + max);

//Adding each column in the row
        int sum2;
        for(int c = 0; c < max; c++) {
            sum2 = 0;
            for(int y = 0; y < numbers.length; y++){
                if(numbers[y].length > c){
                    sum2 += numbers[y][c];
                    System.out.println(numbers[y][c]);
                }
            }
            System.out.println("Sum of column: " + sum2);
        }
    }
Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32
  • Thanks @Hypnic Jerk for helping me out! I finally understood what I had to do! –  Jan 09 '18 at 00:52