Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

totaling array rows and columns

 
Ryan Perlman
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can total my whole array. but now I am trying to do a total for the rows and an total for the columns. I am stuck on how I need to manipulate the code to just print by row and column seperate.

Thanks Ryan.
 
Suni Das
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------------------------------------------------------------
I can total my whole array. but now I am trying to do a total for the rows and an total for the columns. I am stuck on how I need to manipulate the code to just print by row and column seperate.
--------------------------------------------------------------
int rowTotal = 0;
int colTotal =0 ;
for(int row = 0; row < sales.length; row++){
for(int col = 0; col < sales[row].length; col++){
rowTotal += sales[row][col];
}
System.out.println("Row Total for the row:"+(row+1)+
" = "+rowTotal);
rowTotal = 0;
}
for(int col = 0; col < sales[0].length; col++){
for(int row = 0; row < sales.length; row++){
colTotal += sales[row][col];
}
System.out.println("Col Total for the col:"+(col+1)+
" = "+colTotal);
colTotal = 0;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic