#C1452. Square Matrix Operations
Square Matrix Operations
Square Matrix Operations
You are given two square matrices A and B of order \( n \). Your task is to implement operations on these matrices by creating a class SquareMatrix
that supports:
- Initialization (ensure that the matrix is square, otherwise throw an error)
- String representation: Convert the matrix into a string where each row is printed on a new line and elements in a row are separated by a single space.
- Transpose: The transpose of a matrix \( A \) is defined as \( A^T \) where \( A^T[i][j] = A[j][i] \).
- Addition: Matrix addition of two matrices of the same dimensions.
- Multiplication: Matrix multiplication of two matrices of the same dimensions.
The program will read matrices from standard input and then print the following five outputs in order:
- The original matrix A
- The matrix B
- The transpose of matrix A
- The result of A + B
- The result of A \times B
Each matrix should be printed in the same format as described by its string representation. Separate each printed matrix output by an empty line.
inputFormat
The input is read from standard input in the following format:
n row1_of_A row2_of_A ... (n rows for matrix A) row1_of_B row2_of_B ... (n rows for matrix B)
where:
n
is the order of the square matrices (an integer).- Each row consists of
n
space-separated integers.
outputFormat
Print the following five matrices to standard output in order, each separated by an empty line:
- Matrix A (using the string representation as described).
- Matrix B.
- The transpose of matrix A.
- The sum of matrices A and B.
- The product of matrices A and B.
Each matrix should be printed exactly as described: each row on a new line with space separated integers.
## sample2
1 2
3 4
5 6
7 8
1 2
3 4
5 6
7 8
1 3
2 4
6 8
10 12
19 22
43 50
</p>