C Code Snippets 7 - Matrix Addition



In mathematics, matrix addition is the operation of adding two matrices by adding the corresponding entries together.



 Let us take two matrices namely A and B;



/**
* Author : Berk Soysal
*/

#include <stdio.h>

#define ROW 2
#define COL 3

int main()
{
    int a[ROW][COL] = {5, 3, 7,  0, 1, 2};
    int b[ROW][COL] = {1, 2, 3,  4, 5, 6};

    int c[ROW][COL];
    int i, j;

    printf("Matrix A:\n");
    for(i=0; i<ROW; i++)
    {
        for(j=0; j<COL; j++)
            printf("%4d",a[i][j]);
        printf("\n");
    }

    printf("Matrix B:\n");
    for(i=0; i<ROW; i++)
    {
        for(j=0; j<COL; j++)
            printf("%4d",b[i][j]);
        printf("\n");
    }

    printf("Resulting Matrix C:\n");
    for(i=0; i<ROW; i++)
    {
        for(j=0; j<COL; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
            printf("%4d",c[i][j]);
        }
        printf("\n");
    }

    return 0;
}


Result











 


If you have any questions or comments, please leave a comment below. Thanks.

  Keywords: CCode Snippets
Author:

Software Developer, Codemio Admin

Disqus Comments Loading..