Addition of Two Matrix in C

Matrix addition in C is used to add two matrices. i.e. calculate and print the sum of them.

Example:

Given two matrices of same size, this program will add the corresponding elements of each matrix and print the result.

Input:
First Matrix: \(\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\)

Second Matrix: \(\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\)

Output:
\(\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\) + \(\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\) = \(\begin{bmatrix}
1+1 & 2+2 & 3+3\\
4+4 & 5+5 & 6+6
\end{bmatrix}\) = \(\begin{bmatrix}
2 & 4 & 6\\
8 & 10 & 12\end{bmatrix}\)

Problem Description

Write a C program which calculates the addition of two matrices of the same size.

Problem Solution
advertisement
advertisement

In order to add two matrices, we need to add the corresponding elements of each matrix. Suppose we have two matrices of size m x n and p x q.

Algorithm:
1. Take the number of rows and columns as input.
2. Store the number in a variable.
3. Initialize the first matrix.
4. Initialize the second matrix.
5. Initialize the result matrix.
6. Inside a loop, add the corresponding elements of the two matrices and store them in the result matrix.
7. Print the result matrix.

There are several ways to add two matrices in C language. Let’s take a detailed look at all the approaches for adding matrices in C.

Method 1: Addition of Two Matrix in C using For Loop (Naive Approach)

In this approach, we’ll use a for loop to loop through the number of rows and columns and add the corresponding elements of the two matrices and store them in the result matrix.

Example:

Input:
First Matrix: \(\begin{bmatrix}
2 & 5\\
32 & 65\\
23 & 76
\end{bmatrix}\) and Second Matrix: \(\begin{bmatrix}
23 & 65\\
35 & 2\\
4 & 0
\end{bmatrix}\)

Output:
\(\begin{bmatrix}
2 & 5\\
32 & 65\\
23 & 76
\end{bmatrix}\) + \(\begin{bmatrix}
23 & 65\\
35 & 2\\
4 & 0
\end{bmatrix}\) = \(\begin{bmatrix}
2+23 & 5+65\\
32+35 & 65+2\\
23+4 & 76+0
\end{bmatrix}\) = \(\begin{bmatrix}
25 & 70\\
67 & 67\\
27 & 76
\end{bmatrix}\)

Program/Source Code

Here is source code of the C program to add two matrices using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. /* C Program to add two matrices of same size using for loop */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void)
  7. {
  8.     int r, c;
  9.     printf("Enter the number of rows: ");
  10.     scanf("%d", &r);
  11.     printf("Enter the number of columns: ");
  12.     scanf("%d", &c);
  13.     int i, j;
  14.     int **a = (int **)malloc((unsigned) r * sizeof(int *));
  15.     int **b = (int **)malloc((unsigned) r * sizeof(int *));
  16.     int **res = (int **)malloc((unsigned) r * sizeof(int *));
  17.     for (i = 0; i < r; i++)
  18.     {
  19.         a[i] = (int *)malloc((unsigned) c * sizeof(int));
  20.         b[i] = (int *)malloc((unsigned) c * sizeof(int));
  21.         res[i] = (int *)malloc((unsigned) c * sizeof(int));
  22.     }
  23.     printf("Enter the elements of first matrix:\n");
  24.     for (i = 0; i < r; i++)
  25.     {
  26.         for (j = 0; j < c; j++)
  27.         {
  28.             printf("[%d][%d]: ", i, j);
  29.             scanf("%d", &a[i][j]);
  30.         }
  31.     }
  32.     printf("Enter the elements of second matrix:\n");
  33.     for (i = 0; i < r; i++)
  34.     {
  35.         for (j = 0; j < c; j++)
  36.         {
  37.             printf("[%d][%d]: ", i, j);
  38.             scanf("%d", &b[i][j]);
  39.         }
  40.     }
  41.     for (i = 0; i < r; i++)
  42.     {
  43.         for (j = 0; j < c; j++)
  44.         {
  45.             res[i][j] = a[i][j] + b[i][j];
  46.         }
  47.     }
  48.     printf("The result matrix is:\n");
  49.     for (i = 0; i < r; i++)
  50.     {
  51.         for (j = 0; j < c; j++)
  52.         {
  53.             printf("%6.d", res[i][j]);
  54.         }
  55.         printf("\n");
  56.     }
  57. }
Program Explanation

1. The program will ask the user to enter the number of rows and columns and will return the result matrix.
2. The program will loop through the number of rows and columns and add the corresponding elements of the two matrices and store them in the result matrix.
3. Here, the memory allocation is done a bit differently. We’ll use malloc to allocate the memory for the two matrices and the result matrix.
4. First we allocate the rows of the three matrices and then we allocate the columns.

Time complexity: O(n2)
The time complexity of this algorithm is O(n2), where n is the number of digits in the number.

Space Complexity: O(n2)
space complexity of this algorithm is O(n2), because n2 needs extra space for storing the results.

Runtime Test Cases

In this case, we enter “3” for the number of rows and “2” for the number of columns as input for the addition of two matrices.

advertisement
Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of first matrix:
[0][0]: 2
[0][1]: 5
[1][0]: 32
[1][1]: 65
[2][0]: 23
[2][1]: 76
Enter the elements of second matrix:
[0][0]: 23
[0][1]: 65
[1][0]: 35
[1][1]: 2
[2][0]: 4
[2][1]: 0
The result matrix is:
    25    70
    67    67
    27    76

Method 2: Addition of Two Matrix in C using Function

In this approach, we’ll use a function to add the corresponding elements of the two matrices and store them in the result matrix.

Method used:
add(int **, int **, int **, int, int): This function adds the corresponding elements of the two matrices and stores them in the result matrix.

Example:

Input:
First Matrix: \(\begin{bmatrix}
34 & 64\\
86 & 0\\
34 & -25
\end{bmatrix}\) and Second Matrix: \(\begin{bmatrix}
65 & -67\\
576 & -674\\
58 & 435
\end{bmatrix}\)

Output:
\(\begin{bmatrix}
34 & 64\\
86 & 0\\
34 & -25
\end{bmatrix}\) + \(\begin{bmatrix}
65 & -67\\
576 & -674\\
58 & 435
\end{bmatrix}\) = \(\begin{bmatrix}
34+65 & 64-67\\
86+576 & 0-674\\
34+58 & -25+435
\end{bmatrix}\) = \(\begin{bmatrix}
99 & -3\\
662 & -674\\
92 & 410
\end{bmatrix}\)

Program/Source Code

Here is source code of the C program to add two matrices using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to add two matrices of same size using function
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void add(int **a, int **b, int **res, int r, int c)
  9. {
  10.     int i, j;
  11.     for (i = 0; i < r; i++)
  12.     {
  13.         for (j = 0; j < c; j++)
  14.         {
  15.             res[i][j] = a[i][j] + b[i][j];
  16.         }
  17.     }
  18. }
  19.  
  20. int main(void)
  21. {
  22.     int r, c;
  23.     printf("Enter the number of rows: ");
  24.     scanf("%d", &r);
  25.     printf("Enter the number of columns: ");
  26.     scanf("%d", &c);
  27.     int i, j;
  28.     int **a = (int **)malloc((unsigned) r * sizeof(int *));
  29.     int **b = (int **)malloc((unsigned) r * sizeof(int *));
  30.     int **res = (int **)malloc((unsigned) r * sizeof(int *));
  31.     for (i = 0; i < r; i++)
  32.     {
  33.         a[i] = (int *)malloc((unsigned) c * sizeof(int));
  34.         b[i] = (int *)malloc((unsigned) c * sizeof(int));
  35.         res[i] = (int *)malloc((unsigned) c * sizeof(int));
  36.     }
  37.     printf("Enter the elements of first matrix:\n");
  38.     for (i = 0; i < r; i++)
  39.     {
  40.         for (j = 0; j < c; j++)
  41.         {
  42.             printf("[%d][%d]: ", i, j);
  43.             scanf("%d", &a[i][j]);
  44.         }
  45.     }
  46.     printf("Enter the elements of second matrix:\n");
  47.     for (i = 0; i < r; i++)
  48.     {
  49.         for (j = 0; j < c; j++)
  50.         {
  51.             printf("[%d][%d]: ", i, j);
  52.             scanf("%d", &b[i][j]);
  53.         }
  54.     }
  55.     add(a, b, res, r, c);
  56.     printf("The result matrix is:\n");
  57.     for (i = 0; i < r; i++)
  58.     {
  59.         for (j = 0; j < c; j++)
  60.         {
  61.             printf("%6.d", res[i][j]);
  62.         }
  63.         printf("\n");
  64.     }
  65. }
Program Explanation

1. The program will ask the user to enter the number of rows and columns.
2. Then it will initialize the first matrix, initialize the second matrix and initialize the result matrix.
3. The program will then call the function to add the corresponding elements of the two matrices and store them in the result matrix.
4. In the function, we’ll loop through the number of rows and columns and add the corresponding elements of the two matrices and store them in the result matrix.
5. Print the result matrix.

Time complexity: O(n2)
The time complexity of this algorithm is O(n2), where n is the number of digits in the number.

Space Complexity: O(n2)
space complexity of this algorithm is O(n2), because n2 needs extra space for storing the results.

Runtime Test Cases

In this case, we enter “3” for the number of rows and “2” for the number of columns as input for the addition of two matrices.

Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of first matrix:
[0][0]: 34
[0][1]: 64
[1][0]: 86
[1][1]: 0
[2][0]: 34
[2][1]: -25
Enter the elements of second matrix:
[0][0]: 65
[0][1]: -67
[1][0]: 576
[1][1]: -674
[2][0]: 58  
[2][1]: 435
The result matrix is:
    99    -3
   662  -674
    92   410

Method 3: Advanced Approach for Adding Two Matrices in C

In this approach, we’ll use functions to add the corresponding elements of the two matrices and store them in the result matrix.

Methods used:

  • printMatrix(int **, int, int): This function will print the matrix.
  • initializeMatrix(int **, int, int): This function will initialize the matrix.
  • addMatrix(int **, int **, int **, int, int): This function will add the corresponding elements of the two matrices and store them in the result matrix.
Program/Source Code

Here is source code of the C program to add two matrices using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to add two matrices of same size using functions
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void printMatrix(int **a, int r, int c)
  9. {
  10.     int i, j;
  11.     for (i = 0; i < r; i++)
  12.     {
  13.         for (j = 0; j < c; j++)
  14.         {
  15.             printf("%d ", a[i][j]);
  16.         }
  17.         printf("\n");
  18.     }
  19. }
  20.  
  21. void initializeMatrix(int **a, int r, int c)
  22. {
  23.     int i, j;
  24.     for (i = 0; i < r; i++)
  25.     {
  26.         for (j = 0; j < c; j++)
  27.         {
  28.             printf("[%d][%d]: ", i, j);
  29.             scanf("%d", &a[i][j]);
  30.         }
  31.     }
  32. }
  33.  
  34. void addMatrix(int **a, int **b, int **res, int r, int c)
  35. {
  36.     int i, j;
  37.     for (i = 0; i < r; i++)
  38.     {
  39.         for (j = 0; j < c; j++)
  40.         {
  41.             res[i][j] = a[i][j] + b[i][j];
  42.         }
  43.     }
  44. }
  45.  
  46. int main(void)
  47. {
  48.     int r, c;
  49.     printf("Enter the number of rows: ");
  50.     scanf("%d", &r);
  51.     printf("Enter the number of columns: ");
  52.     scanf("%d", &c);
  53.     int i, j;
  54.     int **a = (int **)malloc(r * sizeof(int *));
  55.     int **b = (int **)malloc(r * sizeof(int *));
  56.     int **res = (int **)malloc(r * sizeof(int *));
  57.     for (i = 0; i < r; i++)
  58.     {
  59.         a[i] = (int *)malloc(c * sizeof(int));
  60.         b[i] = (int *)malloc(c * sizeof(int));
  61.         res[i] = (int *)malloc(c * sizeof(int));
  62.     }
  63.     printf("Enter the elements of first matrix:\n");
  64.     initializeMatrix(a, r, c);
  65.     printf("Enter the elements of second matrix:\n");
  66.     initializeMatrix(b, r, c);
  67.     addMatrix(a, b, res, r, c);
  68.     printf("The result matrix is:\n");
  69.     printMatrix(res, r, c);
  70. }
Program Explanation

1. The program begins with asking the number of rows and columns and then initializes the matrices using the function initializeMatrix.
2. The program then calls the addMatrix function to add the corresponding elements of the two matrices and store them in the result matrix.
3. Print the result matrix using the function printMatrix().

Time complexity: O(n2)
The time complexity of this algorithm is O(n2), where n is the number of digits in the number.

Space Complexity: O(n2)
space complexity of this algorithm is O(n2), because n2 needs extra space for storing the results.

Runtime Test Cases

In this case, we enter “3” for the number of rows and “4” for the number of columns as input for the addition of two matrices.

Enter the number of rows: 3
Enter the number of columns: 4
Enter the elements of first matrix:
[0][0]: 2
[0][1]: 765
[0][2]: 42
[0][3]: 52
[1][0]: 843
[1][1]: 45
[1][2]: 32
[1][3]: 91
[2][0]: 34
[2][1]: 25
[2][2]: 84
[2][3]: 26
Enter the elements of second matrix:
[0][0]: 48
[0][1]: 25
[0][2]: 18
[0][3]: 258
[1][0]: 59
[1][1]: 25
[1][2]: 89
[1][3]: 23
[2][0]: 28
[2][1]: 292
[2][2]: 5958
[2][3]: 56
The result matrix is:
    50    790     60    310 
   902     70    121    114 
    62    317   6042     82

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.