-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array Implementation.cpp
65 lines (53 loc) · 1.45 KB
/
Array Implementation.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
#include <iostream>
#include <vector>
using namespace std;
struct Element {
int row;
int col;
int value;
};
class SparseMatrix {
private:
int numRows;
int numCols;
vector<Element> elements;
public:
SparseMatrix(int rows, int cols) {
numRows = rows;
numCols = cols;
}
void addElement(int row, int col, int value) {
if (row < 0 || row >= numRows || col < 0 || col >= numCols) {
cerr << "Invalid row or column index." << endl;
return;
}
Element element;
element.row = row;
element.col = col;
element.value = value;
elements.push_back(element);
}
void printMatrix() {
int currentElement = 0;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (currentElement < elements.size() && elements[currentElement].row == i && elements[currentElement].col == j) {
cout << elements[currentElement].value << " ";
currentElement++;
} else {
cout << "0 ";
}
}
cout << endl;
}
}
};
int main() {
SparseMatrix sparseMatrix(4, 5);
sparseMatrix.addElement(0, 2, 30);
sparseMatrix.addElement(0, 3, 4);
sparseMatrix.addElement(1, 2, 570);
sparseMatrix.addElement(2, 1, 26);
sparseMatrix.printMatrix();
return 0;
}