-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse.c
126 lines (120 loc) · 2.46 KB
/
sparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<stdio.h>
#include<stdlib.h>
typedef struct element
{
int i;
int j;
int x;
}elem;
typedef struct sparse
{
int m;
int n;
int num;
elem *e;
}sparse;
void create(sparse *s)
{
int i;
printf("Enter the dimension\n");
scanf("%d %d",&s->m,&s->n);
printf("The toal number of non zero element\n");
scanf("%d",&s->num);
s->e=(elem*)malloc(sizeof(elem)*s->num);
printf("Enter all elements\n");
for(i=0;i<s->num;i++)
{
scanf("%d %d %d",&s->e[i].i,&s->e[i].j,&s->e[i].x);
}
}
//void transpose(sparse *s)
//{
//int i,j,k=0;
// for(i=0;i<s->m;i++)
// {
// for(j=0;j<s->n;j++)
// {
// if(s->e[k].i==i &&s->e[k].j==j)
// {
// printf("%d ",s.e[k++].x);
//}
//else
//printf("0 ");
//}
// printf("\n");
//}
//}//
//}
void trans(sparse *s)
{
int temp;
elem t,p;
int j;
for(int i=0;i<s->num;i++)
{
temp=s->e[i].i;
s->e[i].i=s->e[i].j;
s->e[i].j=temp;
}
for(int j=0;j<s->num-1;j++)
{
if(s->e[j].i>s->e[j+1].i)
{
t=s->e[j];
s->e[j]=s->e[j+1];
s->e[j+1]=t;
}
else if(s->e[j].i==s->e[j+1].i)
{
if(s->e[j].j>s->e[j+1].j)
{
p=s->e[j];
s->e[j]=s->e[j+1];
s->e[j+1]=p;
}
}
}
}
void display(sparse s)
{
int i,j,k=0;
for(i=0;i<s.m;i++)
{
for(j=0;j<s.n;j++)
{
if(s.e[k].i==i &&s.e[k].j==j)
{
printf("%d ",s.e[k++].x);
}
else
printf("0 ");
}
printf("\n");
}
}
void display1(sparse s)
{
int i,j,k=0;
for(i=0;i<s.n;i++)
{
for(j=0;j<s.m;j++)
{
if(s.e[k].i==i &&s.e[k].j==j)
{
printf("%d ",s.e[k++].x);
}
else
printf("0 ");
}
printf("\n");
}
}
void main()
{
sparse s;
create(&s);
display(s);
trans(&s);
printf("The transpose is:\n");
display1(s);
}