8.4 Coding Addition and Subtraction of Matrices in C
8. Basic Matrix Programming in C
Full Course on Data Structures and Algorithms in C
By: Chrysanthus Date Published: 3 Feb 2026
The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.
For two matrices to be added together, both matrices must have the same order, and the summed matrix will be of that order. That is, if the augend matrix has n rows, the addend matrix must also have n rows; if the augend matrix has m columns, the addend matrix must also have m columns; and the summed (result) matrix will be an n-by-m matrix.
For one matrix to be subtracted from another, both matrices must have the same order, and the difference matrix will be of that order. That is, if the minuend matrix has n rows, the subtrahend matrix must also have n rows; if the minuend matrix has m columns, the subtrahend matrix must also have m columns; and the difference (result) matrix will be an n-by-m matrix.
Adding Two Matrices
Mathematics
The following diagram shows the addition of two 3-by-2 matrices.
When two matrices are added, the corresponding elements in each row and column are added. That is:
Row 1/Column 1: 3 + 1 = 4
Row 1/Column 2: -4 + -4 = -4 – 4 = -8
Row 2/Column 1: 0 + -1 = -1-1 = -1
Row 2/Column 2: -4 + 2 = -4+2 = -2
Row 3/Column 1: 2 + 3 = 5
Row 3/Column 2: 4 + 2 = 6
Program for Adding Two Matrices of Same Order
The addition program is as follows (read through the code and comments):
#include <stdio.h>
void addMatices (int mat1[][2], int mat2[][2], int n, int m) {
int matS[3][2];
for (int i=0; i < n; i++) { //visit rows
for (int j=0; j < m; j++) { //then column cells
matS[i][j] = mat1[i][j] + mat2[i][j]; //addition of corresponding elements
printf("%i, ", matS[i][j]);
}
printf("\n");
}
}
int main() {
int mat1[3][2];
mat1[0][0] = 3; mat1[0][1] = -4;
mat1[1][0] = 0; mat1[1][1] = -4;
mat1[2][0] = 2; mat1[2][1] = 4;
int mat2[3][2];
mat2[0][0] = 1; mat2[0][1] = -4;
mat2[1][0] = -1; mat2[1][1] = 2;
mat2[2][0] = 3; mat2[2][1] = 2;
int n=3, m=2;
addMatices(mat1, mat2, n, m);
return 0;
}
The output is:
4, -8,
-1, -2,
5, 6,
as expected.
The time complexity is actually O(3 x n x m), where 'n x m' refers to the time to iterate over all the elements in the matrix (2D array) in the called function. This same amount of time is needed to populate the two given matrices with numbers, one-by-one, in the calling function area of the program. And so that makes '3 x n x m'. However, the coefficient (multiplicand of 3) is normally omitted when quoting the time complexity, to give a quoted time complexity of O(n x m), here.
The space complexity is actually O(3 x n x m) for the three matrices: two in the calling function area and one in the called function. However, the coefficient (multiplicand of 3) is normally omitted when quoting the space complexity, to give a quoted space complexity of O(n x m). Any temporary space is ignored.
Subtracting One Matrix from Another
Mathematics
The following diagram shows the subtraction with two 3-by-2 matrices.
When one matrix is subtracted from another, subtraction takes place between corresponding elements in each row and column. That is:
Row 1/Column 1: -2 - 4 = -6
Row 1/Column 2: 3 - -1 = 3 + 1 = 4
Row 2/Column 1: 1 - -3 = 1+3 = 4
Row 2/Column 2: -4 - 2 = -4-2 = -6
Row 3/Column 1: 0 - 1 = -1
Row 3/Column 2: 2 - -2 = 2+2 = 4
Program to Subtract One Matrix from Another, of Same Order
The subtraction program is as follows (read through the code and comments):
#include <stdio.h>
void addMatices (int mat1[][2], int mat2[][2], int n, int m) {
int matS[3][2];
for (int i=0; i < n; i++) { //visit rows
for (int j=0; j < m; j++) { //then column cells
matS[i][j] = mat1[i][j] - mat2[i][j]; //subtraction of corresponding elements
printf("%i, ", matS[i][j]);
}
printf("\n");
}
}
int main() {
int mat1[3][2];
mat1[0][0] = -2; mat1[0][1] = 3;
mat1[1][0] = 1; mat1[1][1] = -4;
mat1[2][0] = 0; mat1[2][1] = 2;
int mat2[3][2];
mat2[0][0] = 4; mat2[0][1] = -1;
mat2[1][0] = -3; mat2[1][1] = 2;
mat2[2][0] = 1; mat2[2][1] = -2;
int n=3, m=2;
addMatices(mat1, mat2, n, m);
return 0;
}
The output is:
-6, 4,
4, -6,
-1, 4,
as expected.
The time complexity is actually O(3 x n x m), where 'n x m' refers to the time to iterate over all the elements in the matrix (2D array) in the called function. This same amount of time is needed to populate the two given matrices with numbers, one-by-one, in the calling function area of the program. And so that makes '3 x n x m'. However, the coefficient (multiplicand of 3) is normally omitted when quoting the time complexity, to give a quoted time complexity of O(n x m), here.
The space complexity is actually O(3 x n x m) for the three matrices: two in the calling function area and one in the called function. However, the coefficient (multiplicand of 3) is normally omitted when quoting the space complexity, to give a quoted space complexity of O(n x m). Any temporary space is ignored.
Thanks for reading.
Related Links
More Related LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.