I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout<<"Enter number of rows (between 1 and 100): ";
cin>>r;
cout<<"Enter number of columns (between 1 and 100): ";
cin>>c;
cout<<"\nEnter elements of First matrix:\n";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
cout<<"Enter elements of Second matrix:\n";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>b[i][j];
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
cout<<"\nSum of two matrix is: \n\n";
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout<<"\t"<<sum[i][j];
if(j==c-1)
{
cout<<"\n\n";
}
}
return 0;
}
I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
Result:
Enter number of rows (between 1 and 100): 3
Enter number of columns (between 1 and 100): 2
Enter elements of First matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a21: 3
Enter element a22: 4
Enter element a31: 5
Enter element a32: 6
Enter elements of Second matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a21: 3
Enter element a22: 4
Enter element a31: 5
Enter element a32: 6
Sum of two matrix is:
2 4
6 8
10 12
need an explanation for this answer? contact us directly to get an explanation for this answer