import java.util.*; import java.io.*; public class TwoDEx { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("data.txt")); int rows = input.nextInt(); int cols = input.nextInt(); int [][] data = new int[rows][cols]; for (int i = 0; i < rows; i++) { // read the data into the array by rows for (int j = 0; j < cols; j++) { data[i][j] = input.nextInt(); } } int[] rowTotals = new int[rows]; // calculate the row totals for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { rowTotals[i] += data[i][j]; } } int[] colTotals = new int[cols]; // calculate the column totals for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { colTotals[j] += data[i][j]; } } System.out.printf("%6s\n","Total"); System.out.printf("%6s\n","-----"); for (int i = 0; i < rows; i++) { // print out the matrix System.out.printf("%6s",rowTotals[i]); for (int j = 0; j < cols; j++) { System.out.printf("%5d",data[i][j]); } System.out.println(); } System.out.printf("%6s","Total:"); for (int i = 0; i < cols; i++) { System.out.printf("%5d",colTotals[i]); } } }