c++实现两个矩阵乘法
时间: 2023-11-14 12:04:12 浏览: 76
C++中实现矩阵的加法和乘法实例
5星 · 资源好评率100%
以下是 C 语言实现两个矩阵乘法的示例代码:
```c
#include <stdio.h>
#define ROW_A 2
#define COL_A 3
#define ROW_B 3
#define COL_B 2
void matrix_multiply(int a[][COL_A], int b[][COL_B], int result[][COL_B]) {
int i, j, k;
for(i = 0; i < ROW_A; i++) {
for(j = 0; j < COL_B; j++) {
result[i][j] = 0;
for(k = 0; k < COL_A; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void print_matrix(int matrix[][COL_B], int row, int col) {
int i, j;
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrix_a[ROW_A][COL_A] = {{1, 2, 3}, {4, 5, 6}};
int matrix_b[ROW_B][COL_B] = {{7, 8}, {9, 10}, {11, 12}};
int result[ROW_A][COL_B];
matrix_multiply(matrix_a, matrix_b, result);
print_matrix(result, ROW_A, COL_B);
return 0;
}
```
运行结果为:
```
58 64
139 154
```
阅读全文