# C Program for Finding Transpose of a Sparse Matrix

Here you will get C program to discover transpose of a meagre matrix.

Transpose of a matrix is gotten by exchanging lines and segments. In another manner, we can

say that component in the I, j position gets put in the j, I position. Transpose of the matrix B1 is gotten as B2 by embeddings (i,j)th

component of B1 as (j, i)th component in B2.

Program

``````#include<stdio.h>

#define MAX 20

void printsparse(int[]);
void transpose(int[],int[]);

int main()
{
int b1[MAX],b2[MAX],m,n;
printf("Enter the size of matrix (rows,columns):");

scanf("%d%d",&m,&n);
b1=m;
b1=n;

transpose(b1,b2);
printsparse(b2);
}

{
int i,t;
printf("\nEnter no. of non-zero elements:");
scanf("%d",&t);
b=t;

for(i=1;i<=t;i++)
{
printf("\nEnter the next triple(row,column,value):");
scanf("%d%d%d",&b[i],&b[i],&b[i]);
}
}

void printsparse(int b[MAX])
{
int i,n;
n=b;	//no of 3-triples

printf("\nAfter Transpose:\n");

printf("\nrow\t\tcolumn\t\tvalue\n");
for(i=0;i<=n;i++)
printf("%d\t\t%d\t\t%d\n",b[i],b[i],b[i]);
}

void transpose(int b1[],int b2[])
{
int i,j,k,n;
b2=b1;
b2=b1;
b2=b1;

k=1;
n=b1;

for(i=0;i<b1;i++)
for(j=1;j<=n;j++)
//if a column number of current triple==i then insert the current triple in b2
if(i==b1[j])
{
b2[k]=i;
b2[k]=b1[j];
b2[k]=b1[j];
k++;
}
}``````

Output

``````Enter the size of matrix (rows,columns):3 4

Enter no. of non-zero elements:4

Enter the next triple(row,column,value):1 0 5

Enter the next triple(row,column,value):1 2 3

Enter the next triple(row,column,value):2 1 1

Enter the next triple(row,column,value):2 3 2

After Transpose:

row column value
4 3 4
0 1 5
1 2 1
2 1 3
3 2 2``````