#C1007. 2D Array Operations
2D Array Operations
2D Array Operations
You are given a 2D array (matrix) of integers with N rows and K columns. Your task is to perform the following operations:
- Cumulative Sum: Compute the cumulative sum along axis 0. For each column \(j\), the cumulative sum is defined as \[ S_{ij} = \sum_{k=0}^{i} a_{kj}, \] where \(a_{kj}\) is the element in row \(k\) and column \(j\) of the matrix.
- Row-wise Product: Compute the product of the elements in each row. For row \(i\), the product is defined as \[ P_i = \prod_{j=0}^{K-1} a_{ij}. \]
- First-order Difference: For each row, compute the first-order discrete difference. For row \(i\) and for each column index \(j\) with \(0 \leq j \leq K-2\), define \[ D_{ij} = a_{i, j+1} - a_{ij}. \] If \(K = 1\), then the difference array is empty.
Print the results in the following order:
- The cumulative sum matrix (print \(N\) lines, each line contains \(K\) space-separated integers).
- A blank line.
- The row-wise product (a single line containing \(N\) space-separated integers).
- A blank line.
- The first-order difference matrix (print \(N\) lines, each line contains \(K-1\) space-separated integers). If \(K = 1\), print nothing for this part.
Note: Use \(stdin\) for input and \(stdout\) for output.
inputFormat
The input is read from standard input and has the following format:
N K row1_element1 row1_element2 ... row1_elementK row2_element1 row2_element2 ... row2_elementK ... rowN_element1 rowN_element2 ... rowN_elementK
Where:
- N is the number of rows.
- K is the number of columns.
- Each of the following N lines contains K integers separated by spaces.
outputFormat
The output should be printed to standard output in the following order:
- The cumulative sum matrix printed over N lines; each line contains K space-separated integers.
- A blank line.
- The row-wise product as one line containing N space-separated integers.
- A blank line.
- The first-order difference matrix printed over N lines; each line contains K-1 space-separated integers (if K=1, this part is omitted).
2 3
1 2 3
4 5 6
1 2 3
5 7 9
6 120
1 1
1 1
</p>