#K35172. Array Operations on 2D Arrays

    ID: 25473 Type: Default 1000ms 256MiB

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:

  1. The cumulative sum matrix, where each row is printed on a new line with its elements separated by a space.
  2. 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).
  3. A blank line, then the product of each row printed on a single line, with products separated by a space.
  4. 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. The cumulative sum matrix. Each row should be printed on a separate line with its values separated by a single space.
    2. 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).
    3. The product of each row printed on a single line (space-separated).
    ## sample
    1 2 3;4 5 6
    1 3 6
    

    4 9 15

    3 3 3

    6 120

    </p>