xxxxxxxxxx
import java.io.* ;
class ArrayMaxMin
{
public static void main ( String[] args )
{
int[][] data = { {3, 2, 5, 0, 0, 0},
{1, 4, 4, 8, 13, 0},
{9, 1, 0, 2, 0, 0},
{0, 2, 6, 3, -1, -8} };
int sumCol;
//Calculates sum of each column of given matrix
int rows = data.length;
int cols = data[0].length;
for(int i = 0; i < cols; i++){
sumCol = 0;
for(int j = 0; j < rows; j++){
sumCol = sumCol + data[j][i];
}
System.out.println("Sum of " + (i+1) +" column: " + sumCol);
}
}
}
xxxxxxxxxx
import java.io.* ;
class RowSums
{
public static void main ( String[] args )
{
int[][] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
// compute the sums for each row
for ( int row=0; row < data.length; row++)
{
// initialize the sum
int sum =0;
// compute the sum for this row
for ( int col=0; col < data[row].length; col++)
{
sum+=data[row][col];
}
// write the sum for this row
System.out.println( sum );
}
}
}