#include<iostream.h> #include<conio.h> #include<stdlib.h> class mat { int a[3][3],b[3][3],c[3][3],i,j,k; public: void add(); void mul(); }; void mat::add() { cout<<"\nenter elements of a"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>a[i][j]; } } cout<<"\nenter elements of b"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>b[i][j]; } } cout<<"\naddition of a,bis:\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<"\t"<<c[i][j]; } cout<<"\n"; } } void mat::mul() { cout<<"\nenter elements of a"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>a[i][j]; } } cout<<"\nenter elements of b"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>b[i][j]; } } cout<<"c="; for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } cout<<"\nresultant matrix is:\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<"\t"<<c[i][j]; } cout<<"\n"; } } void main() { clrscr(); mat m; m.add(); m.mul(); getch(); }
Sunday, 7 December 2014
C++ program to print addition and multiplication of two matrices
Posted by Madan
Posted on 14:37