0% found this document useful (0 votes)
122 views3 pages

Sparse Matrix Transpose

This document defines a C++ class for sparse matrices and includes methods to read in a sparse matrix, transpose it, and display it. The smatrix class defines a 2D array to store the non-zero elements, rows, columns and number of non-zeros. It contains methods to read in the matrix, transpose it by iterating through columns and swapping row/column indices, and display the matrix. The main function demonstrates reading in a matrix, transposing it by calling the transpose method, and displaying the original and transposed matrices.

Uploaded by

vishak unni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views3 pages

Sparse Matrix Transpose

This document defines a C++ class for sparse matrices and includes methods to read in a sparse matrix, transpose it, and display it. The smatrix class defines a 2D array to store the non-zero elements, rows, columns and number of non-zeros. It contains methods to read in the matrix, transpose it by iterating through columns and swapping row/column indices, and display the matrix. The main function demonstrates reading in a matrix, transposing it by calling the transpose method, and displaying the original and transposed matrices.

Uploaded by

vishak unni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//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;

[Link]=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();

[Link]();

cout<<"Matrix 1\n";

[Link]();

ob3=[Link]();

cout<<"Matrix 3\n";

[Link]();

getch();

return 0;

GG

You might also like