-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparseaddition3.c
175 lines (153 loc) · 4.97 KB
/
sparseaddition3.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
168
169
170
171
172
173
174
175
#include <stdio.h>
int main() {
int A[10][10], B[10][10];
int TUPLE1[100][3], TUPLE2[100][3], SUM[100][3];
int sparseSum[10][10] = {0}; // To store the sparse matrix after addition
int r, c, k1 = 1, k2 = 1, count1 = 0, count2 = 0;
// Input for Sparse Matrix A
printf("Enter the number of rows and columns for the sparse matrices: ");
scanf("%d %d", &r, &c);
printf("Enter elements for Sparse Matrix A:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &A[i][j]);
}
}
// Input for Sparse Matrix B
printf("Enter elements for Sparse Matrix B:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
scanf("%d", &B[i][j]);
}
}
// Convert Sparse Matrix A to Tuple Matrix TUPLE1
int i = 0, j = 0;
while (i < r) {
while (j < c) {
if (A[i][j] != 0) {
TUPLE1[k1][0] = i; // Row
TUPLE1[k1][1] = j; // Column
TUPLE1[k1][2] = A[i][j]; // Value
k1++;
count1++;
}
j++;
}
j = 0;
i++;
}
// Set the size and count for TUPLE1
TUPLE1[0][0] = r;
TUPLE1[0][1] = c;
TUPLE1[0][2] = count1;
// Convert Sparse Matrix B to Tuple Matrix TUPLE2
i = 0; j = 0;
while (i < r) {
while (j < c) {
if (B[i][j] != 0) {
TUPLE2[k2][0] = i; // Row
TUPLE2[k2][1] = j; // Column
TUPLE2[k2][2] = B[i][j]; // Value
k2++;
count2++;
}
j++;
}
j = 0;
i++;
}
// Set the size and count for TUPLE2
TUPLE2[0][0] = r;
TUPLE2[0][1] = c;
TUPLE2[0][2] = count2;
// Display TUPLE1
printf("\nTuple Representation of Sparse Matrix A (TUPLE1):\n");
for (int i = 0; i <= count1; i++) {
printf("%d %d %d\n", TUPLE1[i][0], TUPLE1[i][1], TUPLE1[i][2]);
}
// Display TUPLE2
printf("\nTuple Representation of Sparse Matrix B (TUPLE2):\n");
for (int i = 0; i <= count2; i++) {
printf("%d %d %d\n", TUPLE2[i][0], TUPLE2[i][1], TUPLE2[i][2]);
}
// Sum Tuple Matrices
int ptr = 1;
i = 1; j = 1;
// Start adding tuples based on the cases you mentioned
while (i <= count1 && j <= count2) {
// Case 1: TUPLE1[i][0] < TUPLE2[j][0]
if (TUPLE1[i][0] < TUPLE2[j][0]) {
SUM[ptr][0] = TUPLE1[i][0];
SUM[ptr][1] = TUPLE1[i][1];
SUM[ptr][2] = TUPLE1[i][2];
i++;
// Case 2: TUPLE1[i][0] > TUPLE2[j][0]
} else if (TUPLE1[i][0] > TUPLE2[j][0]) {
SUM[ptr][0] = TUPLE2[j][0];
SUM[ptr][1] = TUPLE2[j][1];
SUM[ptr][2] = TUPLE2[j][2];
j++;
// Case 3: TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] == TUPLE2[j][1]
} else if (TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] == TUPLE2[j][1]) {
SUM[ptr][0] = TUPLE1[i][0];
SUM[ptr][1] = TUPLE1[i][1];
SUM[ptr][2] = TUPLE1[i][2] + TUPLE2[j][2]; // Add values
i++;
j++;
// Case 4: TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] < TUPLE2[j][1]
} else if (TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] < TUPLE2[j][1]) {
SUM[ptr][0] = TUPLE1[i][0];
SUM[ptr][1] = TUPLE1[i][1];
SUM[ptr][2] = TUPLE1[i][2];
i++;
// Case 5: TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] > TUPLE2[j][1]
} else if (TUPLE1[i][0] == TUPLE2[j][0] && TUPLE1[i][1] > TUPLE2[j][1]) {
SUM[ptr][0] = TUPLE2[j][0];
SUM[ptr][1] = TUPLE2[j][1];
SUM[ptr][2] = TUPLE2[j][2];
j++;
}
ptr++;
}
// Case 6a: Add remaining terms from TUPLE1
while (i <= count1) {
SUM[ptr][0] = TUPLE1[i][0];
SUM[ptr][1] = TUPLE1[i][1];
SUM[ptr][2] = TUPLE1[i][2];
i++;
ptr++;
}
// Case 6b: Add remaining terms from TUPLE2
while (j <= count2) {
SUM[ptr][0] = TUPLE2[j][0];
SUM[ptr][1] = TUPLE2[j][1];
SUM[ptr][2] = TUPLE2[j][2];
j++;
ptr++;
}
// Set size and count for SUM
SUM[0][0] = r;
SUM[0][1] = c;
SUM[0][2] = ptr - 1;
// Display SUM Tuple Matrix
printf("\nResultant SUM Tuple Matrix:\n");
for (int i = 0; i < ptr; i++) {
printf("%d %d %d\n", SUM[i][0], SUM[i][1], SUM[i][2]);
}
// Convert SUM Tuple Matrix back to Sparse Matrix
for (int i = 1; i <= SUM[0][2]; i++) {
int row = SUM[i][0];
int col = SUM[i][1];
int value = SUM[i][2];
sparseSum[row][col] = value;
}
// Display the Sparse Matrix obtained from the SUM tuple
printf("\nSparse Matrix obtained from the SUM Tuple Matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", sparseSum[i][j]);
}
printf("\n");
}
return 0;
}