Add two matrix in C++ by R4R Team

Matrix:
-Multidimensional Array
-Matrix consist of the rows and columns.

Syntax-
array[row][column]
//Matrix with numbers of rows and columns

Addition of two matrix:
Condition: order of both matrix must be same

Example-
a[2,3]={1,2,3,4,5,6}
b[2,3]={1,1,1,1,1,1}
Sum={2,3,4,5,6,7}

program:

#include< iostream>
int main()
{
int a[10][10],b[10][10],r1,c1,r2,c2,i,j;
cout<<"Enter rows and columns for Matrix_1"<< endl;
cin>>r1>>c1;
//input in matrix_1
cout<<"Enter "<< r1*c1<<" number in matrix_1"<< endl;
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++){
cin>>a[i][j];
}
}
cout<<"Enter rows and columns for Matrix_2"<< endl;
cin>>r2>>c2;
//input in matrix_2
cout<<"Enter "<< r2*c2<<" number in matrix_2"<< endl);
for(i=0;i< r2;i++)
{
for(j=0;j< c2;j++){
cin>>b[i][j];
}
}
//Add two matrix
if(r1==r2 && c1==c2)
{
cout<<"Result is:"<< endl;
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
cou<< a[i][j]+b[i][j]<<" ";
}
cout<< endl;
}
}
else
cout<<"Addition of these two matrix is not possible";
}


output-

Enter rows and columns in matrix_1
2 2
Enter 4 number in matrix_1
1 2 3 4
Enter rows and columns in matrix_2
2 2
Enter 4 number in matrix_2
4 3 2 1
Result
5 5
5 5

Enter rows and columns in matrix_1
2 2
Enter 4 number in matrix_1
1 2 3 4
Enter rows and columns in matrix_2
3 3
Enter 9 number in matrix_2
1 2 3 4 5 6 7 8 9
Addition of these two matrix is not possible




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!