Martin
Martin

Reputation: 1

How to get the sum of each column displayed

//How can i get the sum of the columns of the output of this array.

public class Arrays {
private static int column;

public static void main(String[] args) {
    int [][]array1={{1,2,3,4},{5,6,7},{8,9},{10}};
   System.out.println("Values in array1 by row are");
   OutputArray(array1);
}

public static void OutputArray(int[][] array1) {
    int sum=0;

    for(int row=0;row<array1.length;row++){
            for( column=0;column<array1[row].length;column++)
                System.out.printf("\t"+"%d\t", array1 [row][column]);
               System.out.println();
    }
     System.out.println();
     sum=sum+array1.length;
     System.out.print("sum=\t"+sum+"\t");
     System.out.println();
}

Upvotes: 0

Views: 986

Answers (1)

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

Instead of having row iterator in outer loop, have the column iterator in outer loop.

Cliche: The implementation is left as an exercise :)

Upvotes: 3

Related Questions