You are on page 1of 3

//Sparse Matrix Transpose

#include<iostream.h>

#include<conio.h>

#include<process.h>

class smatrix

int m[20][3],r,c,nz;

public:

void read();

void display();

smatrix transpose();

};

void smatrix:: read()

{ cout<<"Enter the Total Rows\n";

cin>>r;

cout<<"Enter the Total Cols\n";

cin>>c;

cout<<"Enter the Total Non Zero Elemnts\n";

cin>>nz;

m[0][0]=r;

m[0][1]=c;

m[0][2]=nz;

cout<<"Enter the Elements\n";

for(int i=1;i<=nz;i++)

GG
for(int j=0;j<3;j++)

cin>>m[i][j];

smatrix smatrix :: transpose()

smatrix m3;

int i,j,k,n;

m3.m[0][0]=m[0][1];

m3.m[0][1]=m[0][0];

m3.m[0][2]=m[0][2];

k=1;

m3.nz=m[0][2];

for(i=0;i<c;i++)

for(j=1;j<=nz;j++)

//if a column number of current triple==i then insert the current triple in b2

if(i==m[j][1])

m3.m[k][0]=i;

m3.m[k][1]=m[j][0];

m3.m[k][2]=m[j][2];

k++;

GG
return(m3);

void smatrix:: display()

{ int i,j;

for(i=0;i<=nz;i++)

for(j=0;j<3;j++)

cout<<m[i][j]<<"\t";

cout<<"\n";

int main()

{ smatrix ob1,ob3;

clrscr();

ob1.read();

cout<<"Matrix 1\n";

ob1.display();

ob3=ob1.transpose();

cout<<"Matrix 3\n";

ob3.display();

getch();

return 0;

GG

You might also like