In: Computer Science
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code)
Can you let me know?
Please find the matmul() implemented function below:
// C program to multiply two matrices. 
#include <stdio.h> 
const int MAX = 100; 
// Function to print Matrix 
void printMatrix(int M[][MAX], int n) 
{ 
    for (int i = 0; i < n; i++) { 
        for (int j = 0; j < n; j++) 
            printf("%d ", M[i][j]); 
  
        printf("\n"); 
    } 
} 
// Function to multiply two matrices A[][] and B[][] 
void matmul(int A[][MAX], int B[][MAX], int n) 
{ 
        int i, j, k; 
        // Matrix to store the result 
        int C[MAX][MAX]; 
        // Multiply the two 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        C[i][j] = 0; 
                        for (k = 0; k < n; k++) 
                                C[i][j] += A[i][k] * B[k][j]; 
                } 
        } 
        // Print the result 
        printf("\nResultant Matrix: \n"); 
        printMatrix(C, n); 
} 
// Main function 
int main() 
{ 
        int n, i, j; 
        int A[MAX][MAX], B[MAX][MAX]; 
        // Read size of Matrix A from user 
        printf("Enter the value of n: "); 
        scanf("%d", &n); 
        // Read the elements of Matrix A from user 
        printf("\nEnter the elements of First Matrix: "); 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        printf("\nA[%d][%d]: ", i, j); 
                        scanf("%d", &A[i][j]); 
                        printf("%d", A[i][j]); 
                } 
        }  
        // Read the elements of Matrix B from user 
        printf("\nEnter the elements of First Matrix: "); 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        printf("\nB[%d][%d]: ", i, j); 
                        scanf("%d", &B[i][j]); 
                        printf("%d", B[i][j]); 
                } 
        } 
        // Print the Matrix A 
        printf("\n\nFirst Matrix: \n"); 
        printMatrix(A, n); 
        // Print the Matrix B 
        printf("\nSecond Matrix: \n"); 
        printMatrix(B, n); 
        // Find the product of the 2 matrices 
        matmul(A, B, n); 
        return 0; 
} 
OUTPUT:
First Matrix: 
1 2 3 
4 5 6 
7 8 9 
Second Matrix: 
1 2 3 
4 5 6 
7 8 9 
Resultant Matrix: 
30 36 42 
66 81 96 
102 126 150