#include<iostream>
using namespace std;
template<class T>
class sparse
{
T **mat;
int s_size;
T **sp;
int m,n;
public:
void readmatrix();
sparse<T> createsparse(sparse<T>);
sparse<T> add(sparse<T>,sparse<T>);
sparse<T> transpose(sparse<T>);
void display(sparse<T>);
};
template<class T>
void sparse<T> :: readmatrix()
{
int i,j;
cout<<"\n enter no of rows and columns in the matrix ";
cin>>m>>n;
mat=new T *[m];
for(i=0;i<m;i++)
mat[i]=new T [n];
cout<<"\n enter elements of original matrix ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat[i][j];
}
template<class T>
sparse<T> sparse<T> :: createsparse(sparse<T> ob)
{
int i,j;
sparse<T> s;
s.m=ob.m;
s.n=ob.n;
s.s_size=0;
for(i=0;i<ob.m;i++)
for(j=0;j<ob.n;j++)
if(ob.mat[i][j]!=0)
s.s_size++;
s.s_size=s.s_size+1;
s.sp=new T *[3];
for(i=0;i<3;i++)
s.sp[i]=new T [s.s_size];
s.sp[0][0]=ob.m;
s.sp[1][0]=ob.n;
s.sp[2][0]=0;
int k=1;
for(i=0;i<ob.m;i++)
for(j=0;j<ob.n;j++)
if(ob.mat[i][j]!=0)
{
s.sp[0][k]=i;
s.sp[1][k]=j;
s.sp[2][k]=ob.mat[i][j];
k++;
}
return s;
}
template<class T>
void sparse<T>::display(sparse<T> ob)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<ob.s_size;j++)
cout<<" "<<ob.sp[i][j]<<" ";
cout<<"\n";
}
}
template<class T>
sparse<T> sparse<T>::add(sparse<T> ob1,sparse<T> ob2)
{
sparse<T> s;
s.m=ob1.m;
s.n=ob2.n;
int i=1,j=1;
s.s_size=0;
while(i<=ob1.s_size && j<=ob2.s_size)
{
if(ob1.sp[0][i]==ob2.sp[0][j])
{
if(ob1.sp[1][i]==ob2.sp[1][j])
{
s.s_size++;
i++;
j++;
}
else
{
if(ob1.sp[1][i]<ob2.sp[1][j])
i++;
else
j++;
}
}
else
{
if(ob1.sp[0][i]<ob2.sp[0][j])
i++;
else
j++;
}
}
int c=(ob1.s_size+ob2.s_size-s.s_size);
s.sp=new T *[3];
for(int a=0;a<3;a++)
s.sp[a]=new T [c];
s.s_size=c;
int x=1;
int y=1;
int k=1;
s.sp[0][0]=s.m;
s.sp[1][0]=s.n;
s.sp[2][0]=0;
while(x<=ob1.s_size && y<=ob2.s_size)
{
if(ob1.sp[0][x]==ob2.sp[0][y])
{
if(ob1.sp[1][x]==ob2.sp[1][y])
{
s.sp[0][k]=ob1.sp[0][x];
s.sp[1][k]=ob1.sp[1][x];
s.sp[2][k]=ob1.sp[2][x]+ob2.sp[2][y];
x++;
y++;
k++;
}
else
if(ob1.sp[1][x]<ob2.sp[1][y])
{
s.sp[0][k]=ob1.sp[0][x];
s.sp[1][k]=ob1.sp[1][x];
s.sp[2][k]=ob1.sp[2][x];
x++;
k++;
}
else
{
s.sp[0][k]=ob2.sp[0][y];
s.sp[1][k]=ob2.sp[1][y];
s.sp[2][k]=ob2.sp[2][y];
y++;
k++;
}
}
else
{
if(ob1.sp[0][x]<ob2.sp[0][y])
{
s.sp[0][k]=ob1.sp[0][x];
s.sp[1][k]=ob1.sp[1][x];
s.sp[2][k]=ob1.sp[2][x];
x++;
k++;
}
else
{
s.sp[0][k]=ob2.sp[0][y];
s.sp[1][k]=ob2.sp[0][y];
s.sp[2][k]=ob2.sp[0][y];
y++;
k++;
}
}
}
while(x<=ob1.s_size)
{
s.sp[0][k]=ob1.sp[0][x];
s.sp[1][k]=ob1.sp[1][x];
s.sp[2][k]=ob1.sp[2][x];
x++;
k++;
}
while(y<=ob2.s_size)
{
s.sp[0][k]=ob2.sp[0][y];
s.sp[1][k]=ob2.sp[0][y];
s.sp[2][k]=ob2.sp[0][y];
y++;
k++;
}
return s;
}
template<class T>
sparse<T> sparse<T> :: transpose(sparse<T> ob)
{
T temp;
int i,j;
for(i=0;i<ob.s_size;i++)
{
temp=ob.sp[0][i];
ob.sp[0][i]=ob.sp[1][i];
ob.sp[1][i]=temp;
}
for(i=1;i<ob.s_size;i++)
{
int j=i+1;
while(ob.sp[0][j]<ob.sp[0][j-1] && j>=1)
{
T ele;
ele=ob.sp[0][j];
ob.sp[0][j]=ob.sp[0][j-1];
ob.sp[0][j-1]=ele;
ele=ob.sp[1][j];
ob.sp[1][j]=ob.sp[1][j-1];
ob.sp[1][j-1]=ele;
ele=ob.sp[2][j];
ob.sp[2][j]=ob.sp[2][j-1];
ob.sp[2][j-1]=ele;
j--;
}
}
}
int main()
{
sparse<int> obj;
obj.readmatrix();
obj=obj.createsparse(obj);
obj.display(obj);
sparse<int> obj1;
obj1.readmatrix();
obj1=obj1.createsparse(obj1);
obj1.display(obj1);
cout<<"\n addition sparse matrix is \n";
sparse<int> obj2;
obj2=obj2.add(obj,obj1);
obj2.display(obj2);
sparse<int> obj3;
obj3.readmatrix();
obj3=obj3.createsparse(obj3);
obj3.display(obj3);
obj3=obj3.transpose(obj3);
cout<<"\n transpose of the matrix \n";
obj3.display(obj3);
}
No comments:
Post a Comment