#K35172. Array Operations on 2D Arrays
Array Operations on 2D Arrays
Array Operations on 2D Arrays
You are given a string that represents a 2-dimensional array of integers. The rows are separated by a semicolon (;
) and the integers in each row are separated by a space. Your task is to perform three operations on the array:
- Cumulative Sum (\(\text{cumsum}\)): For each row, compute the cumulative sum. For example, if a row is 1 2 3, its cumulative sum is 1 3 6.
- First-order Difference (\(\Delta\)) along the 0th axis: Compute the difference between consecutive rows. That is, for each column and for rows \(i\) and \(i+1\), compute \(B_{i+1,j} - B_{i,j}\). If there is only one row, this result is an empty matrix.
- Product: For each row, compute the product of its elements.
The output should be printed in three sections:
- The cumulative sum matrix, where each row is printed on a new line with its elements separated by a space.
- A blank line, then the first-order difference matrix in the same format (if there are no rows for the difference, nothing is printed in this section).
- A blank line, then the product of each row printed on a single line, with products separated by a space.
- The cumulative sum matrix. Each row should be printed on a separate line with its values separated by a single space.
- The first order difference matrix computed along the vertical (0th axis). Each row should be printed on a separate line. If there is only one row in the input, this section will be empty (i.e. print nothing for this part).
- The product of each row printed on a single line (space-separated).
Note: All calculations should follow the normal rules of integer arithmetic.
inputFormat
The input consists of a single line. This line is a string representing a 2-D array, where rows are separated by a semicolon (;
) and the integers within a row are separated by spaces.
For example: 1 2 3;4 5 6
outputFormat
The output contains three parts separated by a blank line:
1 2 3;4 5 6
1 3 6
4 9 15
3 3 3
6 120
</p>