#include
#include
int **cal(int a[][3], int b[][3]);
int main() {
int a[3][3], b[3][3], i, j;
//输入数组a
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
//输入数组b
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
//计算矩阵乘积
int **mul = cal(a, b);
//打印
for (i = 0; i < 3; i++)
printf("%d %d %d\n", mul[i][0], mul[i][1], mul[i][2]);
//释放动态数组
for (i = 0; i < 3; i++)
free(mul[i]);
free(mul);
return 0;
}
int **cal(int a[][3], int b[][3]) {
//创建动态数组
int **p = (int **) malloc(3 * sizeof(int *)), i, j;
for (i = 0; i < 3; i++)
p[i] = (int *) malloc(3 * sizeof(int));
//计算
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
p[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
return p;
}
代码效果图
运行效果