Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 101
- typedef struct{
- int col;
- int row;
- int val;
- }term;
- term a[MAX], b[MAX];
- int matrix[MAX][MAX], transposeMatrix[MAX][MAX];
- void transpose()
- {
- b[0].row=a[0].col;
- b[0].col=a[0].row;
- b[0].val=a[0].val;
- int i=0, indexB=0;
- while(i<a[0].col)
- {
- for(int j=1;j<=a[0].val;j++)
- {
- if(a[j].col==i)
- {
- b[++indexB].row=a[j].col;
- b[indexB].col=a[j].row;
- b[indexB].val=a[j].val;
- }
- }
- i++;
- }
- }
- int main()
- {
- printf("Enter the number of rows and columns of the matrix: ");
- scanf("%d%d",&a[0].row,&a[0].col);
- printf("\nEnter the %d elements of the matrix: ",a[0].row*a[0].col);
- for(int i=0;i<a[0].row;i++)
- {
- for(int j=0;j<a[0].col;j++)
- scanf("%d",&matrix[i][j]);
- }
- printf("\nThe entered sparse matrix\n");
- int k=0;
- a[0].val=0;
- for(int i=0;i<a[0].row;i++)
- {
- for(int j=0;j<a[0].col;j++)
- {
- if(matrix[i][j]!=0)
- {
- a[++k].row=i;
- a[k].col=j;
- a[k].val=matrix[i][j];
- a[0].val++;
- }
- printf("%d\t",matrix[i][j]);
- }
- printf("\n");
- }
- if(a[0].val==0)
- {
- printf("\nThe entered sparse matrix has no non-zero elements");
- exit(0);
- }
- printf("\nThe entered sparse matrix in triplet form is\n");
- printf("index\trow\tcol\tval\n");
- for(int i=0;i<=(a[0].val);i++)
- {
- printf("a[%d]\t%d\t%d\t%d",i,a[i].row,a[i].col,a[i].val);
- printf("\n");
- }
- transpose();
- printf("\nThe transpose of the sparse matrix in triplet form is\n");
- printf("index\trow\tcol\tval\n");
- for(int i=0;i<=b[0].val;i++)
- {
- printf("b[%d]\t%d\t%d\t%d",i,b[i].row,b[i].col,b[i].val);
- printf("\n");
- }
- for(int i=0;i<b[0].row;i++)
- {
- for(int j=0;j<b[0].col;j++)
- transposeMatrix[i][i]=0;
- }
- for(int i=1;i<=b[0].val;i++)
- transposeMatrix[b[i].row][b[i].col]=b[i].val;
- printf("\nThe transpose of the entered sparse matrix is\n");
- for(int i=0;i<b[0].row;i++)
- {
- for(int j=0;j<b[0].col;j++)
- printf("%d\t",transposeMatrix[i][j]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement