#include <stdio.h>
#define M 3
#define N 4
#define P 3
void fun(int a[M][N], int b[N][P], int m, int p, int n)//定义函数fun
{
printf("m: %d, p: %d, n: %d\n", m, p, n);
int c[M][P]={0};
int i, j, k;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
for(k = 0; k < p; k++)
{
c[i][j] += a[i][k] * b[k][j];//计算相乘后矩阵各个值
}
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("c[%d][%d]: %3d ", i, j, c[i][j]);
}
printf("\n");
}
}
int main()
{
int a[M][N] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; //2行4列
int b[N][P] = {{1,2},{3,4},{5,6},{7,8}}; //4行2列
fun(a, b, M, N, P);//调用矩阵相乘函数fun
return 0;
}
运行结果: