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

Sparse Matrix. Simple Transpose.c

1. The document defines functions to read a matrix from input, build a sparse matrix representation from the dense matrix, and calculate the transpose of the sparse matrix. 2. It takes a dense matrix as input, converts it to a sparse representation storing only non-zero elements, and calculates the transpose of the sparse matrix. 3. The main function gets the matrix dimensions, calls the functions to read the dense matrix, build the sparse representation, calculate the transpose, and print out the original and transposed sparse matrices.

Uploaded by

Mohit Awasthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Sparse Matrix. Simple Transpose.c

1. The document defines functions to read a matrix from input, build a sparse matrix representation from the dense matrix, and calculate the transpose of the sparse matrix. 2. It takes a dense matrix as input, converts it to a sparse representation storing only non-zero elements, and calculates the transpose of the sparse matrix. 3. The main function gets the matrix dimensions, calls the functions to read the dense matrix, build the sparse representation, calculate the transpose, and print out the original and transposed sparse matrices.

Uploaded by

Mohit Awasthi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include<stdio.

h>
2 #define max_cols 100
3 #define max_term 100
4 typedef struct {
5 int col;
6
int
row;
7 int val;
8 }term;
9 void read_matrix(int mat[][max_term],int rows,int cols);
10
void
build_a(
int
mat[][max_term],
int
rows,
int
cols,term a[]);
11 void transpose(term a[],term b[]);
12 int main()
13 {
14
int
i,j,rows,cols;
15 int mat[max_term][max_term];
16 term a[max_term],b[max_term];
17 printf("Enter rows & cols");
18 scanf("%d%d",&rows,&cols);
19
20 read_matrix(mat,rows,cols);
21 build_a(mat,rows,cols,a);
22 transpose(a,b);
23
24 printf("Sparse matrix is:\n");
25 for(j=0;j<=a[0].val;j++){
26 printf(" %d %d %d ",a[j].row,a[j].col,a[j].val);
27 printf("\n");}
28
29 printf("Sparse matrix transpose is:\n");
30
for
(j=0;j<=b[0].val;j++){
31 printf(" %d %d %d ",b[j].row,b[j].col,b[j].val);
32 printf("\n");}}
33
34
void
read_matrix(
int
mat[][max_term],
int
rows,
int
cols)
35 {
36 int i,j;
37 printf("Enter mat:");
38
for
(i=0;i<rows;i++){
39 for(j=0;j<cols;j++)
40 scanf("%d",&mat[i][j]);}
41 }
42
void
build_a(
int
mat[][max_term],
int
rows,
int
cols,term a[])
43 {
44 int i,j,base=1;
45 a[0].row=rows;
46 a[0].col=cols;
47 a[0].val=0;
48 for(i=0;i<rows;i++){
49 for(j=0;j<cols;j++)
50
if
(mat[i][j]!=0){
51 a[0].val++;
52 a[base].row=i;
53 a[base].col=j;
54 a[base].val=mat[i][j];
55 base++;
56 }}
57 }
58
void
transpose(term a[],term b[])
59 {
60 int i,j,n,current_b;
61 n=a[0].val;
62 b[0].row=a[0].col;
63 b[0].col=a[0].row;
64 b[0].val=a[0].val;
65 if(n>0)
66 {
67 current_b=1;
68 for(i=0;i<a[0].col;i++)
69
for
(j=0;j<=n;j++)
70 if(a[j].col==i)
71 {
72 b[current_b].row=a[j].col;
73 b[current_b].col=a[j].row;
74 b[current_b].val=a[j].val;
75 current_b++;
76 // printf("{%d},{%d}",a[j].col,i);
77 }
78 }
79 }
80

You might also like