-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparse-matrix.cpp
48 lines (46 loc) · 1.17 KB
/
sparse-matrix.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
/*
Linear Representation Of Sparse Matrix
*/
#include <iostream>
#define ROWS 20
#define COLS 20
using namespace std;
int main() {
int row, col, k = 0, counter = 0;
int matrix[ROWS][COLS];
cout << "Enter Size Of Rows And Colums : ";
cin >> row >> col;
cout << "Enter Matrix Elements : \n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> matrix[i][j];
if (matrix[i][j] != 0)
counter++;
}
}
if (counter <= (row * col) / 2) {
cout << "Given Matrix Is Not Sparse!!";
sleep(1);
exit(0);
}
int denseMatrix[2][counter];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] != 0) {
denseMatrix[0][k] = i;
denseMatrix[1][k] = j;
denseMatrix[2][k] = matrix[i][j];
k++;
}
}
}
cout << "Linear Representation Of Sparse Matrix : \n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < k; j++) {
cout << denseMatrix[i][j] << " ";
}
cout << endl;
}
getch();
return 0;
}