Addition of two matrices in C

Written by

Pooja Rao

Program to add two matrices in c

  • Sum of matrices can be done when the matrices are compatible with each other.
  • The matrices are said to be compatible with each other when the number of rows and columns in both the matrices are equal.
  • Thus, if the first matrix has m rows and n columns second matrix should also have m rows and n columns.
  • Thus,  the elements can be added using the following formula: Ci,j = Ai,j + Bi,j   where i is the number of rows and j is the number of columns.

Approach:

  • First we will take the number of rows and columns of each matrix as our input.
  • Next we validate if addition is possible, based upon number of rows and columns of both matrices are equal or not and accordingly either proceed with addition if valid and if invalid inform the user of it.
  • Next, using the above mentioned formula we calculate the sum of the matrices.

Code:

#include<stdio.h>

int main()

{

   int arr1[5][5], arr2[5][5], sum[5][5]; //declaring array of predefined size 5 x 5

   int i, j, rows1,col1, rows2, col2;

printf ("Enter the number of rows in the first matrix\n");

scanf("%d", &rows1);

    printf ("Enter the number of columns in the first matrix\n");

scanf("%d", &col1);

printf ("Enter the number of rows in the second matrix\n");

scanf("%d", &rows2);

    printf ("Enter the number of columns in the second matrix\n");

scanf("%d", &col2);

if ((rows1 != rows2) || (col1!=col2))

{

 printf("\nThe matrices are not compatible. In order to perform sum of matrices number of rows and columns of the matrices should be equal.\n");

}

else

   {

    printf("Enter First Matrix Elements:\n");   //First Matrix

    for(i = 0; i < rows1; i++)

    {

        for(j = 0; j < col1; j++)

        {

           scanf("%d", &arr1[i][j]);

        }

    }

   printf("\nEnter Second Matrix Elements:\n"); //Second Matrix

    for(i = 0; i < rows2; i++)

    {

        for(j = 0; j < col2; j++)

        {

            scanf("%d", &arr2[i][j]);

        }

}

   

   for(i = 0; i < rows1; i++) //Performing addition of Matrix 1 and 2

    {

        for(j = 0; j < col1; j++)

        {

            sum[i][j] = arr1[i][j] + arr2[i][j];

        }

    }

   printf("\nSum of matrices is as follows:\n");

   

    for(i = 0; i < rows1; i++)

    {

        for(j = 0; j < col1; j++)

        {

            printf("%d\t", sum[i][j]);

        }

       printf("\n");

    }

 }

   return 0;   

}

Output:

Case 1: When number of rows and columns of both matrices do not match:

Enter the number of rows in the first matrix

3

Enter the number of columns in the first matrix

2

Enter the number of rows in the second matrix

1

Enter the number of columns in the second matrix

4

The matrices are not compatible. In order to perform sum of matrices number of rows and columns of the matrices should be equal.

 

Case 2: When number of rows and columns of both matrices match:

Enter the number of rows in the first matrix

2

Enter the number of columns in the first matrix

3

Enter the number of rows in the second matrix

2

Enter the number of columns in the second matrix

3

Enter First Matrix Elements:

1

2

1

4

1

5

Enter Second Matrix Elements:

2

0

3

4

1

0

Sum of matrices is as follows:

3 2 4

8 2 5

Addition of two matrices in C