C Program to perform addition & subtraction of matrices also find the trace


March 1, 2022, Learn eTutorial
2146

A Matrix is a collection of numbers or elements in Rows and Columns. Each number in the Matrix is called Matrix Elements. In this C program, we have to perform Matrix Addition, Matrix Subtraction. After completing the Addition or Subtraction, we have to find out the Trace of the Resultant Matrix. For achieving this operation on the Matrix, It should follow the condition. The condition is, the Order of the Matrix should be Equal, which means the number of Rows and Columns. The Order of the Matrix is denoted by 'a x b' Where 'a' is the number of Rows, and 'b' is the number of columns in the Matrix.

For doing the Addition of the Matrix, we need to add each element in Matrix A with the corresponding element in Matrix B. Means,

 "sum[i][j] = A[i][j] + B[i][j]"

For doing the Subtraction of the Matrix, we need to Subtract each element in Matrix A with the corresponding element in Matrix B. Means,
 
 "diffmat[i][j] = A[i][j] - B[i][j]".

Trace of a Matrix A is the Sum of the Diagonal elements of the Matrix. So it is calculated by adding the Diagonal elements of the Matrix. Means,

 "Trace = A[1][1]+A[2][2]+A[3][3]....A[n][n]".

This C program's logic is that we have to read the Order of the Matrix from the user. Then we are accepting the elements of the two matrices by using For loop. After getting the elements of the matrices, we print them. Then display a Message to the user to enter,

  • 1 to perform Addition
  • 2 to perform subtraction".

If the user enters 1, then we perform Matrix Addition by using a Nested for loop. Where the Outer for-loop for Rows and inner loop for Columns. In Inside for loop, we add each element in Matrix A to each element in Matrix B. Then, we call the Trace function to get the Trace of the Output Matrix.

If the user enters 2, then we perform Matrix Subtraction in the same way and call the Trace function to find out the Trace of the resultant Matrix. The Trace function adds the Diagonal elements of the given Matrix by using for loop and checking the condition where 'i=j.'

ALGORITHM

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

STEP 2: Initialize and Define the Matrix and the Variables to use in the C program.

STEP 3: Declare the user-defined function Trace to find the Trace of both Added and Subtracted Matrices.

STEP 4: Accept the Order of Matrix A and B using the printf and scanf functions in C programming.

STEP 5: Accept the elements of Matrix A using Nested for loop and scanf function.

STEP 6: Display the Matrix A using Nested for loop and printf function.

STEP 7: Accept matrix B using Nested for loop and scanf function then print it using printf function.

STEP 8: Accept the choice from the user,1 for Addition 2 for Subtraction. 

STEP 9: If the user enters the 1 for performing Addition, then follow step 10 and step 11.

STEP 10: Open the Nested for loop and do the Addition of Matrix elements using formula sumat[i][j] = A[i][j] + B[i][j] and print the Result Matrix using Nested for loop.

STEP 11: Call the Function Trace to get the Trace of the resultant Added Matrix.

STEP 12: If the user enters option for performing Subtraction, follow step 13 and step 14.

STEP 13: Use the nested for loop to find the Subtraction Matrix using the formula diffmat[i][j] = A[i][j] - B[i][j] and print the result matrix.

STEP 14: Call the function Trace to find the Trace of the Subtracted Matrix.

User-Defined function

a: Accept the Matrix from the calling function as parameters to the Trace function.

b: Open a nested for loop and check the condition (i = j) inside the loop using an if condition.

c: If so add the element with the Trace as trace = trace + arr[i][j]

d: Return the Trace to the called function using a C programming language.

C Source Code

                                          #include <stdio.h>
#include <conio.h>

void main()
{

  int A[10][10], B[10][10], sumat[10][10], diffmat[10][10]; /* declares matrix sum and difference as integers  */
  int i, j, M, N, option;
  void trace(int arr[][10], int M, int N); /* declares function trace as void */
  clrscr();
  printf("Enter the order of the matrice A and B\n");
  scanf("%d %d", & M, & N);
  printf("Enter the elements of matrix A\n"); /* enter the order and elements of two matrices */
  for (i = 0; i < M; i++) {
    for (j = 0; j < M; i++) {
      for (j = 0; j < N; j++) {
        printf("=", A[i][j]);
      }
      printf("\n");
    }
    printf("Enter the elements of matrix B\n");
    for (i = 0; i < M; i++) {
      for (j = 0; j < M; i++) {
        for (j = 0; j < M; i++) /* using switch, switch control to addition or multiplication case */ {
          for (j = 0; j < N; j++) {
            sumat[i][j] = A[i][j] + B[i][j];
          }
        }
        printf("Sum matrix is\n");
        for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            printf("=", sumat[i][j]); /* print and calculate the matrix sum */
          }
          printf("\n");
        }
        trace(sumat, M, N); /* calling the function trace for sum output */
        break;
        case 2: for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            diffmat[i][j] = A[i][j] - B[i][j];
          }
        }
        printf("Difference matrix is\n");
        for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            printf("=", diffmat[i][j]); /* calculating and printing the difference of the matrix */
          }
          printf("\n");
        }
        trace(diffmat, M, N); /* calling function trace for difference output */
        break;
      }
      getch();
    } /* End of main() */
    /*Function to find the trace of a given matrix and print it*/

    void trace(int arr[][10], int M, int N)

    {
      int i, j, trace = 0;
      for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) /* calculating trace by adding elements that match i=j as we discussed in logic of program */ {
          if (i == j) {
            trace = trace + arr[i][j];
          }
        }
      }
      printf("Trace of the resultant matrix is = %d\n", trace);
    }
                                      

OUTPUT

Enter the order of the matrix A and B
2* 2

Enter the elements of matrix A
1 1
2 2

MATRIX A is
1  1
2  2

Enter the elements of matrix B
3 3
4 4

MATRIX B is
3  3
4  4

Enter your option: 1 for Addition and 2 for Subtraction
1

Sum matrix is
4  4
6  6

Trace of the resultant matrix is = 10