Adding Two Matrices in C

Adding Two Matrices in C

3 mins read398 Views Comment
Updated on Mar 11, 2024 16:32 IST

A matrix is a multidimensional array of numbers organized in rows and columns. Matrices can be manipulated using operations like Addition, Subtraction, and Multiplication.

2023_03_Feature-Image-Templates-3.jpg

Adding two matrices in C, is a binary operation that takes two matrices of the same size and returns a new matrix in which each element is the sum of the corresponding elements in the original matrices. 

To better understand this article, it is recommended that you have prior knowledge of the following C Programming topics: 

Today, we will be covering the following sections: 

Introduction to Matrix Addition 

Matrix addition is a fundamental operation in linear algebra. if A and B are two m × n matrices, then their sum A + B is a matrix of the same size as A and B, where the element in the ith row and jth column is obtained by adding the element in the ith row and jth column of A to the element in the ith row and jth column of B.  

The notation for matrix addition is as follows: 

A + B = [aij + bij]mxn where 1 ≤ i ≤ m and 1 ≤ j ≤ n. 

For example, consider the matrices A and B: 

A = [1 2 3 

     4 5 6] 

B = [7  8  9 

     10 11 12] 

Their sum A + B is: 

A + B = [1+7     2+8     3+9 

              4+10   5+11   6+12] 

= [8    10   12 

     14  16  18] 

You can also explore: Building Better Code with Enum in C: Best Practices and Examples

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

– / –
40 hours
4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
– / –
4 months

Properties of Matrix Addition 

The properties of matrix addition are: 

  • Commutative property: The order of adding matrices does not affect the result, i.e., A + B = B + A for any two matrices A and B of the same size. 
  • Associative property: The grouping of matrices in addition does not affect the result, i.e., (A + B) + C = A + (B + C) for any three matrices A, B, and C of the same size. 
  • Identity property: There exists a matrix called the additive identity matrix, denoted by O, such that A + O = A for any matrix A of the same size as O. The additive identity matrix is a matrix of all zeros. 
  • Inverse property: For every matrix A of the same size, there exists a unique matrix called the additive inverse matrix, denoted by -A, such that A + (-A) = O, where O is the additive identity matrix. The additive inverse matrix is obtained by changing the sign of each element in the original matrix. 
  • Scalar multiplication distributive property: The distributive property holds for scalar multiplication with matrix addition, i.e., k(A + B) = kA + kB for any scalar k and any two matrices A and B of the same size. 

Note that matrix addition is not multiplicative, i.e., in general, A + B ≠ AB, where AB denotes matrix multiplication.  

You can also explore: Switch Case in C Programming: A Comprehensive Guide

Adding Two Matrices in C 

In C programming language, you can add two matrices using nested loops to iterate over each element of the matrices and add the corresponding elements. Here’s an example code to add two matrices of size m x n:  


 
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (1-100): ");
scanf("%d", &r);
printf("Enter the number of columns (1-100): ");
scanf("%d", &c);
printf("\nEnter elements of Matrix 1:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of Matrix 2:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of the two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
Copy code

Output: 

2023_03_image-63.jpg

In this example, we have two 2×2 square matrices: a and b. The user is asked to enter the number of rows r and columns c. Then, the user is asked to enter the elements of the two matrices (of order rxc). 

We then add the corresponding elements of both the matrices and save it into another 2×2 matrix (2D array). This resulting matrix is then displayed on the screen.  

Endnotes 

Hope this article was helpful for you to understand how to add two matrices in C. If you want to learn more about C programming and solidify your basics, you can explore our articles on C. 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio