*
The moose likes Beginning Java and the fly likes totaling array rows and columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "totaling array rows and columns" Watch "totaling array rows and columns" New topic
Author

totaling array rows and columns

Ryan Perlman
Greenhorn

Joined: Oct 31, 2001
Posts: 23
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

Joined: Nov 27, 2001
Posts: 2
------------------------------------------------------------
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;
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: totaling array rows and columns
 
Similar Threads
jtable
Write a method that can be called, that will initialize the seating plan.
Comparing the sums of rows and columns in a 2D array
Problem with indexing of an array
Arrays with files