In previous post, i’ve explained how to write a program to perform matrix addition. Now we’ll look how to perform matrix multiplication in C++. Before continuing, this picture will explain you how multiply 2 matrices manually.

An example of matrix multiplication |

I hope no more explanation is needed for this. To perform matrix multiplication, number of columns of first matrix should be equal to number of rows of second matrix.

Steps:

- Read number of rows and columns of first matrix
- Read first matrix
- Read number of rows and columns of second matrix
- Check whether no.of columns in first matrix = no.of rows in second matrix
- Read second matrix
- Multiply the matrices
- Print the result

Easy as that! Now see these steps converted into C++ code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include<iostream> using namespace std; int main() { int mat1[10][10], mat2[10][10], mat3[10][10]; int row1, row2, col1, col2; int i, j, k; /* Read the size of matrix 1 */ cout<<"Enter the number of rows of matrix 1: "; cin>>row1; cout<<"Enter the number of cols of matrix 1: "; cin>>col1; /* Read the first matrix */ cout<<"Enter the elements in matrix 1:n"; for(i=0; i<row1; ++i) { for(j=0; j<col1; ++j) { cin>>mat1[i][j]; } } /* Read the size of matrix 2 */ cout<<"Enter the number of rows of matrix 1: "; cin>>row2; cout<<"Enter the number of cols of matrix 1: "; cin>>col2; /* Check whether both matrices satisfy the condition to multiply */ if(col1 != row2) { cout<<"These matrices cannot be multiplied!"; return 0; } /* Read the second matrix */ cout<<"Enter the elements in matrix 2:n"; for(i=0; i<row2; ++i) { for(j=0; j<col2; ++j) { cin>>mat2[i][j]; } } /* Calculate the product matrix */ for(i=0; i<row1; ++i) { for(j=0; j<col2; ++j) { mat3[i][j] = 0; for(k=0; k<col1; ++k) { mat3[i][j] += mat1[i][k] * mat2[k][j]; } } } /* Print the result */ cout<<"nSum of 2 matrices:"; for(i=0; i<row1; ++i) { cout<<"n"; for(j=0; j<col2; ++j) { cout<<mat3[i][j]<<" "; } } return 0; } |

Let me explain again. We operate each rows of first matrix with each columns of matrix 2. By saying ‘operate’, i mean multiplying corresponding elements and adding up all together as in picture above. Hence we need 3 for-loops in nested form to multiply 2 matrices. The outermost for-loop loops through each rows of matrix 1. Second for-loop loops through each column of second matrix. We do the actual calculation in the innermost for-loop which multiplies corresponding elements of currently selected row and column and add up to get one of the element in result matrix. Before starting innermost loop we set the current element of result matrix to zero, to prevent garbage value from being added up.

You have a lot more to do to make it a perfect one, which i have skipped for keeping it simple:

- Check whether entered size of matrix is below the maximum of array size.
- You may ask for the user to enter size of second matrix before entering matrix 1, thus telling the user whether it cannot be multiplied before entering the whole matrix.
- Use pointers instead of matrices, thus allowing the user to enter matrix of any size at the same time saving memory.
- You may print the result while calculating, thus saving a few lines of code.