java - Finding the sum of columns in a Two Dimensional Array with rows of different lengths -


so i'm having bit of trouble code in returning sum of columns in 2 dimensional array rows of different lengths. example 2x2 array works calculate sum of columns. if have example 2x3 array gives me out of bounds error. want work numbers given user rows , columns not fixed number said example. can me resolve issue? thanks!

here code:

public static void columnsum(int[][]anarray) {     int totalcol = 0;     for(int col =0; col < anarray.length; col++){         totalcol = 0;         for(int row = 0; row < anarray[col].length; row++) {             totalcol += anarray[row][col];         }     system.out.println("sum of column " + col + " = " + totalcol);     }   } 

your indices flipped. for loops written column-major ordering, totalizer written row-major order. need reverse 1 or other.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -