C++ Program for Addition of Two Matrices using 2d Array

C++ program for addition of two matrices: In this program, we are going to make a C++ program for the addition of two matrices.
In this program, we are going to make a C++ program for the addition of two matrices. To make this program, we initialize three matrices A, B & c then we insert elements of matrices A & B by the user using nested for loops. At last, we add the corresponding elements of matrices A & B and then we store those values in another matrix c and display all elements of matrices c.

Related post: Subtraction of two matrices in c++

C++ Program for Addition of Two Matrices using 2d Array

In this program, we are going to perform the sum of elements in a 2d array in c++.
#include<iostream.h> 
using namespace std;
void main()
{
int A[3][3],B[3][3],c[3][3],i,j;
int R,C;
cout<<"\n Enter Number of rows and columns respectively:";
cin>>R>>C;
cout<<"\n Enter elements of Matrix A:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Matrix A:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
cout<<A[i][j];
cout<<"\t";
}
cout<<"\n";
}
cout<<"\n Enter elements of Matrix B:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
cin>>B[i][j];
}
}
cout<<"\n Matrix B:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
cout<<B[i][j];
cout<<"\t";
}
cout<<"\n";
}
cout<<"\n Sum of Matrix A & B:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
c[i][j]=A[i][j]+B[i][j];
cout<<c[i][j];
cout<<"\t";
}
cout<<"\n";
}
return 0;
}
Output:
C++ program for addition of two matrices

Approach to make a C++ program to Perform Sum of elements in a 2D array:

  • First, we initialize three matrices A, B & c using 2D arrays.
  • We take the number of rows and columns of both matrices A & B.
  • We insert elements of matrices A by the user using nested for loops.
  • We print elements of matrices A on the output screen using nested for loop.
  • We insert elements of matrices B by the user using nested for loops.
  • We print elements of matrices B on the output screen using nested for loop.
  • Using nested for loop, we add the corresponding elements of matrices A & B and stores these values in matrix c.
  • We print all the elements of matrix c which contains the sum of corresponding elements of matrices A & B.

Nested for loop statement used in this program:

for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
statement1;
statement2;
}
}
Where R= Number of rows of matrices A & B
C= Number of columns of matrices A & B
 

FAQ on Sum of elements in the 2D array in C++

How do you add two matrices in C++?
Using nested for loop, we add the corresponding elements of two matrices and store these values in another matrix. 

How do you add two matrices?
We simply add the corresponding elements of two matrices
How do you display a matrix in C++?
We display a matrix using statement:
cout<<"\n Matrix A:\n";
for(i=1;i<=R;i++)
{
for(j=1;j<=C;j++)
{
cout<<A[i][j];
cout<<"\t";
}
cout<<"\n";
}
Where R= Number of rows of matrices A & B
C= Number of columns of matrices A & B

What is the value of the identity matrix?

It is a square matrix, whose all the diagonal elements are 1 and all the non-diagonal elements are 0.
For example, An identity matrix of order 3*3
1 0 0
0 1 0
0 0 1
How do you multiply matrices in C++?
Coming soon, stay connected.

Programs You may like:
Factorial program in c++
Menu Driven C++ Program for a Simple Calculator

Post a Comment

Please do not enter any spam link in the comment box.