C语言矩阵乘法
C中的矩阵乘法: 我们可以对2个矩阵进行加,减,乘和除运算。为此,我们从用户那里获取行号,列号,第一矩阵元素和第二矩阵元素的输入。然后,我们对用户输入的矩阵执行乘法运算。
在矩阵乘法中,第一个矩阵的一个行元素与第二个矩阵的所有列元素相乘。
让我们尝试通过下图了解 2 * 2和3 * 3 矩阵的矩阵乘法:
让我们看看C中的矩阵乘法程序。
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("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++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
输出:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18
让我们尝试通过下图了解 3 * 3和3 * 3 矩阵的矩阵乘法: