Before going to the program first let is understand what is Sum of two Matrix?

Sum of two matrix:

                     Sum of two Matrix is a matrix obtained by adding the corresponding elements of the two given matrix.

But to perform the Sum of two Matrix the number of rows and columns of two matrix must be equal.

Thus the Sum matrix will have same number of rows and columns as the two given matrix.

add_mat

For example,

add_example

Program code for Sum of two Matrix in C:

#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10],i,j,row,col; clrscr(); printf("n Enter the row and column size : "); scanf("%d %d",&row,&col); printf("n Enter the matrix 1 :nn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d",&a[i][j]); } } printf("n Enter the matrix 2 :nn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<row;i++) { for(j=0;j<col;j++) { c[i][j]=a[i][j]+b[i][j]; } } printf("n The sum is : nn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf(" %d ",c[i][j]); } printf("n"); } getch();
}

Working:

  • First the computer reads the rows and columns of matrix 1 and matrix 2 from the user.
  • Then using for loops the computer reads the matrix 1 and matrix 2 from the user.
  • Then using for loops the matrix addition is carried out.
  • Finally using for loops the Sum matrix is printed.

Step by step working of the above program:

  1. Let us assume that a user enters the number of rows and columns as 3 and 3 respectively.
  2.  And then enters the elements of matrix 1 and matrix 2 as given below

Matrix 1:  1 1 1

   1 1 1

   1 1 1

Matrix 2:  2 2 2

   2 2 2

   2 2 2

  1. Then it assigns the value of i=0 and j=0 and the loop continues till condition of the for loop is true.

3.1.   i<row    (0<3)    for loop condition is true

3.1.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[0][0]=a[o][o]+b[o][o])    So  c[0][0]=3

j++     (j=j+1)    So  j=1

3.1.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[0][1]=a[o][1]+b[o][1])    So  c[0][1]=3

j++     (j=j+1)    So  j=2

3.1.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[0][2]=a[o][2]+b[o][2])    So  c[0][2]=3

j++     (j=j+1)    So  j=3

3.1.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=1   ,   j=0

3.2.   i<row    (1<3)    for loop condition is true

3.2.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[1][0]=a[1][o]+b[1][o])    So  c[1][0]=3

j++     (j=j+1)    So  j=1

3.2.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[1][1]=a[1][1]+b[1][1])    So  c[1][1]=3

j++     (j=j+1)    So  j=2

3.2.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[1][2]=a[1][2]+b[1][2])    So  c[1][2]=3

j++     (j=j+1)    So  j=3

3.2.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=2   ,   j=0

3.3.   i<row    (2<3)    for loop condition is true

3.2.1.   j<col     (0<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[2][0]=a[2][o]+b[2][o])    So  c[2][0]=3

j++     (j=j+1)    So  j=1

3.2.2.   j<col     (1<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[2][1]=a[2][1]+b[2][1])    So  c[2][1]=3

j++     (j=j+1)    So  j=2

3.2.3.   j<col     (2<3)    for loop condition is true

c[i][j]=a[i][j]+b[i][j]     (c[2][2]=a[2][2]+b[2][2])    So  c[2][2]=3

j++     (j=j+1)    So  j=3

3.2.4.   j<col     (3<3)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j.

i++     (i=i+1)    So   i=3   ,   j=0

3.4.   i<row    (3<3)    for loop condition is false

It comes out of the for loop.

  1. Finally using two for loops it prints the elements of the Sum matrix as given below:

Sum matrix: 3 3 3

 3 3 3

 3 3 3

  1. Thus the program execution is completed.

Output:

Sum of two matrix

Sum of two matrix

TO DOWNLOAD THE PROGRAM CODE :CLICK HERE

Similar Posts