0% found this document useful (0 votes)
85 views

Sparse Matrix Transpose

This C program takes a sparse matrix as input, stores it in sparse representation as a 3-tuple array, and then transposes the sparse matrix. It first reads in the dimensions and elements of the original matrix, stores the non-zero elements and their indices in a 3-tuple array. It then loops through this array to build the transpose, by iterating through each column of the original and writing the elements to the corresponding rows of the transposed matrix. Finally, it prints out the transposed sparse matrix.

Uploaded by

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

Sparse Matrix Transpose

This C program takes a sparse matrix as input, stores it in sparse representation as a 3-tuple array, and then transposes the sparse matrix. It first reads in the dimensions and elements of the original matrix, stores the non-zero elements and their indices in a 3-tuple array. It then loops through this array to build the transpose, by iterating through each column of the original and writing the elements to the corresponding rows of the transposed matrix. Finally, it prints out the transposed sparse matrix.

Uploaded by

probindas2717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*Sparse Matrix Transpose*/

#include<stdio.h>
void main()
{
int m,p,n,i,j,k,t=0,l,a[10][10],s[10][10],st[10][10];
printf("Enter the size of the orginal array");
scanf("%d%d",&m,&n);
printf("\nEnter the array elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
k=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
s[k][0]=i;
s[k][1]=j;
s[k][2]=a[i][j];
k++;
if(j>t)
t=j;
}
}
}
s[0][0]=m;
s[0][1]=n;
s[0][2]=k-1;
printf("3 tuple representation of the given matrix is\n");
for(i=0;i<k;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",s[i][j]);
}
}
p=1;
l=k-1;
printf("\nt= %d, l= %d",t,l);
for(i=0;i<=t;i++)
{
k=1;
for(j=0;j<l;j++)
{
if(s[k][1]==i)
{
st[p][1]=s[k][0];
st[p][0]=s[k][1];
st[p][2]=s[k][2];
p++;
k++;
}
else
k++;
}
}
st[0][0]=s[0][1];
st[0][1]=s[0][0];
st[0][2]=s[0][2];
printf("\nTranspose of the sparse matrix is\n");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",st[i][j]);
}
}
}

You might also like