Here you will get C++ program to find addition of two matrices.
For example:
Matrix A:
3 7
2 5
Matrix B:
5 6
1 4
Addition of A and B is:
8 13
3 9
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#include<iostream> #include<process.h> using namespace std; int main() { int A[10][10],B[10][10],c[10][10]; int i,j,m,n,p,q; cout<<"Enter no. of rows and cloumns of matrix A:"; cin>>m>>n; cout<<"\nEnter no. of rows and columns of matrix B:"; cin>>p>>q; if(m==p&&n==q) cout<<"\n\nMatrices can be Added"; else { cout<<"\n\nMatrices can not Added"; exit(0); } cout<<"\nEnter matrix A row wise:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>A[i][j]; } cout<<"\nMatrix A:\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout<<A[i][j]<<" "; cout<<"\n"; } cout<<"\nEnter Matrix B row wise:"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>B[i][j]; } cout<<"\n\nMatrix B:\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout<<B[i][j]<<" "; cout<<"\n"; } for(i=0;i<m;i++) { for(j=0;j<n;j++) c[i][j]=A[i][j]+B[i][j]; } cout<<"\nSum of Matrices A and B:\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout<<c[i][j]<<" "; cout<<"\n"; } return 0; } |
Output
Enter no. of rows and cloumns of matrix A:2
2
Enter no. of rows and columns of matrix B:2
2
Matrices can be Added
Enter matrix A row wise:4 5
3 7
Matrix A:
4 5
3 7
Enter Matrix B row wise:2 1
8 2
Matrix B:
2 1
8 2
Sum of Matrices A and B:
6 6
11 9