#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class matrix
{
private:
int i,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2;
public:
void read();
void add();
void sub();
void mult();
void display();
};
void matrix::read()
{
cout<<"\nenter the order of matrix A ";
cin>>m1>>n1;
cout<<"\nenter the elements of matrix A ";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
//cout<<a[i][j];
}
}
cout<<"\nenter the order of matrix B ";
cin>>m2>>n2;
cout<<"\nenter the matrix B ";
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
cin>>b[i][j];
}
}
}
void matrix::add()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for addition";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
}
void matrix::sub()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for subtraction ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];
//cout<<a[i][j];
}
}
}
}
void matrix::mult()
{
if(n2!=m2)
{
cout<<"Invalid order limit ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(int k=0;k<n1;k++)
{
c[i][k]+=a[i][k]*b[k][j];
}
}
}
}
}
void matrix::display()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cout.width(3);
cout<<c[i][j];
}
cout<<"\n";
}
}
void main()
{
int ch;
matrix m;
clrscr();
while(1)
{
cout<<"\n1. Addition of matrices ";
cout<<"\n2. Subtraction of matrices ";
cout<<"\n3. Multipication of matrices ";
cout<<"\n4. Exit";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
case 1:
m.read();
m.add();
m.display();
break;
case 2:
m.read();
m.sub();
m.display();
break;
case 3:
m.read();
m.mult();
m.display();
break;
case 4:
exit(0);
}
}
}
OUTPUT:
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class matrix
{
private:
int i,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2;
public:
void read();
void add();
void sub();
void mult();
void display();
};
void matrix::read()
{
cout<<"\nenter the order of matrix A ";
cin>>m1>>n1;
cout<<"\nenter the elements of matrix A ";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
//cout<<a[i][j];
}
}
cout<<"\nenter the order of matrix B ";
cin>>m2>>n2;
cout<<"\nenter the matrix B ";
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
cin>>b[i][j];
}
}
}
void matrix::add()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for addition";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
}
void matrix::sub()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for subtraction ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];
//cout<<a[i][j];
}
}
}
}
void matrix::mult()
{
if(n2!=m2)
{
cout<<"Invalid order limit ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(int k=0;k<n1;k++)
{
c[i][k]+=a[i][k]*b[k][j];
}
}
}
}
}
void matrix::display()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cout.width(3);
cout<<c[i][j];
}
cout<<"\n";
}
}
void main()
{
int ch;
matrix m;
clrscr();
while(1)
{
cout<<"\n1. Addition of matrices ";
cout<<"\n2. Subtraction of matrices ";
cout<<"\n3. Multipication of matrices ";
cout<<"\n4. Exit";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
case 1:
m.read();
m.add();
m.display();
break;
case 2:
m.read();
m.sub();
m.display();
break;
case 3:
m.read();
m.mult();
m.display();
break;
case 4:
exit(0);
}
}
}
OUTPUT:
No comments:
Post a Comment