C Program to Add Two Matrices

How to write a C Program to Add Two Matrices?. Or, How to write a C program to add two Multi-Dimensional Arrays with example.

C Program to Add Two Matrices Example 1

C Program to Add Two Matrices

This program for matrix addition in c allows the user to enter the number of rows and columns of two Matrices. Next, we are going to add those two matrices using For Loop.

/* C Program to Add Two Matrices */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], b[10][10];
 	int Addition[10][10];
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the First Matrix Elements\n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}
   
 	printf("\n Please Enter the Second Matrix Elements\n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		scanf("%d", &b[rows][columns]);
    	}
  	}
  
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		Addition[rows][columns] = a[rows][columns] + b[rows][columns];    
   	 	}
  	}
 
 	printf("\n The Sum of Two Matrix a and b = a + b \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		printf("%d \t ", Addition[rows][columns]);
    	}
    	printf("\n");
  	}
 	return 0;
}
C Program to Add Two Matrices Example

In this matrix addition in c Program, We declared 3 Two dimensional arrays a, b, and Addition of size of 10 * 10.

Below statements asks the User to enter the Matrices a, b sizes (Number of rows and columns. For instance 2 Rows, 3 Columns = a[2][3] and b[2][3])

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate each cell present in a[2][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the C Programming compiler, not to exceed the Matrix limit. Otherwise, the matrix will overflow

scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

Next, for Loop in C matrix addition program is to store user entered values into b[2][3] matrix.

In the next program line, We have one more for loop to perform addition.

for(rows = 0; rows < i; rows++)
  {
   for(columns = 0;columns < j;columns++)
    {
      Addition[rows][columns] = a[rows][columns] + b[rows][columns];   
    }
  }

User inserted values for this matrix addition in c example are

a[2][3] = {{10, 20, 30}, { 40, 50, 60}}
b[2][3] = {{25, 35, 45}, { 55, 65, 75}}

C Program to Add Two Matrices – Row First Iteration: for(rows = 0; rows < 2; 0++)
The condition (0 < 2) is True. So, it will enter into second for loop

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True. So, it will start executing the statements inside the loop
Addition [rows][columns] = a[rows][columns] + b[rows][columns] =  a[0][0] + b[0][0]
Addition [0][0] = 10 + 25 = 35

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (1 < 3) in this program for matrix addition in c is True.
Addition [0][1]= a[0][1] + b[0][1]
Addition [0][1]= 20 + 35 = 55

Column Second Iteration: for(columns = 2; 2 < 3; 2++)
The condition (1 < 3) is True.
Addition [0][2] = a[0][2] + b[0][2]
Addition [0][2]= 30 + 45 = 75

Next, j value will increment. After the increment, the condition inside the second for loop (columns < 3) will fail. So it will exit from the loop.

Now the value of rows incremented (rows will become 1) and starts the second-row iteration.

Please follow the same steps where rows = 1

At last, we used another for loop to print the Addition Matrix in C

Comments are closed.