Wednesday, 18 May 2016

Write a C++ program to create a „MATRIX‟ class of size m×n. Overload the „+‟ operator to add two Matrix objects. Write a main function to implement it.

#include<iostream.h>
#include<conio.h>

class MATRIX{

public:
int m,n;
int val[10][10];
int i,j;
void print();
MATRIX operator+(const MATRIX);

};

MATRIX MATRIX:: operator+(const MATRIX mat1){
int i,j;
MATRIX rmat;
rmat.m=mat1.m;
rmat.n=mat1.n;
for(i=0;i<mat1.m;i++)
for(j=0;j<mat1.n;j++)
rmat.val[i][j]=mat1.val[i][j]+this->val[i][j];

return rmat;
}
void MATRIX::print(){
int i,j;
for(i=0;i<m;i++){
 for(j=0;j<n;j++)
cout<<val[i][j]<<"\t";
cout<<endl;
}
}
void main(){
int m,n;
int i,j;
MATRIX mat1,mat2,rmat;
clrscr();
cout<<"Enter The value of row and col : ";
cin>>m>>n;
mat1.m=m;
mat1.n=n;
mat2.m=m;
mat2.n=n;
cout<<"Enter the value of Matrix 1 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat1.val[i][j];
cout<<"Enter the value of Matrix 2 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat2.val[i][j];

rmat=mat1+mat2;
cout<<"\nSum Of Matrix is \n";
rmat.print();
getch();
}

No comments:

Post a Comment