#C6301. Matrix Operations

    ID: 50047 Type: Default 1000ms 256MiB

Matrix Operations

Matrix Operations

This problem requires you to implement a Matrix class (or equivalent functionality) that supports initialization, addition, multiplication, and transposition. Matrices are two-dimensional arrays of numbers.

For addition, given two matrices A and B of the same dimensions, the result C is defined by:

\( C_{ij} = A_{ij} + B_{ij} \)

For multiplication, if matrix A is of dimensions \( r \times c \) and matrix B is of dimensions \( c \times q \), the product matrix C is computed as:

\( C_{ij} = \sum_{k=1}^{c} A_{ik} \times B_{kj} \)

The transpose of a matrix A of dimensions \( r \times c \) is a new matrix AT of dimensions \( c \times r \) defined by:

\( (A^T)_{ij} = A_{ji} \)

Your solution should read input from stdin and output the result matrix to stdout in the specified format.

inputFormat

The first token in the input is a string that specifies the operation to perform: either add, multiply, or transpose.

  • If the operation is add:
    • Read two integers r and c representing the number of rows and columns.
    • Read r lines each containing c integers for the first matrix.
    • Read another pair of integers r and c (which will be the same as the first) for the second matrix.
    • Read r additional lines for the second matrix.
  • If the operation is multiply:
    • Read two integers r and c for the first matrix, followed by r lines each with c integers.
    • Then read two integers p and q for the second matrix (with c = p), followed by p lines with q integers.
  • If the operation is transpose:
    • Read two integers r and c for the matrix, followed by r lines each with c integers.

All input is read from standard input (stdin).

outputFormat

Output the resulting matrix to standard output (stdout), with each row printed on a new line, and elements separated by a single space.

## sample
add
2 2
1 2
3 4
2 2
5 6
7 8
6 8

10 12

</p>