#C13413. Matrix Multiplication with Validations

    ID: 42949 Type: Default 1000ms 256MiB

Matrix Multiplication with Validations

Matrix Multiplication with Validations

Given two matrices (A) and (B), your task is to compute the product (A \times B) if possible. The multiplication is defined only if the number of columns in (A) equals the number of rows in (B). In addition, ensure that every element in both matrices is numeric (an integer or a real number). If the matrices have incompatible dimensions, output the error message: "Incompatible matrix dimensions for multiplication." If any element in the matrices is non-numeric, output the error message: "Matrices should only contain numeric values."

The input is provided via standard input (stdin) and the output must be produced via standard output (stdout). The result should be printed as a matrix where each row is printed on a separate line with the elements separated by a single space. For example, the multiplication of a (2 \times 3) matrix with a (3 \times 2) matrix is computed as follows:

Cij=k=1nAik×BkjC_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj}

inputFormat

The input is given via stdin. The first line contains two space-separated integers (n) and (m) representing the number of rows and columns of matrix (A). The next (n) lines each contain (m) values (which can be integers or floating-point numbers) separated by spaces, representing matrix (A). Then, a line follows with two space-separated integers (p) and (q) representing the number of rows and columns of matrix (B). The next (p) lines each contain (q) values separated by spaces, representing matrix (B).

outputFormat

If the matrices can be multiplied, output the resulting matrix, with each row on a new line and the values separated by a single space. If the matrices have incompatible dimensions, output exactly: "Incompatible matrix dimensions for multiplication." If any matrix element is non-numeric, output exactly: "Matrices should only contain numeric values."## sample

2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
58 64

139 154

</p>