//Reads two matrices, multiply them and print the result //Author: Bashir M. Ghandi #include #define ROW 10 #define COL 10 main() { int i, j, k, A[ROW][COL], B[ROW][COL], C[ROW][COL] = {0}; int rowsA, colsA, rowsB, colsB; printf("Enter number of rows for Matrix 1:\n"); scanf("%d", &rowsA); printf("Enter number of columns for Matrix 1:\n"); scanf("%d", &colsA); printf("Enter the %d elements:\n", rowsA*colsA); for (j=0; j < rowsA; j++) for (k=0; k < colsA; k++) scanf("%d", &A[j][k]); printf("Enter number of rows for Matrix 2:\n"); scanf("%d", &rowsB); printf("Enter number of columns for Matrix 2:\n"); scanf("%d", &colsB); printf("Enter the %d elements:\n", rowsB*colsB); for (j=0; j < rowsB; j++) for (k=0; k < colsB; k++) scanf("%d", &B[j][k]); if (colsA != rowsB) { printf("Sorry, you cannot multiply these matrices!\n"); return 0; } else //multiply them { for (i=0; i < rowsA; i++) for (j=0; j < rowsB; j++) for (k=0; k < colsB; k++) C[i][k] += A[i][j] * B[j][k]; } printf("The product of the two matrices is:\n"); for (i=0; i < rowsA; i++) { for (k=0; k < colsB; k++) printf("%5d ", C[i][k]); printf("\n"); } return 0; }