c语言中计算矩阵的乘积。
矩阵相乘的条件:左侧矩阵的列数等于右侧矩阵的行数。
矩阵相乘的结果:行数为左侧矩阵的行数,列数为右侧矩阵的列数。
#include <stdio.h>
int main(void)
{
int i, j, k, a[4][6], b[6][7], c[4][7] = {0};
puts("please input the elements of matrix a.");
for(i = 0; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]);
}
}
puts("\nshow the matrix for of matrix a.");
for(i = 0; i < 4; i++)
{
for(j = 0; j < 6; j++)
{
printf("%4d", a[i][j]);
}
putchar('\n');
}
puts("\nplease input the elements of matrix b.");
for(i = 0; i < 6; i++)
{
for(j = 0; j < 7; j++)
{
printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
}
}
puts("\nshow the matrix form of matrix b.");
for(i = 0; i < 6; i++)
{
for(j = 0; j < 7; j++)
{
printf("%4d", b[i][j]);
}
putchar('\n');
}
puts("\n===================================");
for(i = 0; i < 4; i++)
{
for(j = 0; j < 7; j++)
{
for(k = 0; k < 6; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
puts("show the product of the two matrixes.");
for(i = 0; i < 4; i++)
{
for(j = 0; j < 7; j++)
{
printf("%4d", c[i][j]);
}
putchar('\n');
}
return 0;
}