Beginner-friendly blog post explaining how to add two 2D matrices in C
Introduction
In this blog post, we’ll learn how to add two 2D matrices in C programming. Matrix addition is a basic operation, often seen in mathematics and programming, where corresponding elements from two matrices are added to create a new matrix.
Let’s start by understanding what a matrix is and how to perform matrix addition in C with a step-by-step explanation.
What is a Matrix?
A matrix is a rectangular array of numbers arranged in rows and columns. In programming, we usually use a 2D array to represent a matrix.
For example, a 2×2 matrix can look like this:
1 2
3 4
If we add another 2×2 matrix to it, we get:
Matrix 1: Matrix 2: Result:
1 2 5 6 6 8
3 4 7 8 10 12
Steps for Matrix Addition
- First, make sure both matrices have the same dimensions (same number of rows and columns).
- Then, add each element in Matrix 1 to the corresponding element in Matrix 2.
- Store the result in a third matrix.
Let’s see how we can write this in a C program.
Matrix Addition Code in C
#include <stdio.h>
int main() {
int rows, cols;
// Step 1: Get matrix dimensions
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
// Step 2: Input elements for the first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Step 3: Input elements for the second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Step 4: Add corresponding elements of both matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Step 5: Display the resultant matrix
printf("Resultant matrix after addition:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Explanation of Code, Line by Line
Let’s go through the code step-by-step to understand each line.
1. Including the Standard I/O Library
#include <stdio.h>
- This line includes the standard input-output library in C, which allows us to use functions like
printfandscanf.
2. Declaring main Function
int main() {
- Here, we start our
mainfunction where the execution of the program begins.
3. Declaring Variables for Rows and Columns
int rows, cols;
- We declare two integer variables,
rowsandcols, to store the number of rows and columns for the matrices.
4. Taking Matrix Dimensions from the User
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
- We ask the user to enter the number of rows and columns for the matrices. The
scanffunction reads the input values and stores them inrowsandcols.
5. Declaring Matrices
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
- We declare three 2D arrays:
matrix1andmatrix2for the input matrices andsumto store the result of the addition.
6. Taking Input for First Matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
- Here, we prompt the user to enter the elements of the first matrix.
- We use two
forloops to iterate over each element in the matrix. - Inside the inner loop, we take input for each element and store it in
matrix1at the specified position.
7. Taking Input for Second Matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
- This block of code is similar to Step 6, but here we store the values in
matrix2.
8. Adding Corresponding Elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
- After we have both matrices, we use two nested loops to add each corresponding element.
- For each position
(i, j), we addmatrix1[i][j]andmatrix2[i][j]and store the result insum[i][j].
9. Displaying the Resultant Matrix
printf("Resultant matrix after addition:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
- Finally, we display the result by iterating over the
summatrix. - Each row is printed on a new line to maintain the matrix format.
10. Ending the Program
return 0;
}
- The
return 0;statement indicates that the program finished successfully.
Output Example
If the user enters matrices like:
Matrix 1: Matrix 2:
1 2 5 6
3 4 7 8
The output will be:
Resultant matrix after addition:
6 8
10 12
Conclusion
Matrix addition is a straightforward concept where you add each corresponding element of two matrices. In this program, we learned how to take input for matrices, add them element-by-element, and display the result. This code serves as a foundation for more advanced matrix operations in C programming.