Skip to content

Commit c72e4ae

Browse files
committed
Create Sparse Matrix.cpp
1 parent 11a4cb2 commit c72e4ae

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

Sparse Matrix.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
struct Node {
6+
int row;
7+
int col;
8+
int data;
9+
};
10+
11+
struct Head {
12+
int row; // Total rows
13+
int col; // Total columns
14+
int numNodes;
15+
Node* nodes;
16+
};
17+
18+
bool isSparse(int** mat, int rows, int columns) {
19+
int zeroCounter = 0;
20+
21+
for (int i = 0; i < rows; i++) {
22+
for (int j = 0; j < columns; j++) {
23+
if (mat[i][j] == 0) {
24+
zeroCounter++;
25+
}
26+
}
27+
}
28+
29+
if (zeroCounter >= (rows * columns) / 2) {
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
Head toSparse(int** mat, int rows, int columns) {
36+
if (isSparse(mat, rows, columns)) {
37+
Head newHead;
38+
newHead.row = rows;
39+
newHead.col = columns;
40+
newHead.numNodes = 0;
41+
42+
int Counter = 0;
43+
for (int i = 0; i < rows; i++) {
44+
for (int j = 0; j < columns; j++) {
45+
if (mat[i][j] != 0) {
46+
Counter++;
47+
}
48+
}
49+
}
50+
51+
newHead.nodes = new Node[Counter];
52+
int tempIndex = 0;
53+
for (int i = 0; i < rows; i++) {
54+
for (int j = 0; j < columns; j++) {
55+
if (mat[i][j] != 0) {
56+
newHead.nodes[tempIndex].row = i;
57+
newHead.nodes[tempIndex].col = j;
58+
newHead.nodes[tempIndex].data = mat[i][j];
59+
newHead.numNodes++;
60+
tempIndex++;
61+
}
62+
}
63+
}
64+
return newHead;
65+
} else {
66+
Head newHead;
67+
newHead.row = rows;
68+
newHead.col = columns;
69+
newHead.numNodes = 0;
70+
newHead.nodes = NULL;
71+
return newHead; // Not Sparse
72+
}
73+
}
74+
75+
void display(int** mat, int rows, int columns) {
76+
for (int i = 0; i < rows; i++) {
77+
for (int j = 0; j < columns; j++) {
78+
cout << mat[i][j] << " ";
79+
}
80+
cout << endl;
81+
}
82+
}
83+
84+
void sparseDisplay(Head sparseMatrix) {
85+
cout << "Sparse Matrix:" << endl;
86+
cout << "\tRow\tColumn\tData" << endl;
87+
for (int i = 0; i < sparseMatrix.numNodes; i++) {
88+
cout << "\t" << sparseMatrix.nodes[i].row << "\t" << sparseMatrix.nodes[i].col << "\t" << sparseMatrix.nodes[i].data << endl;
89+
}
90+
}
91+
92+
int main() {
93+
int rows , columns ;
94+
cout << "Enter row & columns : " ;
95+
cin >> rows >> columns ;
96+
int** mat = new int*[rows];
97+
for (int i = 0; i < rows; i++) {
98+
mat[i] = new int[columns];
99+
for (int j = 0 ; j < columns ; j++){
100+
int tempNum ;
101+
cout << "Enter element of [" << i << "][" << j << "] : " ;
102+
cin >> tempNum ;
103+
mat[i][j] = tempNum ;
104+
}
105+
}
106+
107+
display(mat , rows , columns) ;3
108+
109+
Head sparseMatrix = toSparse(mat, rows, columns);
110+
111+
sparseDisplay(sparseMatrix);
112+
113+
return 0;
114+
}
115+
116+
/*
117+
Reza Asadi (Github : RezaGooner)
118+
1402/08/22 ~ 2023/11/13
119+
*/

0 commit comments

Comments
 (0)