C Program to find sum and difference of two matrices


March 11, 2022, Learn eTutorial
1366

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is the matrix?

Here is a c program to find the sum and difference of two matrices. A matrix is a gathering of numbers or components which we arrange in rows and columns. Every number in the matrix is called components. In this c program, we have to do addition and subtraction on the matrix, for that first condition is the order of the matrix should be equal. For doing addition or subtraction we have to include every component in 'A' with the comparing component in 'B' Means " sum[i][j] = A[i][j] + B[i][j]".

To implement that logic in this C program, we accept two matrices from the client by calling the function readmatA() and readmatB(). Display the matrices by calling the function printmatA() and printmatB(). Then check the Order of the Matrices is equal or not. If the order of the matrices is not similar, then we will display 'Addition and subtraction not possible.'Else call the function sum(), diff() to perform addition and subtraction of the matrices A and B.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the Arrays A[][], B[][], summat[][], diffmat[][].

STEP 3: Declare some integer variables i, j, R1, C1, R2, C2.

STEP 4: Declare the functions readmatA(), printmatB(), readmatB(), printmatB(), sum(), diff().

STEP 5: Read the order of Matrix A to R1 and C1.

STEP 6: Read the order of Matrix B to R2 and C2.

STEP 7: Check if  R1 != R2 && C1 != C2 ,then Display Addition and Subtraction are not possible and exit.Otherwise do Step 8.

STEP 8: Read the elements of Matrix A by using readmatA().

STEP 9: Then Display the Matrix A by using printmatA().

STEP 10: Read the elements of Matrix B by using readmatB().

STEP 11: Then display Matrix B by using printmatB().

STEP 12: Call the function sum().

STEP 13: Call the function diff().

Function readmatA()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Read the number into A[i][j].

STEP 6: Increment j by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

Function readmatB()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Read the number into B[i][j].

STEP 6: Increment 'j' by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

Function printmatA()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Display A[i][j].

STEP 6: Increment 'j' by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

Function printmatB()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Display B[i][j].

STEP 6: Increment j by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

Function sum()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Sumat[i][j] = A[i][j] + B[i][j]

STEP 6: Increment j by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

STEP 8: Display 'Sum matrix is.'

STEP 9: Set i=0.

STEP 10: By using for loop check i

STEP 11: Set j=0.

STEP 12: By using another for loop check j

STEP 13: Display sumat[i][j].

STEP 14: Increment j by one and repeat step 12.

STEP 15: Increment 'i' by one repeat step 10.

Function diff()

STEP 1: Set i=0.

STEP 2: By using for loop check i

STEP 3: Set j=0.

STEP 4: By using another for loop check j

STEP 5: Diffmat[i][j] = A[i][j] - B[i][j].

STEP 6: Increment j by one and repeat step 4.

STEP 7: Increment 'i' by one repeat step 2.

STEP 8: Display 'Difference Matrix is.'

STEP 9: Set i=0.

STEP 10: By using for loop check i

STEP 11: Set j=0.

STEP 12: By using another for loop check j

STEP 13: Display diffmat[i][j].

STEP 14: Increment j by one and repeat step 12.

STEP 15: Increment 'i' by one repeat step 10.

C Source Code

                                          #include <stdio.h>
#include <stdlib.h>

int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, R1, C1, R2, C2;
void main() {
  void readmatA(); /* Function declarations */
  void printmatA();
  void readmatB();
  void printmatB();
  void sum();
  void diff();
  printf("Enter the order of the matrix A\n");
  scanf("%d %d", & R1, & C1);
  printf("Enter the order of the matrix B\n"); /* enter the order of matrix */
  scanf("%d %d", & R2, & C2);
  if (R1 != R2 && C1 != C2) {
    printf("Addition and subtraction are not possible\n");
    exit(1);
  } else {
    printf("Enter the elements of matrix A\n"); /* enter the elements of the matrix */
    readmatA();
    printf("MATRIX A is\n");
    printmatA();
    printf("Enter the elements of matrix B\n");
    readmatB();
    printf("MATRIX B is\n");
    printmatB();
    sum();
    diff();
  }
}
/* main() */
void readmatA() /* Function to read a matrix A */ {
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C1; j++) {
      scanf("%d", & A[i][j]);
    }
  }
  return;
}
void readmatB() /* Function to read a matrix B */ {
  for (i = 0; i < R2; i++) {
    for (j = 0; j < C2; j++) {
      scanf("%d", & B[i][j]);
    }
  }
}
void printmatA() /* Function to print a matrix A */ {
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C1; j++) {
      printf("%d", A[i][j]);
    }
    printf("\n");
  }
}
void printmatB() /* Function to print a matrix B */ {
  for (i = 0; i < R2; i++) {
    for (j = 0; j < C2; j++) {
      printf("%d", B[i][j]);
    }
    printf("\n");
  }
}
void sum() /*Function to find the sum of elements of matrix A and Matrix B*/ {
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C2; j++) {
      sumat[i][j] = A[i][j] + B[i][j];
    }
  }
  printf("Sum matrix is\n");
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C2; j++) {
      printf("%d", sumat[i][j]);
    }
    printf("\n");
  }
  return;
}
void diff() /*Function to find the difference of elements of matrix A and Matrix B*/ {
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C2; j++) {
      diffmat[i][j] = A[i][j] - B[i][j];
    }
  }
  printf("Difference matrix is\n");
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C2; j++) {
      printf("%d", diffmat[i][j]);
    }
    printf("\n");
  }
  return;
}
                                      

OUTPUT

Enter the order of the matrix A
2 2

Enter the order of the matrix B
2 2

Enter the elements of matrix A
1
2
3
4

MATRIX A is
  1   2
  3   4

Enter the elements of matrix B
2
4
6
8

MATRIX B is
2  4
6  8

Sum matrix is
3   6
9  12

Difference matrix is
-1  -2
-3  -4