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}\)
Write a C program which calculates the addition of two matrices of the same size.
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.
- Addition of Two Matrix in C using For Loop
- Addition of Two Matrix in C using Function
- Advanced Approach for Adding Two Matrices in C
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}\)
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.
/* C Program to add two matrices of same size using for loop */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int **a = (int **)malloc((unsigned) r * sizeof(int *));
int **b = (int **)malloc((unsigned) r * sizeof(int *));
int **res = (int **)malloc((unsigned) r * sizeof(int *));
for (i = 0; i < r; i++)
{
a[i] = (int *)malloc((unsigned) c * sizeof(int));
b[i] = (int *)malloc((unsigned) c * sizeof(int));
res[i] = (int *)malloc((unsigned) c * sizeof(int));
}
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] + b[i][j];
}
}
printf("The result matrix is:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%6.d", res[i][j]);
}
printf("\n");
}
}
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.
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]: 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
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}\)
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.
/*
* C Program to add two matrices of same size using function
*/
#include <stdio.h>
#include <stdlib.h>
void add(int **a, int **b, int **res, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] + b[i][j];
}
}
}
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int **a = (int **)malloc((unsigned) r * sizeof(int *));
int **b = (int **)malloc((unsigned) r * sizeof(int *));
int **res = (int **)malloc((unsigned) r * sizeof(int *));
for (i = 0; i < r; i++)
{
a[i] = (int *)malloc((unsigned) c * sizeof(int));
b[i] = (int *)malloc((unsigned) c * sizeof(int));
res[i] = (int *)malloc((unsigned) c * sizeof(int));
}
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
add(a, b, res, r, c);
printf("The result matrix is:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%6.d", res[i][j]);
}
printf("\n");
}
}
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.
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
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.
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.
/*
* C Program to add two matrices of same size using functions
*/
#include <stdio.h>
#include <stdlib.h>
void printMatrix(int **a, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
void initializeMatrix(int **a, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
}
void addMatrix(int **a, int **b, int **res, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] + b[i][j];
}
}
}
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int **a = (int **)malloc(r * sizeof(int *));
int **b = (int **)malloc(r * sizeof(int *));
int **res = (int **)malloc(r * sizeof(int *));
for (i = 0; i < r; i++)
{
a[i] = (int *)malloc(c * sizeof(int));
b[i] = (int *)malloc(c * sizeof(int));
res[i] = (int *)malloc(c * sizeof(int));
}
printf("Enter the elements of first matrix:\n");
initializeMatrix(a, r, c);
printf("Enter the elements of second matrix:\n");
initializeMatrix(b, r, c);
addMatrix(a, b, res, r, c);
printf("The result matrix is:\n");
printMatrix(res, r, c);
}
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.
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]
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C Books
- Apply for Computer Science Internship