Multiplication of two matrix |C Program|

Introduction

     Matrix multiplication is a fundamental operation in linear algebra and finds extensive use in various applications, including computer graphics, scientific computing, and machine learning. In this blog post, we'll explore a C program that performs matrix multiplication, breaking down each step to help you understand the underlying logic.  

Code

    
     
    #include<stdio.h>
    #include<conio.h>
    void main() {
        int i, j, k, a[10][10], b[10][10], c[10][10];
        int m, n, p, q;
        clrscr();
    
        // Input for the dimensions of the first matrix
        printf("Enter the rows and columns of the first matrix: ");
        scanf("%d%d", &m, &n);
    
        // Input for the dimensions of the second matrix
        printf("Enter the rows and columns of the second matrix: ");
        scanf("%d%d", &p, &q);
    
        // Checking if matrices are compatible for multiplication
        if (n == p) {
            // Input for the elements of the first matrix
            printf("Enter the elements of the first matrix:\n");
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    scanf("%d", &a[i][j]);
                }
            }
    
            // Input for the elements of the second matrix
            printf("Enter the elements of the second matrix:\n");
            for (i = 0; i < p; i++) {
                for (j = 0; j < q; j++) {
                    scanf("%d", &b[i][j]);
                }
            }
    
            // Matrix multiplication
            for (i = 0; i < m; i++) {
                for (j = 0; j < q; j++) {
                    c[i][j] = 0;
                    for (k = 0; k < n; k++) {
                        c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
                    }
                }
            }
    
            // Displaying the result matrix
            printf("Resultant Matrix:\n");
            for (i = 0; i < m; i++) {
                for (j = 0; j < q; j++) {
                    printf("%d\t", c[i][j]);
                }
                printf("\n");
            }
        } else {
            printf("\nMatrices are not compatible for multiplication.\n");
        }
    
        getch();
    }
       
     


Output


    Enter the rows and column of first matrix:2 2
    Enter the rows and column of second matrix:2 2
    Enter the element of first matrix:
    1 2
    3 4
    Enter the element of second matrix:
    1 2
    3 4
    
    Matrix;
    7       10
    15      22
       



Explanation

1. Header Files:
The program includes the standard input/output header <stdio.h> and the console I/O header <conio.h>.
2. Main Function:
The main function initiates the program.
Users are prompted to enter the dimensions of the two matrices.
3. Matrix Compatibility Check:
The program checks if the number of columns in the first matrix (n) is equal to the number of rows in the second matrix (p), making them compatible for multiplication.
4. Matrix Input:
The user is prompted to input the elements of both matrices.
5. Matrix Multiplication:
The program performs matrix multiplication using nested loops, following the standard formula for each element in the result matrix (c[i][j]).
6. Result Display:
The program prints the resulting matrix after multiplication.
7. User Interaction:
Users receive clear feedback about the dimensions of the matrices, the compatibility check, and the resulting matrix if multiplication is possible.


Conclusion

     This C program provides a clear and concise implementation of matrix multiplication, a fundamental operation in linear algebra. Understanding the logic behind matrix multiplication is essential for various computational applications, including graphics rendering and scientific simulations. Experiment with different matrix dimensions, analyze the flow of the program, and consider optimizations or alternative matrix multiplication algorithms. This example serves as a solid introduction to the world of matrix operations in programming. Happy coding!

Post a Comment

0 Comments