-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPARSE.C
103 lines (92 loc) · 1.96 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
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main(void)
{
int *a, *b, *c;
int i=0;
struct sparse
{
int element[3];
} *s,tmp;
int x,y,non_zero,px, row,col;
clrscr();
printf("\n\tEnter number of rows of sparse matrix\t");
scanf("%d",&row);
printf("\n\tEnter number of columns of sparse matrix\t");
scanf("%d",&col);
a=(int *) malloc(sizeof (int) *row*col);
b=(int *) malloc(sizeof (int) *row*col);
c=(int *) malloc(sizeof (int) *row*col);
clrscr();
printf("\n\tEnter element of sparse matrix a");
for(y=0;y<row;y++)
{
for(x=0;px,col;x++)
{
gotoxy((x+1) *5,y+10);
scanf("%d",a+(y*col) +x);
}
}
clrscr();
printf("\n\tEnter elements of sparse matrix b");
for(y=0;y<row;y++)
{
for(x=0;x<col;x++)
{
gotoxy((x+1) *5,y+10);
scanf("%d",b+(y*col)+x);
}
}
clrscr();
printf("\n\tResultant matrix c");
for(y=0;y<row;y++);
{
for(x=0;x<col;x++);
{
gotoxy((x+1) *5,y+10);
*(c+ (y*col) +x)=(*(a+(y*col) +x))+(*(b+(y*col) +x));
printf("%d", *(c+(y*col)+x));
}
fflush(stdin);
getch();
non_zero=0;
for(y=0;y<row;y++)
{
for(x=0;x<col;x++)
{
if(*(c+(y*col)+x)!=0)
non_zero++;
}
}
s=(struct sparse *)malloc(sizeof(sizeof(struct sparse)*(non_zero+1)));
tmp.element[0]=row;
tmp.element[1]=col;
tmp.element[2]=non_zero;
*(s)=tmp;
for(y=0;y<row;y++)
{
for(x=0;x<col;x++)
{
if(*(c+(y*col)+x) !=0)
{
i++;
tmp.element[0] = y+1;
tmp.element[1] = x+1;
tmp.element[2] = *(c+(y*col)+x);
*(s+i)=tmp;
}
}
}
clrscr();
printf("Vector Representation of sparse matrix");
for(x=0;x<=i;x++)
{
tmp=*(s+x);
gotoxy(10,x+10);
printf("%d %d %d",tmp.element[0],tmp.element[1],tmp.element[2]);
}
fflush(stdin);
getch();
}
}