CJCoding With Joseph
15free / daySign inCreate account

Row-Major Matrix in One malloc Block

Store the whole matrix in a SINGLE block of rows * cols ints and index it row-major (block[r * cols + c]).

Input: the first line has two integers rows and cols. The remaining numbers are the rows * cols matrix values in row-major order (row 0 first, left to right).

Allocate ONE block with malloc, read every value into it, print the matrix back one row per line with values separated by a single space and no trailing space, then print the sum of each column on a final line in the exact form Col sums: a b c.

For the input:

2 3
1 2 3
4 5 6

the output is exactly what is shown in Expected Output.

Each line ends with a newline. Do not allocate a second array.

Free the memory you allocate before the program ends — this question checks for memory leaks.

Expected Output:

1 2 3
4 5 6
Col sums: 5 7 9

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
2 3\n1 2 3\n4 5 6\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.