forked from sachinsingh3232/Hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_add.cpp
155 lines (136 loc) · 3.19 KB
/
sparse_add.cpp
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
#include <iostream>
using namespace std;
void sort(int arr[][3], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 1; j < size - i - 1; j++)
{
if (arr[j][0] > arr[j + 1][0])
{
swap(arr[j][0], arr[j + 1][0]);
swap(arr[j][1], arr[j + 1][1]);
swap(arr[j][2], arr[j + 1][2]);
}
else if (arr[j][0]==arr[j+1][0] && arr[j][1]>arr[j+1][1])
{
swap(arr[j][0], arr[j + 1][0]);
swap(arr[j][1], arr[j + 1][1]);
swap(arr[j][2], arr[j + 1][2]);
}
}
}
}
void del(int arr[][3], int size)
{
for (int i = 1; i < size - 1; i++)
{
arr[i][0] = arr[i + 1][0];
arr[i][1] = arr[i + 1][1];
arr[i][2] = arr[i + 1][2];
}
size--;
}
int main()
{
int rows, cols, n1, n2;
cout << "Enter no of rows for both matrices: ";
cin >> rows;
cout << "Enter no of cols for both matrices: ";
cin >> cols;
cout << "Enter no of non-zero values in matrix 1: ";
cin >> n1;
cout << "Enter no of non-zero values in matrix 2: ";
cin >> n2;
int sparse1[n1+1][3], sparse2[n2+1][3];
sparse1[0][0] = sparse2[0][0] = rows;
sparse1[0][1] = sparse2[0][1] = cols;
sparse1[0][2] = n1;
sparse2[0][2] = n2;
string arr[3] = {"row", "col", "value"};
// input
cout << "\nFor matrix 1:" << endl;
for (int i = 1; i < n1 + 1; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "Enter " << arr[j] << ": ";
cin >> sparse1[i][j];
}
}
cout << "\nFor matrix 2:" << endl;
for (int i = 1; i < n2 + 1; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "Enter " << arr[j] << ": ";
cin >> sparse2[i][j];
}
}
sort(sparse1, n1+1);
sort(sparse2, n2+1);
//add
int counter=0, add[n1+n2+1][3], i=1, j=1, k=1;
while(i<=n1+1 && j<=n2+1){
if(sparse1[i][0]<sparse2[j][0]){
add[k][0] = sparse1[i][0];
add[k][1] = sparse1[i][1];
add[k][2] = sparse1[i][2];
k++;
i++;
counter++;
}
else if(sparse1[i][0]>sparse2[j][0]){
add[k][0] = sparse2[j][0];
add[k][1] = sparse2[j][1];
add[k][2] = sparse2[j][2];
k++;
j++;
counter++;
}
else if(sparse1[i][1]<sparse2[j][1]){
add[k][0] = sparse1[i][0];
add[k][1] = sparse1[i][1];
add[k][2] = sparse1[i][2];
k++;
i++;
counter++;
}
else if(sparse1[i][1]>sparse2[j][1]){
add[k][0] = sparse2[j][0];
add[k][1] = sparse2[j][1];
add[k][2] = sparse2[j][2];
k++;
j++;
counter++;
}
else{
add[k][0] = sparse1[i][0];
add[k][1] = sparse1[i][1];
add[k][2] = sparse1[i][2]+sparse2[j][2];
k++;
i++;
j++;
counter++;
}
}
add[0][0] = rows;
add[0][1] = cols;
add[0][2] = counter;
//display
cout << "\nResultant:" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (i == add[1][0] && j == add[1][1])
{
cout << add[1][2] << '\t';
del(add, counter+1);
}
else
cout << 0 << '\t';
}
cout << endl;
}
}