Addition of two matrix in C
In this post, you will learn different ways to perform matrix addition using the C programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop, it uses matrices to process linear transformations to render images. In robotics and automation, matrices are the fundamental segments for robot development. The contributions for controlling robots are acquired depending on the calculations from matrices.
The different operations on the matrix are also very important to us. There are different ways to perform matrix addition in C.
Addition of matrix in C using nested for loop
A nested loop is a loop inside a loop. The following C program uses a nested for loop to iterate through each row and each column. In each iteration, we add the corresponding elements from the two matrices and store them in the result.
#include<stdio.h>
int main()
{
int rows, cols, x, y, matrix1[10][10], matrix2[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix: \n");
scanf("%d%d", &rows, &cols);
printf("Enter the elements of first matrix: \n");
for (x = 0; x < rows; x++)
for (y = 0; y < cols; y++)
scanf("%d", &matrix1[x][y]);
printf("Enter the elements of second matrix: \n");
for (x = 0; x < rows; x++)
for (y = 0 ; y < cols; y++)
scanf("%d", &matrix2[x][y]);
printf("Sum of entered matrices:-\n");
for (x = 0; x < rows; x++) {
for (y = 0 ; y < cols; y++) {
sum[x][y] = matrix1[x][y] + matrix2[x][y];
printf("%d\t", sum[x][y]);
}
printf("\n");
}
return 0;
}
Output of the above code:
Enter the number of rows and columns of matrix:
3 3
Enter the elements of first matrix:
5 9 2
8 5 2
9 6 3
Enter the elements of second matrix:
8 2 7
7 4 2
5 2 5
Sum of entered matrices:-
13 11 9
15 9 4
14 8 8
Addition of matrix in C using function
Here, we create a function 'matrix_addition()' to perform the addition of two matrices.
#include <stdio.h>
#define n 4
// define function to add matrix1[][] and matrix2[][]
// and store the result in result[][]
void matrix_addition(int matrix1[][n], int matrix2[][n], int result[][n])
{
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
int main()
{
int matrix1[n][n] = { {5, 3, 2, 6},
{3, 1, 5, 2},
{3, 2, 4, 1},
{5, 4, 3, 2}};
int matrix2[n][n] = { {2, 4, 1, 5},
{5, 3, 2, 2},
{3, 1, 2, 3},
{2, 3, 4, 6}};
int result[n][n];
int i, j;
matrix_addition(matrix1, matrix2, result);
printf("Result matrix: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf("%d ", result[i][j]);
printf("\n");
}
return 0;
}
Output of the above code:
Result matrix:
7 7 3 11
8 4 7 4
6 3 6 4
7 7 7 8
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative