Matrix Multiplication Calculator
Time limit2sMemory limit512 MB
Read pairs of matrices with dimension lines, multiply them when the inner dimensions match, and print each result row wrapped in vertical bars, or print undefined.
- Level
Easy2 of 10
- Topics
- Implementation, Matrix, Math, Brute force
- Solved
- No attempts yet
Problem
Matrix multiplication is a basic operation of linear algebra, with applications across mathematics, applied mathematics, computer graphics, physics, and engineering.
Two matrices can be multiplied only when their dimensions are compatible: the number of columns of the first matrix must equal the number of rows of the second matrix.
If is an matrix and is an matrix, the product is an matrix. The product is defined as the matrix with
Build a matrix multiplication calculator that multiplies two given matrices and prints the result. If the matrices cannot be multiplied, print undefined.
Input
The input consists of several test cases. For each test case, the first line gives four positive integers (). and are the dimensions of matrix , while and are the dimensions of matrix . The next lines hold matrix , followed by lines holding matrix . Each row lists its entries separated by single spaces. The input ends with a line 0 0 0 0, which is not processed.
Output
For each test case, print a line Case #x:, where is the case number starting from . On the following lines, print the result of the multiplication. When the product exists, print one row of the result matrix per line in the form | entries |, wrapped in | characters with entries separated by single spaces. When the matrices cannot be multiplied, print a single line undefined.