forked from cwhidden/rspr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_matrix.cpp
58 lines (50 loc) · 957 Bytes
/
fill_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
49
50
51
52
53
54
55
56
57
58
// fill an upper right triangular matrix symmetrically
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
int main() {
vector<vector<int> > m = vector<vector<int> >();
string line = "";
int size = 0;
while(getline(cin, line)) {
string token = "";
m.push_back(vector<int>());
for(int i = 0; i < line.size(); i++) {
if (line[i] == ',') {
int num = -1;
if (token != "") {
num = atoi(token.c_str());
}
m[size].push_back(num);
token = "";
}
else {
token.push_back(line[i]);
}
}
int num = -1;
if (token != "") {
num = atoi(token.c_str());
}
m[size].push_back(num);
size++;
}
for(int i = 0; i < m.size(); i++) {
for(int j = i+1; j < m.size(); j++) {
m[j][i] = m[i][j];
}
}
for(int i = 0; i < m.size(); i++) {
cout << m[i][0];
for(int j = 1; j < m.size(); j++) {
cout << ",";
if (m[i][j] >= 0) {
cout << m[i][j];
}
}
cout << endl;
}
}