#C13892. Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
Given two matrices A and B, perform matrix multiplication if possible. The input consists of two matrices provided in a specific format. Your program should multiply the matrices if their dimensions are compatible, i.e. if the number of columns in A is equal to the number of rows in B. If the matrices cannot be multiplied due to dimension mismatch, output the error message: Cannot multiply the two matrices. Incorrect dimensions.
Note: The multiplication of a matrix A (of size \( r_1 \times c_1 \)) by a matrix B (of size \( r_2 \times c_2 \)) is defined only if \( c_1 = r_2 \). The resulting matrix will have the dimensions \( r_1 \times c_2 \) and each element is computed as:
[ C_{ij} = \sum_{k=1}^{c_1} A_{ik} \times B_{kj} ]
Your solution should read from stdin and write the answer to stdout.
inputFormat
The input consists of two parts representing two matrices:
- The first line contains two integers, \(r_1\) and \(c_1\), representing the number of rows and columns of matrix A.
- The next \(r_1\) lines each contain \(c_1\) space-separated integers representing the rows of matrix A.
- Following that, there is a line with two integers, \(r_2\) and \(c_2\), representing the number of rows and columns of matrix B.
- The next \(r_2\) lines each contain \(c_2\) space-separated integers representing the rows of matrix B.
outputFormat
If the two matrices can be multiplied (i.e. if \(c_1 = r_2\)), output the resulting matrix where each row is printed on a new line with its elements separated by a space.
If the matrices cannot be multiplied, output the error message exactly as follows:
Cannot multiply the two matrices. Incorrect dimensions.## sample
2 2
1 2
3 4
2 2
5 6
7 8
19 22
43 50
</p>