我们可以加,减,乘和除2个矩阵。为此,我们从用户那里获取行号,列号,第一矩阵元素和第二矩阵元素的输入。然后,我们对用户输入的矩阵执行乘法运算。
在矩阵乘法中,将第一矩阵的一个行元素与第二矩阵的所有列元素相乘。
让我们尝试通过下图了解 3 * 3和3 * 3 矩阵的矩阵乘法:

让我们看看C++中的矩阵乘法程序。
#include <iostream> using namespace std; int main() { int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; cout<<"enter the number of row="; cin>>r; cout<<"enter the number of column="; cin>>c; cout<<"enter the first matrix element=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } } cout<<"enter the second matrix element=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>b[i][j]; } } cout<<"multiply of the matrix=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { mul[i][j]=0; for(k=0;k<c;k++) { mul[i][j]+=a[i][k]*b[k][j]; } } } //for printing result for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<mul[i][j]<<" "; } cout<<"\n"; } return 0; }
输出:
enter the number of row=3 enter the number of column=3 enter the first matrix element= 1 2 3 1 2 3 1 2 3 enter the second matrix element= 1 1 1 2 1 2 3 2 1 multiply of the matrix= 14 9 8 14 9 8 14 9 8