#include <stdio.h> #include <stdlib.h>
int main(void) { int row1, col1, row2, col2; int i, j, k; int **array1, **array2, **array3;
printf("input row1 and col1: \n"); scanf("%d%d", &row1, &col1); printf("row1 = %d, col1 = %d\n", row1, col1); array1 = (int** )malloc(row1 * sizeof(int* )); for(i = 0; i < row1; i++){ *(array1+i) = (int* )malloc(col1 * sizeof(int)); } printf("input matrix1:\n"); for(i = 0; i < row1; i++){ for(j = 0; j < col1; j++){ scanf("%d", &array1[i][j]); } } printf("output matrix1:\n"); for(i = 0; i < row1; i++){ for(j = 0; j < col1; j++){ printf("%d\t", array1[i][j]); } printf("\n"); }
printf("input row2 and col2:\n"); scanf("%d%d", &row2, &col2); printf("rw2 = %d, col2 = %d\n", row2, col2); array2 = (int** )malloc(row2 * sizeof(int* )); for(i = 0; i < row2; i++){ array2[i] = (int* )malloc(col2 * sizeof(int)); } printf("input matrix2: \n"); for(i = 0; i < row2; i++){ for(j = 0; j < col2; j++){ scanf("%d", &array2[i][j]); } } printf("output maxtrix2:\n"); for(i = 0; i < row2; i++){ for(j = 0; j < col2; j++){ printf("%d\t", array2[i][j]); } printf("\n"); }
printf("*************************************\n"); array3 = (int** )malloc(row1 * sizeof(int* )); for(i = 0; i < row1; i++){ array3[i] = (int* )malloc(col2 * sizeof(int)); }
for(i = 0; i < row1; i++){ for(j = 0; j < col2; j++){ for(k = 0; k < col1; k++){ array3[i][j] = array3[i][j] + array1[i][k]*array2[k][j]; } } }
printf("output the result: \n"); for(i = 0; i < row1; i++){ for(j = 0; j < col2; j++){ printf("%d\t", array3[i][j]); } printf("\n"); }
for(i = 0; i < row1; i++){ free(array1[i]); free(array3[i]); } free(array1);
for(i = 0; i < row2; i++){ free(array2[i]); } free(array2);
exit(0); }
|