forked from Manupriya-Vayalambron/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparseaddition.c
More file actions
69 lines (53 loc) · 1.74 KB
/
Copy pathsparseaddition.c
File metadata and controls
69 lines (53 loc) · 1.74 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
#include <stdio.h>
#define MAX 100
typedef struct {
int row;
int col;
int value;
} SparseMatrixElement;
void readSparseMatrix(SparseMatrixElement sparse[], int *num) {
printf("Enter number of non-zero elements: ");
scanf("%d", num);
printf("Enter row, column, and value of each non-zero element:\n");
for (int i = 0; i < *num; i++) {
scanf("%d %d %d", &sparse[i].row, &sparse[i].col, &sparse[i].value);
}
}
void printSparseMatrix(SparseMatrixElement sparse[], int num) {
printf("Row\tCol\tValue\n");
for (int i = 0; i < num; i++) {
printf("%d\t%d\t%d\n", sparse[i].row, sparse[i].col, sparse[i].value);
}
}
void addSparseMatrices(SparseMatrixElement a[], int n1, SparseMatrixElement b[], int n2, SparseMatrixElement result[], int *n3) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
if (a[i].row < b[j].row || (a[i].row == b[j].row && a[i].col < b[j].col)) {
result[k++] = a[i++];
} else if (a[i].row > b[j].row || (a[i].row == b[j].row && a[i].col > b[j].col)) {
result[k++] = b[j++];
} else {
result[k] = a[i];
result[k++].value = a[i++].value + b[j++].value;
}
}
while (i < n1) {
result[k++] = a[i++];
}
while (j < n2) {
result[k++] = b[j++];
}
*n3 = k;
}
int main() {
SparseMatrixElement a[MAX], b[MAX], result[MAX];
int n1, n2, n3;
printf("Enter elements of first sparse matrix:\n");
readSparseMatrix(a, &n1);
printf("Enter elements of second sparse matrix:\n");
readSparseMatrix(b, &n2);
addSparseMatrices(a, n1, b, n2, result, &n3);
printf("Resultant sparse matrix after addition:\n");
printSparseMatrix(result, n3);
return 0;
}