C Program to Subtract Two Matrices

How to write a C Program to Subtract Two Matrices?. Or, How to write a C program to subtract one Multi-Dimensional Array from another Matrix with example.

C Program to Subtract Two Matrices Example

C Program to Subtract Two Matrices

This program allows the user to enter the number of rows and columns of two Matrices. Next, we are going to subtract one matrix from another matrix using C For Loop.

/* C Program to Subtract Two Matrices */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], b[10][10];
 	int Subtraction[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++)
    	{
      		Subtraction[rows][columns] = a[rows][columns] - b[rows][columns];    
   	 	}
  	}
 
 	printf("\n After Subtracting Matrix a from Matrix b = a - b \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		printf("%d \t ", Subtraction[rows][columns]);
    	}
    	printf("\n");
  	}
 	return 0;
}
C Program to Subtract Two Matrices Example 1

In this C program, We declared 3 Two dimensional arrays a, b, and Subtraction of size of 10 * 10.

Below C Programming 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 every cell present in a[2][3] matrix. Conditions inside the for loops (rows < i) and (columns < j) will ensure the 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 is to store user entered values into b[2][3] matrix.

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

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

User inserted values for this C Program to Subtract Two Matrices are
a[2][3] = {{10, 20, 30}, { 40, 50, 60}}
b[2][3] = {{25, 95, 65}, { 75, 12, 100}}

Row First Iteration for C Program to Subtract Two Matrices : 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
Subtraction [rows][columns] = a[rows][columns] – b[rows][columns] =  a[0][0] – b[0][0]
Subtraction [0][0] = 10 – 25 = -15

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (1 < 3) is True.
Subtraction [0][1]= a[0][1] – b[0][1]
Subtraction [0][1]= 20 – 95 = -75

Column Second Iteration: for(columns = 2; 2 < 3; 2++)
The condition (1 < 3) is True.
Subtraction [0][2] = a[0][2] – b[0][2]
Subtraction [0][2] = 30 – 65 = -35

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

Next, the value of rows will be 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 Subtraction Matrix