Here you will get C program to find transpose of a sparse matrix.
在这里,您将获得C程序来查找稀疏矩阵的转置。
Transpose of a matrix is obtained by interchanging rows and columns. In another way, we can say that element in the i, j position gets put in the j, i position. Transpose of the matrix B1 is obtained as B2 by inserting (i,j)th element of B1 as (j,i)th element in B2.
矩阵的转置通过交换行和列来获得。 换句话说,我们可以说在i,j位置的元素被放置在j,i位置。 通过将B1的第(i,j)个元素插入B2中的第(j,i)个元素,将矩阵B1的转置作为B2获得。
#include<stdio.h>
#define MAX 20
void printsparse(int[][3]);
void readsparse(int[][3]);
void transpose(int[][3],int[][3]);
int main()
{
int b1[MAX][3],b2[MAX][3],m,n;
printf("Enter the size of matrix (rows,columns):");
scanf("%d%d",&m,&n);
b1[0][0]=m;
b1[0][1]=n;
readsparse(b1);
transpose(b1,b2);
printsparse(b2);
}
void readsparse(int b[MAX][3])
{
int i,t;
printf("\nEnter no. of non-zero elements:");
scanf("%d",&t);
b[0][2]=t;
for(i=1;i<=t;i++)
{
printf("\nEnter the next triple(row,column,value):");
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
}
void printsparse(int b[MAX][3])
{
int i,n;
n=b[0][2]; //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][0],b[i][1],b[i][2]);
}
void transpose(int b1[][3],int b2[][3])
{
int i,j,k,n;
b2[0][0]=b1[0][1];
b2[0][1]=b1[0][0];
b2[0][2]=b1[0][2];
k=1;
n=b1[0][2];
for(i=0;i<b1[0][1];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][1])
{
b2[k][0]=i;
b2[k][1]=b1[j][0];
b2[k][2]=b1[j][2];
k++;
}
}
Output
输出量
Enter the size of matrix (rows,columns):3 4
输入矩阵的大小(行,列):3 4
Enter no. of non-zero elements:4
输入编号 非零元素的数量:4
Enter the next triple(row,column,value):1 0 5
输入下一个三元组(行,列,值):1 0 5
Enter the next triple(row,column,value):1 2 3
输入下一个三元组(行,列,值):1 2 3
Enter the next triple(row,column,value):2 1 1
输入下一个三元组(行,列,值):2 1 1
Enter the next triple(row,column,value):2 3 2
输入下一个三元组(行,列,值):2 3 2
After Transpose:
转置后:
row column value 4 3 4 0 1 5 1 2 1 2 1 3 3 2 2
行列值 4 3 4 0 1 5 1 2 1 2 1 3 3 2 2
翻译自: https://www.thecrazyprogrammer.com/2014/02/c-program-for-finding-transpose-of-a-sparse-matrix.html