Addition of Matrix in C
The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article, we can frequently speak about the direct sum of matrices and the element-clever addition of matrices. There are many one-of-a-kind approaches to feature matrices. Matrices may be multiplied, delivered, or subtracted. In this section, our number one consciousness might be how matrix addition works.
The alternative of lattices is an interest in including associated additives as a minimum framework. Only matrices of equal size are covered within the matrix addition definition.
What are Matrices Delivered Together?
An operation on matrices called the addition of matrices includes the corresponding factors or greater matrices. Matrices can best be delivered if they're equal in length or have equal order or size. A square array of numbers, expressions, symbols, and facts is called a matrix. Rows and columns are used. A matrix is stated to have the size "m n" if the variety of horizontal rows is "m" and the variety of vertical columns are "n."
Definition of the Addition of Matrices One of the essential operations on matrices is the addition of matrices. Including the factors of the corresponding matrices, or greater matrices of the equal order may be delivered to every different. The following is the result of including matrices A and B if A = [aij] and B = [bij] are matrices with equal size, that is, the equal variety of rows and columns: A+B = [aij] + [bij] = [aij + bij].
Here is a C program that findsmatrix addition. Matrix Addition in C | Two Matrix Addition in C | C Program to Add Two Matrices | Two Matrix Addition in C Considermatrices A = [aij] and B = [bij] as m n. The mn-matrix with the (i,j)th element is the sum of A and B. It is represented by A + B. That is, A plus B equals [aij + bij]. Two matrices of the same size are summed by adding an element at each position.
NOTE: Matrix addition requires that both matrices have the same size. The sum of two matrices is defined only if both have the same number of rows and columns, so you cannot add matrices of different sizes.
Example 1:
#include <stdio.h>
int main()
{
// matrix A and B
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
// declare matix C
int c[2][2];
// add both matrix A and B
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
// add & store to matrix C
c[i][j] = a[i][j] + b[i][j];
}
}
// display matrix
printf("Resultant Matrix: \n");
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n"); // new line
}
return 0;
}
Output:
Resultant Matrix:
6 8
Example 2:
#include <stdio.h>
// function to add two matrix
void addMatrix(int a[10][10], int b[10][10],
int c[10][10], int row, int column)
{
for(int i=0; i< row; ++i) {
for(int j=0; j< column; ++j) {
// add & store to matrix C
c[i][j] = a[i][j] + b[i][j]; } } }
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
for (int i = 0; i< row; ++i)
{
for (int j = 0; j < column; ++j)
{
scanf("%d", &matrix[i][j]); } } }
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
for (int i = 0; i< row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", matrix[i][j]); {
printf("\n"); // new line
}
}
// main function
int main()
{
// declare matrix matrix A, B, & C
int a[10][10]; // first matrix
int b[10][10]; // second matrix
int c[10][10]; // resultant matrix
// read the size of matrices
int row, column;
printf("Enter Row and Column Sizes: ");
scanf("%d %d", &row, &column);
// read matrix A and B
printf("Enter Matrix-1 Elements: \n");
readMatrix(a, row, column);
printf("Enter Matrix-2 Elements: \n");
readMatrix(b, row, column);
// add both matrix A and B
addMatrix(a, b, c, row, column);
// display resultant matrix
printf("Resultant Matrix: \n");
displayMatrix(c, row, column);
return 0;
}
Output:
Enter Row and Column Sizes: 2 2
Enter Matrix-1 Elements:
1 3
7 5
Enter Matrix-2 Elements:
6 8
4 2
Resultant Matrix:
7 11
11 7