C Program to Perform Addition, Subtraction and Trace of 2 Matrices

«
»

This C program calculates the addition or subtraction & trace of 2 matrices.

Problem Description

This is a C program which calculates the addition or subtraction & trace of 2 matrices. Here trace of the matrix is the sum of the elements on the main diagonal i.e the diagonal from the upper left to the lower right of a matrix.

Problem Solution

1. Create four 2D arrays (matrices), 2 for storing data, one for storing sum of the element of first two matrices, one for storing difference of first two matrices.
2. Define elements of the first two matrices.
3. Now, make a menu having two options- (i) sum and (ii) difference.
4. For evaluating sum and difference, we will use nested loop with two iterators.
5. First iterator will be used to locate positions row wise. Whereas second iterator will be used to locate positions column wise.
6. Using both iterators we will locate each position of both arrays, take sum of elements of the array occurring at the same position and storing the sum in the third array at the same location. Same step to be followed for difference.
7. Now make a function to calculate the trace of these sum and difference arrays, in which we will pass the required array with its no. of rows and columns.
8. Inside this function, create a variable to store the value of trace. To calculate trace, all the diagonal elements for the array are summed up.

Program/Source Code

Here is source code of the C program to calculates the addition or subtraction & trace of 2 matrices. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to read two matrices A(MxN) and B(MxN) and perform addition
  3.      * OR subtraction of A and B. Also, find the trace of the resultant
  4.      * matrix. Display the given matrices, their sum or differences and
  5.      * the trace.
  6.      */
  7.  
  8.     #include <stdio.h>
  9.     void trace(int arr[][10], int m, int n);
  10.     void main()
  11.     {
  12.  
  13.         int array1[10][10], array2[10][10], arraysum[10][10],
  14.         arraydiff[10][10];
  15.         int i, j, m, n, option;
  16.  
  17.         printf("Enter the order of the matrix array1 and array2 \n");
  18.         scanf("%d %d", &m, &n);
  19.  
  20.         printf("Enter the elements of matrix array1 \n");
  21.         for (i = 0; i < m; i++) 
  22.         {
  23.             for (j = 0; j < n; j++)
  24.             {
  25.                 scanf("%d", &array1[i][j]);
  26.             }
  27.         }
  28.  
  29.         printf("MATRIX array1 is \n");
  30.         for (i = 0; i < m; i++) 
  31.         {
  32.             for (j = 0; j < n; j++) 
  33.             {
  34.                 printf("%3d", array1[i][j]);
  35.             }
  36.             printf("\n");
  37.         }
  38.  
  39.         printf("Enter the elements of matrix array2 \n");
  40.         for (i = 0; i < m; i++) 
  41.         {
  42.             for (j = 0; j < n; j++) 
  43.             {
  44.                 scanf("%d", &array2[i][j]);
  45.             }
  46.         }
  47.  
  48.         printf("MATRIX array2 is \n");
  49.         for (i = 0; i < m; i++) 
  50.         {
  51.             for (j = 0; j < n; j++) 
  52.             {
  53.                 printf("%3d", array2[i][j]);
  54.             }
  55.             printf("\n");
  56.         }
  57.  
  58.         printf("Enter your option: 1 for Addition and 2 for Subtraction \n");
  59.         scanf("%d", &option);
  60.  
  61.         switch (option)
  62.         {
  63.  
  64.         case 1:
  65.             for (i = 0; i < m; i++) 
  66.             {
  67.                 for (j = 0; j < n; j++) 
  68.                 {
  69.                     arraysum[i][j] = array1[i][j] + array2[i][j];
  70.                 }
  71.             }
  72.  
  73.             printf("Sum matrix is \n");
  74.             for (i = 0; i < m; i++) 
  75.             {
  76.                 for (j = 0; j < n; j++) 
  77.                 {
  78.                     printf("%3d", arraysum[i][j]) ;
  79.                 }
  80.                 printf("\n");
  81.             }
  82.  
  83.             trace (arraysum, m, n);
  84.             break;
  85.  
  86.         case 2:
  87.             for (i = 0; i < m; i++) 
  88.             {
  89.                 for (j = 0; j < n; j++) 
  90.                 {
  91.                     arraydiff[i][j] = array1[i][j] - array2[i][j];
  92.                 }
  93.             }
  94.  
  95.             printf("Difference matrix is \n");
  96.             for (i = 0; i < m; i++) 
  97.             {
  98.                 for (j = 0; j < n; j++) 
  99.                 {
  100.                     printf("%3d", arraydiff[i][j]) ;
  101.                 }
  102.                 printf("\n");
  103.             }
  104.             trace (arraydiff, m, n);
  105.             break;
  106.         }
  107.  
  108.     }
  109.  
  110.     /*  Function to find the trace of a given matrix and print it */
  111.  
  112.     void trace (int arr[][10], int m, int n) 
  113.     {
  114.  
  115.         int i, j, trace = 0;
  116.         for (i = 0; i < m; i++)
  117.         {
  118.             for (j = 0; j < n; j++) 
  119.             {
  120.                 if (i == j)
  121.                 {
  122.                     trace = trace + arr[i][j];
  123.                 }
  124.             }
  125.         }
  126.         printf("Trace of the resultant matrix is = %d\n", trace);
  127.     }
Program Explanation

1. Declare four 2D array of some fixed capacity. 2 for storing data, one for storing sum of the elements of first two matrices, one for storing difference of first two matrices.
2. Take the number of rows and columns as input from users. Accordingly define the elements of first two arrays.
3. Create a menu using switch case, having two option of sum and difference of the elements of the first two arrays.
4. For evaluating both, make a nested loop with 2 iterators i and j, where i will track the row of array and j will track the column of the array.
5. Inside loop, elements from both the array from each location are added and the sum is stored at the same location of the third 2D array.
6. For evaluating difference, same step will be followed as above except difference of the elements will be stored in the 4th 2D array.
7. Create a function called trace() to print the trace of the passed array to it.
8. In this function, again a nested loop will be used to get the diagonal elements (when row tracker(i) == column tracker(j)).
9. A variable trace which has initial value 0, will keep on adding the diagonal elements. Print trace.

advertisement
advertisement
Runtime Test Cases
 
Enter the order of the matrix array1 and array2
3 3
Enter the elements of matrix array1
2 3 4
7 8 9
5 6 8
MATRIX array1 is
  2  3  4
  7  8  9
  5  6  8
Enter the elements of matrix array2
3 3 3
3 4 6
8 4 7
MATRIX array2 is
  3  3  3
  3  4  6
  8  4  7
Enter your option: 1 for Addition and 2 for Subtraction
1
Sum matrix is
  5  6  7
 10 12 15
 13 10 15
Trace of the resultant matrix is = 32
 
$ a.out
Enter the order of the matrix array1 and array2
3 3
Enter the elements of matrix array1
10 20 30
15 18 20
12 14 16
MATRIX array1 is
 10 20 30
 15 18 20
 12 14 16
Enter the elements of matrix array2
1 5 9
10 15 14
9 12 13
MATRIX array2 is
  1  5  9
 10 15 14
  9 12 13
Enter your option: 1 for Addition and 2 for Subtraction
2
Difference matrix is
  9 15 21
  5  3  6
  3  2  3
Trace of the resultant matrix is = 15

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.