-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_matrix.c
More file actions
206 lines (151 loc) · 3.75 KB
/
sparse_matrix.c
File metadata and controls
206 lines (151 loc) · 3.75 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
int main()
{
// first array
int r1, c1, sparse1[30][30], a1[30][30], b1 = 1, count1 = 0;
// Enter row and column of first matrix
printf("Enter the row of array: ");
scanf("%d", &r1);
printf("Enter the column of array: ");
scanf("%d", &c1);
printf("Enter the element of first array: \n");
// Enter element of first array
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
scanf("%d", &a1[i][j]);
}
}
// non-zero element count (first matrix)
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if (a1[i][j] != 0)
{
count1++;
}
}
}
printf("Your first matrix is: \n");
// print first matrix
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
printf("%d ", a1[i][j]);
}
printf("\n");
}
// Your first matrix is sparse or not print
if (count1 <= (r1 * c1) / 2)
{
printf("Sparse matrix..!!\n");
}
else
{
printf("Not sparse matrix..!!\n");
}
sparse1[0][0] = r1;
sparse1[0][1] = c1;
sparse1[0][2] = count1;
// sparse matrix initialize (first matrix)
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
if (a1[i][j] != 0)
{
sparse1[b1][0] = i;
sparse1[b1][1] = j;
sparse1[b1][2] = a1[i][j];
b1++;
}
}
}
// Print the first sparse matrix..
for (int i = 0; i <= count1; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", sparse1[i][j]);
}
printf("\n");
}
printf("------------------------------------------------------------------------------------\n\n");
// Second array
int r2, c2, sparse2[30][30], a2[30][30], b2 = 1, count2 = 0;
// Enter row and column of second matrix
printf("Enter the row of array: ");
scanf("%d", &r2);
printf("Enter the column of array: ");
scanf("%d", &c2);
printf("Enter the element of second array: \n");
// Enter element of second array
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
scanf("%d", &a2[i][j]);
}
}
// non-zero element count (second matrix)
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
if (a2[i][j] != 0)
{
count2++;
}
}
}
printf("Your second matrix is: \n");
// print second matrix
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
printf("%d ", a2[i][j]);
}
printf("\n");
}
// Your second matrix is sparse or not print
if (count2 <= (r2 * c2) / 2)
{
printf("Sparse matrix..!!\n");
}
else
{
printf("Not sparse matrix..!!\n");
}
sparse2[0][0] = r2;
sparse2[0][1] = c2;
sparse2[0][2] = count2;
// second sparse matrix initialize
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
if (a2[i][j] != 0)
{
sparse2[b2][0] = i;
sparse2[b2][1] = j;
sparse2[b2][2] = a2[i][j];
b2++;
}
}
}
// Print the second sparse matrix..
for (int i = 0; i <= count2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", sparse2[i][j]);
}
printf("\n");
}
printf("----------------------------------------------------------------------------\n");
return 0;
}