-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsparseadd.c
167 lines (150 loc) · 4.02 KB
/
sparseadd.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<stdlib.h>
void display(int **a)
{
int t=1;
printf("\n");
for(int i=0;i<a[0][0];i++)
{
for(int j=0;j<a[0][1];j++)
{
if((t<=a[0][2])&&(i==a[t][0])&&(j==a[t][1]))
{
printf("%d ",a[t][2]);
t++;
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
printf("\n");
}
int compare(int a,int b)
{
if(a>b)
return -1;
else if(a<b)
return 1;
else
return 0;
}
int **add(int **a,int **b)
{
int **c=NULL;
int n1=a[0][2],n2=b[0][2];
int i=1,j=1,k=1;
if((a[0][0]!=b[0][0])||(a[0][1]!=b[0][1]))
return c;
c=(int **)malloc((n1+n2+1)*sizeof(int *));
for(i=0;i<(n1+n2+1);i++)
{
c[i]=(int *)malloc(3*sizeof(int));
}
c[0][0]=a[0][0];
c[0][1]=a[0][1];
i=1,j=1;
int m1,m2;
while((i<=n1)&&(j<=n2))
{
m1=compare(a[i][0],b[j][0]);
switch(m1)
{
case 0:m2=compare(a[i][1],b[j][1]);
switch(m2)
{
case 0:c[k][0]=a[i][0];
c[k][1]=a[i][1];
c[k][2]=a[i][2]+b[j][2];
i++;k++;j++;
break;
case 1:c[k][0]=a[i][0];
c[k][1]=a[i][1];
c[k][2]=a[i][2];
i++;k++;
break;
case -1:c[k][0]=b[j][0];
c[k][1]=b[j][1];
c[k][2]=b[j][2];
k++;j++;
break;
}
break;
case 1:c[k][0]=a[i][0];
c[k][1]=a[i][1];
c[k][2]=a[i][2];
i++;k++;
break;
case -1:c[k][0]=b[j][0];
c[k][1]=b[j][1];
c[k][2]=b[j][2];
j++;k++;
break;
}
}
if(i<=n1)
{
while(i<=n1)
{
c[k][0]=a[i][0];
c[k][1]=a[i][1];
c[k][2]=a[i][2];
k++;i++;
}
}
else
{
while(j<=n2)
{
c[k][0]=b[j][0];
c[k][1]=b[j][1];
c[k][2]=b[j][2];
k++;j++;
}
}
c[0][2]=k-1;
c=realloc(c,k*3*sizeof(int));
return c;
}
int main()
{
int r1,c1,n1,r2,c2,n2;
int **a,**b,**c;
register int i;
printf("Enter the Row and Column and no elements in Matrix 1\n");
scanf("%d%d%d",&r1,&c1,&n1);
printf("Enter the Row and Column and no elements in Matrix 2\n");
scanf("%d%d%d",&r2,&c2,&n2);
a=(int **)malloc((n1+1)*sizeof(int *));
for(i=0;i<(n1+1);i++)
{
a[i]=(int *)malloc(3*sizeof(int));
}
b=(int **)malloc((n2+1)*sizeof(int *));
for(i=0;i<(n2+1);i++)
{
b[i]=(int *)malloc(3*sizeof(int));
}
a[0][0]=r1;a[0][1]=c1;a[0][2]=n1;
b[0][0]=r2;b[0][1]=c2;b[0][2]=n2;
printf("\nEnter the row no, column no and the element for matrix 1\n");
for(i=1;i<=n1;i++)
{
scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
}
display(a);
printf("\nEnter the row no,column no and the element for matrix 2\n");
for(i=1;i<=n2;i++)
{
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
display(b);
c=add(a,b);
if(c==NULL)
printf("Matrix cant be added");
else
display(c);
return 0;
}