assignment_17.cpp
Problem Statement
assignment_17.cpp
WAP to convert the Sparse Matrix into non-zero form and vice-versa.
Source Code
cpp
#include <iostream>
using namespace std;
class SparseMatrix {
private:
int rows;
int cols;
int **standardMatrix;
int **triplet;
int nonZeroCount;
void allocateStandard(int r, int c) {
rows = r;
cols = c;
standardMatrix = new int*[rows];
for (int i = 0; i < rows; i++) {
standardMatrix[i] = new int[cols];
for (int j = 0; j < cols; j++) standardMatrix[i][j] = 0;
}
}
void freeStandard() {
if (standardMatrix != nullptr) {
for (int i = 0; i < rows; i++) delete[] standardMatrix[i];
delete[] standardMatrix;
standardMatrix = nullptr;
}
}
void freeTriplet() {
if (triplet != nullptr) {
for (int i = 0; i <= nonZeroCount; i++) delete[] triplet[i];
delete[] triplet;
triplet = nullptr;
}
}
public:
SparseMatrix() {
rows = 0; cols = 0; nonZeroCount = 0;
standardMatrix = nullptr;
triplet = nullptr;
}
void inputStandardMatrix() {
freeStandard();
freeTriplet();
cout << "Enter number of rows: "; cin >> rows;
cout << "Enter number of columns: "; cin >> cols;
if (rows <= 0 || cols <= 0) { cout << "Invalid matrix dimensions!\n"; rows = cols = 0; return; }
allocateStandard(rows, cols);
cout << "Enter matrix elements (" << rows << " x " << cols << "):\n";
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) cin >> standardMatrix[i][j];
}
void convertToTriplet() {
if (standardMatrix == nullptr) { cout << "No standard matrix available!\n"; return; }
freeTriplet();
nonZeroCount = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (standardMatrix[i][j] != 0) nonZeroCount++;
triplet = new int*[nonZeroCount + 1];
for (int i = 0; i <= nonZeroCount; i++) triplet[i] = new int[3];
triplet[0][0] = rows;
triplet[0][1] = cols;
triplet[0][2] = nonZeroCount;
int k = 1;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (standardMatrix[i][j] != 0) {
triplet[k][0] = i;
triplet[k][1] = j;
triplet[k][2] = standardMatrix[i][j];
k++;
}
}
void inputTripletForm() {
freeStandard();
freeTriplet();
int r, c, count;
cout << "Enter total rows: "; cin >> r;
cout << "Enter total columns: "; cin >> c;
cout << "Enter number of non-zero elements: "; cin >> count;
if (r <= 0 || c <= 0 || count < 0 || count > r * c) { cout << "Invalid input parameters!\n"; return; }
rows = r; cols = c; nonZeroCount = count;
triplet = new int*[nonZeroCount + 1];
for (int i = 0; i <= nonZeroCount; i++) triplet[i] = new int[3];
triplet[0][0] = rows;
triplet[0][1] = cols;
triplet[0][2] = nonZeroCount;
cout << "Enter (row, col, value) for each non-zero element:\n";
for (int i = 1; i <= nonZeroCount; i++) cin >> triplet[i][0] >> triplet[i][1] >> triplet[i][2];
}
void convertToStandard() {
if (triplet == nullptr) { cout << "No 3-tuple representation found!\n"; return; }
freeStandard();
rows = triplet[0][0];
cols = triplet[0][1];
nonZeroCount = triplet[0][2];
allocateStandard(rows, cols);
for (int i = 1; i <= nonZeroCount; i++) {
int r = triplet[i][0];
int c = triplet[i][1];
int val = triplet[i][2];
if (r >= 0 && r < rows && c >= 0 && c < cols) standardMatrix[r][c] = val;
}
}
void displayStandard() {
if (standardMatrix == nullptr) { cout << "Standard matrix is empty!\n"; return; }
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) cout << standardMatrix[i][j] << " ";
cout << endl;
}
}
void displayTriplet() {
if (triplet == nullptr) { cout << "3-Tuple representation is empty!\n"; return; }
for (int i = 0; i <= nonZeroCount; i++)
cout << triplet[i][0] << " " << triplet[i][1] << " " << triplet[i][2] << endl;
}
~SparseMatrix() {
freeStandard();
freeTriplet();
}
};
int main() {
SparseMatrix sm;
int choice;
while (true) {
cout << "1.InputMatrix 2.ToTriplet 3.DisplayTriplet \n4.InputTriplet 5.ToStandard 6.DisplayMatrix -1.Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: sm.inputStandardMatrix(); break;
case 2: sm.convertToTriplet(); break;
case 3: sm.displayTriplet(); break;
case 4: sm.inputTripletForm(); break;
case 5: sm.convertToStandard(); break;
case 6: sm.displayStandard(); break;
case -1: cout << "Exiting...\n"; return 0;
default: cout << "Wrong choice.\n";
}
}
}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
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