This C programs perform multiplication of two matrices. In case of matrix multiplication
- The number of columns of the 1st matrix must be equal the number of rows of the 2nd matrix.
- And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.
To compute the matrix multiplication, we need to do the dot product of rows and columns. For instance if (1, 2, 3) is first row of first matrix and (7, 9, 11) is first column of second matrix, then dot product is
(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58
C program to perform matrix multiplication - Source code
Program Output
Program Explanation
1. In this program three functions are declared and defined. One function (readMatrix) to take the matrices as input, second function (printMatrix) to display the matrices and another function (productMatrix) to perform matrix multiplication.
2. First, user is asked to input the number of columns of both matrices one by one. If number of columns of first matrix are not equal to rows of second matrix, user is asked to re-renter them again.
3. Next, the matrix elements are accepted by the user. Here note that same function (readMatrix) is called with different arguments for both the matrix.
4. Both the matrices are displayed using the function printMatrix.
5. Function productsMatrix is called with first first matrix, second matrix, multiplication matrix and rows and columns of both matrices as arguments.
6. In function productMatrix, dot product is performed. Here note that the elements of the multiplication matrix are initialized to zero before assigning them the product values.
7. At last the resultant matrix is displayed using the printMatrix function.
need an explanation for this answer? contact us directly to get an explanation for this answer