Published: 18 Feb 2025 | Reading Time: 4 min read
Matrix addition is a fundamental operation in linear algebra, widely used in various fields such as computer graphics, machine learning, and scientific computing. Matrices are rectangular arrays of numbers arranged in rows and columns, powerful tools for representing and manipulating data in multiple dimensions. The addition of two matrix program in C involves combining two matrices of the same dimensions by adding their corresponding elements.
This article explores how to use different methods to add two matrices using the C programming language, providing detailed explanations of algorithms, implementations, and examples.
A matrix is a rectangular array of numbers arranged in rows and columns. In C, matrices are represented as two-dimensional arrays. For matrix addition to be valid, both matrices must have the same dimensions (i.e., the same number of rows and columns).
Given two matrices A and B, both of size m×n, the matrix sum A+B is a matrix of the same size, with each element obtained by adding the corresponding elements from matrices A and B.
A+B=[aij+bij]mxn
Where:
In C programming, matrices are implemented using arrays. A matrix can be represented as a 2D array, where each element is accessed using row and column indices.
int A[100][100]; // For a matrix A with 100 rows and 100 columns
A[i][j] // Accesses the element at the i-th row and j-th column
A multidimensional array in C is essentially an array of arrays. For matrix representation, the most common multidimensional array is a 2D array, which is used to store rows and columns of a matrix.
int matrix[3][3]; // A 3x3 matrix
matrix[0][0] = 1; // First row, first column
matrix[2][2] = 5; // Third row, third column
int A[100][100], B[100][100], sum[100][100];
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum[i][j] = A[i][j] + B[i][j]; // Element-wise addition
}
}
An array of structures in C is an array where each element is a structure. In the context of matrices, this could be used to store matrices in a more complex way, if you want to add extra metadata or properties to each matrix element.
struct MatrixElement {
int value;
char label[10];
};
struct MatrixElement A[100][100]; // 2D array of structure
A[0][0].value = 1; // Set value of the first element
strcpy(A[0][0].label, "first"); // Assign a label to the first element
struct MatrixElement sum[100][100];
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum[i][j].value = A[i][j].value + B[i][j].value;
}
}
Matrix addition is defined as the process of adding two matrix elements. Two matrices, A and B, must have the same number of rows and columns to be added. The resulting matrix C will also have the same dimensions, where each element is calculated as follows:
Cij = Aij + Bij
This operation is similar to adding algebraic expressions, where only like terms can be combined. In the case of matrices, only corresponding elements from the two matrices can be added together.
Adding in any order does not affect the result. If A and B are matrices of the same dimensions, then:
A+B=B+A
This means that you can add matrices in any order, and the sum will remain the same.
The way in which matrices are grouped during addition does not affect the result. If A, B, and C are matrices of the same dimensions, then:
(A+B)+C=A+(B+C)
This allows for flexibility in how calculations are performed when adding multiple matrices.
An additive identity, known as the zero matrix, results in the original matrix A when added to any matrix A. The zero matrix has all its elements equal to zero:
A+0=A
This property ensures that adding a zero matrix does not change the value of the original matrix.
For every matrix A, there exists an additive inverse (denoted as −A) such that:
A+(−A)=0
Here, −A is the matrix where each element is the negation of the corresponding element in A. This property allows for the cancellation of matrices.
When a matrix is multiplied by a scalar, the result can still be added to another matrix of the same dimension. If k is a scalar and A and B are matrices, then:
k(A+B)=kA+kB
This property shows how scalar multiplication interacts with matrix addition.
In C, a matrix can be represented as a 2D array (array of arrays).
#include <stdio.h>
int main() {
// Define a 3x3 matrix using an array
int matrix[3][3] = {
{5, 10, 15},
{20, 25, 30},
{35, 40, 45}
};
// Print the matrix
printf("Matrix created using an array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Matrix created using an array:
5 10 15
20 25 30
35 40 45
This approach uses nested loops to initialize and print a 3x3 matrix.
#include <stdio.h>
int main() {
int matrix[3][3];
// Initialize the matrix elements using nested loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = (i + 1) * (j + 1); // Example initialization
}
}
// Print the matrix
printf("Matrix created using a nested loop:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Matrix created using a nested loop:
1 2 3
2 4 6
3 6 9
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 3;
// Dynamic memory allocation for a matrix
int **matrix = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++)
matrix[i] = (int *)malloc(cols * sizeof(int));
// Initialize the matrix elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = (i + j) * (i - j); // Example initialization
}
}
// Print the matrix
printf("Matrix created using dynamic memory allocation:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Free allocated memory
for (int i = 0; i < rows; i++)
free(matrix[i]);
free(matrix);
return 0;
}
Matrix created using dynamic memory allocation:
0 -1 -2
1 0 -1
2 -1 0
#include <stdio.h>
int main() {
int rows, cols;
scanf("%d %d", &rows, &cols);
int A[rows][cols], B[rows][cols], C[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
C[i][j] = A[i][j] + B[i][j];
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Input:
2 3
1 2 3
4 5 6
7 8 9
10 11 12
Output:
8 10 12
14 16 18
This program demonstrates how to add two square matrices. A square matrix has an equal number of rows and columns.
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int A[n][n], B[n][n], C[n][n];
// Input elements of first square matrix
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
// Input elements of second square matrix
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &B[i][j]);
}
}
// Adding matrices A and B, storing result in C
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Output the resultant matrix
printf("Resulting Matrix (A + B):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Enter the size of the square matrix: 2
Enter elements of Matrix A:
1 2
3 4
Enter elements of Matrix B:
5 6
7 8
This program adds two rectangular matrices where the number of rows and columns can vary. It ensures that both matrices have the same dimensions before performing the addition.
#include <stdio.h>
int main() {
int r, c;
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &r, &c);
int A[r][c], B[r][c], C[r][c];
// Input elements of the first rectangular matrix
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &A[i][j]);
}
}
// Input elements of the second rectangular matrix
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &B[i][j]);
}
}
// Adding matrices A and B, storing result in C
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Output the resultant matrix
printf("Resulting Matrix (A + B):\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Enter the number of rows and columns for the matrices: 2 3
Enter elements of Matrix A:
1 2 3
4 5 6
Enter elements of Matrix B:
7 8 9
10 11 12
[Flowchart image showing the matrix addition process]
#include <stdio.h>
int main() {
int r, c;
int mat1[100][100], mat2[100][100], sum[100][100];
// Input dimensions
printf("Enter number of rows: ");
scanf("%d", &r);
printf("Enter number of columns: ");
scanf("%d", &c);
// Input first matrix
printf("Enter elements of Matrix 1:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
// Input second matrix
printf("Enter elements of Matrix 2:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}
// Adding matrices
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
// Output result
printf("Sum of the two matrices:\n");
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Number of rows: 3
Number of columns: 3
Matrix 1:
0 1 2
3 4 5
6 7 8
Matrix 2:
9 10 11
12 13 14
15 16 17
The sum of the two matrices:
9 11 13
15 17 19
21 23 25
#include <stdio.h>
void addMatrices(int rows, int cols, int A[rows][cols], int B[rows][cols], int result[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
int main() {
int rows, cols;
// Input number of rows and columns
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int A[rows][cols], B[rows][cols], result[rows][cols];
// Input elements of first matrix
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
// Input elements of second matrix
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
// Call the function to add matrices
addMatrices(rows, cols, A, B, result);
// Output the result
printf("Resulting Matrix (A + B):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Enter number of rows and columns: 2 3
Enter elements of Matrix A:
1 2 3
4 5 6
Enter elements of Matrix B:
7 8 9
10 11 12
Resulting Matrix (A + B):
8 10 12
14 16 18
This program demonstrates matrix addition using dynamic memory allocation with malloc() for creating matrices.
#include <stdio.h>
#include <stdlib.h>
void addMatrices(int rows, int cols, int **A, int **B, int **result) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
int main() {
int rows, cols;
// Input number of rows and columns
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
// Dynamic memory allocation for matrices
int **A = (int **)malloc(rows * sizeof(int *));
int **B = (int **)malloc(rows * sizeof(int *));
int **result = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
A[i] = (int *)malloc(cols * sizeof(int));
B[i] = (int *)malloc(cols * sizeof(int));
result[i] = (int *)malloc(cols * sizeof(int));
}
// Input elements of first matrix
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
// Input elements of second matrix
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
// Call the function to add matrices
addMatrices(rows, cols, A, B, result);
// Output the result
printf("Resulting Matrix (A + B):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
// Free dynamically allocated memory
for (int i = 0; i < rows; i++) {
free(A[i]);
free(B[i]);
free(result[i]);
}
free(A);
free(B);
free(result);
return 0;
}
Enter number of rows and columns: 2 3
Enter elements of Matrix A:
1 2 3
4 5 6
Enter elements of Matrix B:
7 8 9
10 11 12
Resulting Matrix (A + B):
8 10 12
14 16 18
In conclusion, understanding how to add two matrices in C enhances programming skills and provides insight into more complex mathematical operations. It explores advanced topics such as matrix multiplication and determinants. By mastering matrix addition, programmers can effectively handle multi-dimensional data structures and apply mathematical concepts to real-world problems.
The basic operations include addition, subtraction, multiplication, and finding the transpose of a matrix. Each operation typically involves nested loops to iterate through the elements.
Dynamic memory allocation is commonly used to create matrices of variable sizes. You can allocate memory using malloc and ensure to free it after use to avoid memory leaks:
free(matrix[i]); // Free each row
free(matrix); // Free the main pointer
The basic operations include:
Source: NxtWave (CCBP.in)
Original URL: https://www.ccbp.in/blog/articles/addition-of-two-matrix-in-c