c语言中求两个矩阵的乘积
1、
#include <stdio.h> int main(void) { int a[4][3] = {{2,5,4},{1,2,4},{5,3,2},{4,2,6}}; int b[3][4] = {{2,1,6,2},{3,2,4,1},{5,2,1,2}}; int i, j, k; int c[4][4] = {0}; for (i = 0; i < 4; i++) { for (k = 0; k < 4; k++) { for (j = 0; j < 3; j++) { c[i][k] += a[i][j] * b[j][k]; } } } for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { printf("%d ", c[i][j]); } putchar('\n'); } return 0; }
↓
#include <stdio.h>
int main(void)
{
int a[4][3], b[3][4];
int i, j;
puts("please input the elements of 4 * 3 matrix.");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]);
}
}
puts("please input the elements of 3 * 4 matrix.");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
}
}
int k;
int c[4][4] = {0};
for (i = 0; i < 4; i++)
{
for (k = 0; k < 4; k++)
{
for (j = 0; j < 3; j++)
{
c[i][k] += a[i][j] * b[j][k];
}
}
}
puts("output the multiply of the two matrix.");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d ", c[i][j]);
}
putchar('\n');
}
return 0;
}
↓
#include <stdio.h>
int main(void)
{
int a[2][4], b[4][5];
int i, j;
puts("please input the elements of the 2 * 4 matrix");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 4; j++)
{
printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]);
}
}
puts("please input the elements of the 4 * 5 matrix");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
}
}
int c[2][5] = {0};
int k;
for (i = 0; i < 2; i++)
{
for (k = 0; k < 5; k++)
{
for (j = 0; j < 4; j++)
{
c[i][k] += a[i][j] * b[j][k];
}
}
}
puts("output the multiply of the two matrix.");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d ", c[i][j]);
}
putchar('\n');
}
return 0;
}