用C语言实现矩阵乘法
时间: 2024-01-21 07:13:58 浏览: 141
C语言实现矩阵乘法算法
以下是用C语言实现矩阵乘法的代码示例:
```c
#include <stdio.h>
#define ROW1 2
#define COL1 3
#define ROW2 3
#define COL2 2
void matrix_multiply(int mat1[][COL1], int mat2[][COL2], int result[][COL2]) {
int i, j, k;
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL2; j++) {
result[i][j] = 0;
for (k = 0; k < COL1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int mat1[ROW1][COL1] = {{1, 2, 3}, {4, 5, 6}};
int mat2[ROW2][COL2] = {{7, 8}, {9, 10}, {11, 12}};
int result[ROW1][COL2];
matrix_multiply(mat1, mat2, result);
printf("Result:\n");
for (int i = 0; i < ROW1; i++) {
for (int j = 0; j < COL2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
```
阅读全文