← Back to Pointers
Question 461
Row-Major Matrix in One malloc Block
Row-Major Matrix in One malloc Block
Store the whole matrix in a SINGLE block ofrows * colsints and index it row-major (block[r * cols + c]). Input: the first line has two integersrowsandcols. The remaining numbers are therows * colsmatrix 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 formCol 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
Sign in to use hintsTopics:
Pointers
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.