-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparsematrix.c
80 lines (72 loc) · 2.13 KB
/
sparsematrix.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
#include <stdio.h>
#include <stdlib.h>
struct SparseMatrix
{
int row;
int col;
int value;
};
void addSparseMatrices(struct SparseMatrix matrix1[], struct SparseMatrix matrix2[], int size1, int size2)
{
int maxSize = size1 + size2;
struct SparseMatrix result[maxSize];
int i = 0, j = 0, k = 0, l = 0;
while (i < size1 && j < size2)
{
if (matrix1[i].row < matrix2[j].row ||
(matrix1[i].row == matrix2[j].row && matrix1[i].col < matrix2[j].col))
{
result[k++] = matrix1[i++];
}
else if (matrix1[i].row > matrix2[j].row ||
(matrix1[i].row == matrix2[j].row && matrix1[i].col > matrix2[j].col))
{
result[k++] = matrix2[j++];
}
else
{
result[k].row = matrix1[i].row;
result[k].col = matrix1[i].col;
result[k].value = matrix1[i].value + matrix2[j].value;
k++;
i++;
j++;
}
}
while (i < size1)
{
result[k++] = matrix1[i++];
}
while (j < size2)
{
result[k++] = matrix2[j++];
}
printf("Resulting Sparse Matrix:\n");
for (l = 0; l < k; l++)
{
printf("(%d, %d, %d)\n", result[l].row, result[l].col, result[l].value);
}
}
int main()
{
int size1, size2;
int i;
printf("Enter the size of the first sparse matrix: ");
scanf("%d", &size1);
struct SparseMatrix matrix1[size1];
printf("Enter the elements of the first sparse matrix (row col value):\n");
for (i = 0; i < size1; i++)
{
scanf("%d %d %d", &matrix1[i].row, &matrix1[i].col, &matrix1[i].value);
}
printf("Enter the size of the second sparse matrix: ");
scanf("%d", &size2);
struct SparseMatrix matrix2[size2];
printf("Enter the elements of the second sparse matrix (row col value):\n");
for (i = 0; i < size2; i++)
{
scanf("%d %d %d", &matrix2[i].row, &matrix2[i].col, &matrix2[i].value);
}
addSparseMatrices(matrix1, matrix2, size1, size2);
return 0;
}