In: Computer Science
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
C Program:
#include <stdio.h> #include <stdlib.h> #define RMAX 10 #define CMAX 10 int main() { int i, j, k, r, c; int A[RMAX][CMAX], B[RMAX][CMAX], Z[RMAX][CMAX]; printf("Enter #rows #columns: "); scanf("%d %d", &r, &c); //Reading matrix printf("\n Enter %d elements for matrix A: ", (r*c)); //Iterating over rows for(i=0; i<r; i++) { //Iterating over columns for(j=0; j<c; j++) { scanf("%d", &A[i][j]); } } //Reading matrix printf("\n Enter %d elements for matrix B: ", (r*c)); //Iterating over rows for(i=0; i<c; i++) { //Iterating over columns for(j=0; j<r; j++) { scanf("%d", &B[i][j]); } } //Matrix Multiplication for(i=0; i<r; i++) { //Iterating over columns for(j=0; j<r; j++) { //Set to zero Z[i][j] = 0; for(k=0; k<c; k++) { //Matrix Multiplication Z[i][j] += (A[i][k] * B[k][j]); } } } //Printing matrix Z printf("\n Matrix Z: \n\n"); //Iterating over rows for(i=0; i<r; i++) { //Iterating over columns for(j=0; j<r; j++) { printf("%d\t", Z[i][j]); } printf("\n"); } printf("\n"); return 0; }