C = AB
- 函数传入二维数组列数一定要是const
- 最终C(i, j)每个位置上的结果是K次乘法相加的结果
#include <bits/stdc++.h>
using namespace std;
const int N = 2, K = 3, M = 4;
void mul(int c[][M], int a[][K], int b[][M]){
int temp[N][M] = {0};
for(int i = 0; i < N; i ++ )
for(int j = 0; j < M; j ++ )
for(int k = 0; k < K; k ++ )
temp[i][j] += a[i][k] * b[k][j];
memcpy(c, temp, sizeof temp);
}
int main(){
int A[N][K] = {
{1, 2, 3},
{4, 5, 6}
};
int B[K][M] = {
{1, 2, 3, 4},
{4, 3, 2, 1},
{5, 6, 7, 8}
};
int C[N][M];
mul(C, A, B);
cout << "C:" << endl;
for(int i = 0; i < N; i ++ ){
for(int j = 0; j < M; j ++ ){
printf("%d ", C[i][j]);
}
puts("");
}
return 0;
}