#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int m,n,p,q;
class mat
{
int mt[10][10];
public: void add(mat m1,mat m2);
void sub(mat m1,mat m2);
void mul(mat m1,mat m2);
void transpose();
void read_matrix();
void display_matrix();
};
void mat::add(mat m1,mat m2)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mt[i][j]=m1.mt[i][j]+m2.mt[i][j];
}
void mat::sub(mat m1,mat m2)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mt[i][j]=m1.mt[i][j]-m2.mt[i][j];
}
void mat::mul(mat m1,mat m2)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
mt[i][j]=0;
for(int k=0;k<p;k++)
mt[i][j]=mt[i][j]+m1.mt[i][k]*m2.mt[k][j];
}
}
}
void mat::transpose()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<"\t\t"<<mt[j][i];
}
cout<<"\n";
}
}
void mat::read_matrix()
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>mt[i][j];
}
void mat::display_matrix()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<"\t\t"<<mt[i][j];
}
cout<<"\n\n";
}
}
void main()
{
mat m1,m2,msum,msub,mmul,mtrans;
int ch;
clrscr();
cout<<"\nmatrix operations\n";
cout<<"\nenter your choice\n";
cout<<"\n1.addition\n2.subtraction\n3.multiplication\n4.transpose\n5.exit\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nmatrix addition\n";
cout<<"\nenter the row & column values for 1st matrix:";
cin>>m>>n;
cout<<"\nenter the row &column values for 2nd matrix:";
cin>>p>>q;
if((m!=p)||(n!=q))
{
cout<<"\nmatric addition is not possible\n";
getch();
exit(0);
}
cout<<"\nenter the elements for 1st matrix\n";
m1.read_matrix();
cout<<"enter the elementes for 2nd matrix\n";
m2.read_matrix();
cout<<"the 1st matrix is\n";
m1.display_matrix();
cout<<"the 2nd matrix is \n";
m2.display_matrix();
cout<<"\naddition of two matrices\n";
msum.add(m1,m2);
msum.display_matrix();
break;
case 2: cout<<"\nmatrix subtraction \n:";
cout<<"\nenter the row & column values for 1st matrix:";
cin>>m>>n;
cout<<"\nenter the row &column values for 2nd matrix:";
cin>>p>>q;
if((m!=p)||(n!=q))
{
cout<<"\nmatrix subtractioin not possible\n";
getch();
exit(0);
}
cout<<"\nenter the elements for 1st matrix\n";
m1.read_matrix();
cout<<"\nenter the elements for 2nd matrix\n";
m2.read_matrix();
cout<<"the 1st matrix is\n";
m1.display_matrix();
cout<<"the 2nd matrix is \n";
m2.display_matrix();
cout<<"\nsubtraction of two matrices is \n";
msub.sub(m1,m2);
msub.display_matrix();
break;
case 3: cout<<"\nmatrix multiplication\n";
cout<<"\nenter the row & column values for 1st matrix:";
cin>>m>>n;
cout<<"\nenter the row &column values for 2nd matrix:";
cin>>p>>q;
if(n!=p)
{
cout<<"\nmatrix multiplication not possible\n";
getch();
exit(0);
}
cout<<"\nenter the elements for 1st matrix\n";
m1.read_matrix();
cout<<"\nenter the elements for 2nd matrix\n";
int tm=m;
m=p;
n=q;
m2.read_matrix();
m=tm;
cout<<"the 1st matrix is\n";
m1.display_matrix();
cout<<"the 2nd matrix is \n";
m2.display_matrix();
cout<<"\nmultiplication of two matrices is \n";
mmul.mul(m1,m2);
n=q;
mmul.display_matrix();
break;
case 4: cout<<"\nmatrix transpose\n";
cout<<"\nenter the row & column values for 1st matrix:";
cin>>m>>n;
cout<<"\nenter the row &column values for 2nd matrix:";
cin>>p>>q;
cout<<"\nenter the elements for matrix\n";
mtrans.read_matrix();
cout<<"\nthe entered matrix is\n";
mtrans.display_matrix();
cout<<"\nafter transposing matrix is \n";
mtrans.transpose();
break;
default:exit(0);
}
getch();
}
No comments:
Post a Comment