# Addition of two matrices using pointer

While performing the addition of two matrices using pointer both matrices have the same row and column sizes keep to remember.

So firstly we recall,

## What is Matrix?

A matrix is a 2D array which is used to store and display data in a grid format. For example:

The above illustrates a matrix where a number for example 10 is denoted by arr, the first one denotes the row number and the second one denotes the column number.

### What is a matrix addition?

When two matrices are added of the same type and stored in a resultant matrix it is known as matrix addition. Two matrices can be added if they have the same dimension. In the program we will follow the technique shown below:

res=arr+arr1;

it illustrated as the number in the first row and the first column of the first matrix will add to the number in the first row and first column of the second matrix and will be stored in the resultant matrix.

### Complete Algorithm Of Multiplication:

• We take three matrices int a as the first matrix int b as the second matrix and the resultant matrix as c.
• We take loop variables as i and j, to print the resultant matrix.
• We take the input of the two matrices in the matrix input function, where they are stored and their addresses are stored in the pointer variable. This operation is direct performed in the scanf(“%d”, (*(mat + row) + col)) function.
• Then the three matrices are sent to the matrix sum function (the third matrix is sent to store the result).
• The addition operation takes place with the help of the pointers illustrated as *(*(res + i) + j) =(*(*(mat1 + i) + j))+(*(*(mat2 + i) + j)), this states that each variable of both the matrices are added with the help of their memory address and the sum value is generated whose address is stored in the resultant matrix.
• In the end, we print the resultant matrix stored in c, with the help of addresses i.e., the pointer.
• Hence we complete the program of matrix addition with the pointer.

Above algorithm implementation:

```#include<stdio.h>
int main()
{
int a,b,c;
int i,j;
printf("\nEnter First matrix for 3X3 matrix");
matrixInput(a);

printf("\nEnter Second matrix for 3X3 matrix");
matrixInput(b);

matrixsum(a,b,c);

printf("Product of entered matrices :-\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",*(*(c + i) + j));
printf("\n");
}
return 0;
}

void matrixInput(int mat)
{
int row, col;
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}

void matrixsum(int mat1, int mat2, int res)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*(*(res + i) + j) =(*(*(mat1 + i) + j))+(*(*(mat2 + i) + j));
}
}
}```

Summary:

Here in the above program, we have taken a 3X3 matrix to show the example of matrix addition.

The function matrixInput takes the input of the matrix one by one using the loop when row=0 and column=0 it takes the input of the 1st variable of the row and then the value of the column is incremented where row=0 and column=1 taking the input of the 2nd variable of the row and then at last row=0 column=2 taking the input of the 3rd variable of the row.

The row gets incremented now where row=1 and column=0, it takes the input of the 1st variable of the row and then the value of the column is incremented where row=1 and column=1 taking the input of the 2nd variable of the row and then at last row=1 column=2 taking the input of the 3rd variable of the row.

Then the row gets incremented now where row=2 and column=0, it takes the input of the 1st variable of the row and then the value of the column is incremented where row=2 and column=1 taking the input of the 2nd variable of the row and then at last row=2 column=2 taking the input of the 3rd variable of the row.

The whole input operation uses a pointer where the address of the values is stored in the pointer variable.

Then the values are sent to the matrix sum function and the addition operation is carried out, where the values of each row and column are added to the same row and column of the second matrix such as res=mat1+mat2 and so on.

This addition operation is carried out by the help of pointer where the addition takes place and the address is stored of the value i.e., *(*(res + i) + j) =(*(*(mat1 + i) + j))+(*(*(mat2 + i) + j)) and the resultant address of the added variables are stored in the res matrix.

Hence this is how the addition takes place.

Later in the main function, the resultant matrix c is printed to get the output of the addition.

Output: ##### Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40