2025-01-20 21:25:33 +08:00
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdlib.h>
|
2025-01-20 21:30:53 +08:00
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
void mul(int **a ,int **b,int **c ,int m , int k ,int n){
|
|
|
|
int i , j , l ,sum;
|
|
|
|
|
|
|
|
for (i=0;i<m;i++){
|
|
|
|
for(j=0;j<n;j++){
|
|
|
|
sum = 0;
|
|
|
|
for(l=0;l<k;l++){
|
|
|
|
sum += a[i][l]*b[l][j];
|
|
|
|
}c[i][j]=sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
int main(){
|
2025-01-20 21:25:33 +08:00
|
|
|
int i , j ,m=2,k=3,n=4;
|
2025-01-20 21:30:53 +08:00
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
int a[2][3]={{1,2,7},{3,4,1}};
|
|
|
|
int b[3][4]={{5,6,2,4},{7,8,2,4},{7,2,6,1}};
|
2025-01-20 21:30:53 +08:00
|
|
|
|
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
int **arr_a = (int**)malloc(m * sizeof(int*));
|
|
|
|
int **arr_b = (int**)malloc(k * sizeof(int*));
|
|
|
|
int **arr_c = (int**)malloc(m * sizeof(int*));
|
|
|
|
for(i=0;i<m;i++){
|
|
|
|
arr_a[i] = (int*)malloc(k*sizeof(int));
|
|
|
|
arr_c[i] = (int*)malloc(n*sizeof(int));
|
|
|
|
}
|
|
|
|
for(i=0;i<k;i++){
|
|
|
|
arr_b[i] = (int*)malloc(n*sizeof(int));
|
|
|
|
}
|
2025-01-20 21:30:53 +08:00
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
for (i=0;i<m;i++){
|
|
|
|
for(j=0;j<k;j++){
|
|
|
|
arr_a[i][j]=a[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=0;i<k;i++){
|
|
|
|
for(j=0;j<n;j++){
|
|
|
|
arr_b[i][j]=b[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mul(arr_a,arr_b,arr_c,m,k,n);
|
|
|
|
|
|
|
|
for (i=0;i<m;i++){
|
|
|
|
for(j=0;j<n;j++){
|
|
|
|
printf("c[%d][%d]=%d\n",i,j,arr_c[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
free (arr_a);
|
|
|
|
free (arr_b);
|
|
|
|
free (arr_c);
|
2025-01-20 21:30:53 +08:00
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
for(i=0;i<m;i++){
|
|
|
|
free (arr_a[i]);
|
|
|
|
free (arr_c[i]);
|
|
|
|
}
|
|
|
|
for(i=0;i<k;i++){
|
|
|
|
free (arr_b[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0 ;
|
|
|
|
}
|