C语言计算两个矩阵的乘积代码
时间: 2024-05-11 19:17:54 浏览: 118
以下是C语言计算两个矩阵的乘积的代码:
```c
#include <stdio.h>
#define ROW 3
#define COL 3
void matrixMultiply(int a[][COL], int b[][COL], int c[][COL])
{
int i, j, k;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
c[i][j] = 0;
for (k = 0; k < COL; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main()
{
int a[ROW][COL] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[ROW][COL] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int c[ROW][COL];
matrixMultiply(a, b, c);
printf("Matrix A:\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d ", b[i][j]);
}
printf("\n");
}
printf("\nMatrix C (Product of A and B):\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
```
注意这里矩阵的行列数都是3,可以根据需要修改。
阅读全文