×

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

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 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.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

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.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.