THINK FIRST·CODE LATER

← All labs

Row and column sums of a matrix

Problem

Write a class MatrixSums that reads two integers rows and cols, then rows × cols integers into a two-dimensional array, and prints the sum of each row and of each column:

Row 0: 6
Row 1: 15
Col 0: 5
Col 1: 7
Col 2: 9

The rows are printed first, in order, then the columns.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-3-data-1.txt

2 3 1 2 3 4 5 6
What your program must print for l9-3-data-1.txt
Row 0: 6

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.