×

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

Related Topics

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.