Addition of two matrix |C Program|

Introduction

     Matrix addition is a foundational operation in linear algebra, serving as a fundamental building block for numerous computational applications. In this exploration, we'll dive into a C program designed to add two matrices, unraveling the intricacies of the code and offering unique insights into the world of matrix operations.  


Code

    
     
    #include<stdio.h>
    #include<conio.h>
    void main() {
        int a[10][10], b[10][10], c[10][10];
        int i, j, m, n;
        clrscr();
    
        // Input for the dimensions of the matrices
        printf("Enter the number of rows and columns for the matrices: ");
        scanf("%d%d", &m, &n);
    
        // 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 < m; i++) {
            for (j = 0; j < n; j++) {
                scanf("%d", &b[i][j]);
            }
        }
    
        // Matrix addition
        printf("Matrix Addition:\n");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                c[i][j] = a[i][j] + b[i][j];
                printf("%d\t", c[i][j]);
            }
            printf("\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 matrices.
3. Matrix Input:
The user is prompted to input the elements of both matrices.
4. Matrix Addition:
The program performs matrix addition using nested loops, summing corresponding elements from both matrices and storing the result in a new matrix (c).
5. Result Display:
The program prints the resulting matrix after addition.
6. User Interaction:
Users receive clear feedback about the dimensions of the matrices and the resulting matrix after addition.


Conclusion

     This C program not only performs matrix addition but serves as an educational tool, offering a window into the world of matrix manipulation. Experiment with different matrix dimensions, explore the impact of various input values, and delve into the possibilities of matrix operations in programming. As you journey through the code, may you gain a deeper understanding of linear algebra's practical applications. Happy coding!

Post a Comment

0 Comments