forked from SarahN18/Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparseMatrix.cpp
More file actions
40 lines (39 loc) · 911 Bytes
/
sparseMatrix.cpp
File metadata and controls
40 lines (39 loc) · 911 Bytes
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
//Sparse matrix to Compact matrix
#include<iostream>
#include<iomanip>
// #define R 6
// #define C 8
using namespace std;
int main(){
int R, C;
cin >> R >> C;
int sparse[R][C];
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
cin >> sparse[i][j];
}
}
int compact[R*C][3];
compact[0][0] = R;
compact[0][1] = C;
int k = 1;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
if(sparse[i][j]!=0){
compact[k][0] = i+1;
compact[k][1] = j+1;
compact[k][2] = sparse[i][j];
k++;
}
}
}
compact[0][2] = k - 1;
cout << "\n\n";
cout << "The compact Matrix ->" << endl;
for(int i = 0; i < k; i++){
for (int j = 0; j < 3; j++){
cout << setw(6) << compact[i][j];
}
cout << endl;
}
}