-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.cpp
More file actions
53 lines (49 loc) · 1.53 KB
/
Copy pathGroup.cpp
File metadata and controls
53 lines (49 loc) · 1.53 KB
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
#include "Group.h"
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
using namespace fre;
namespace fre{
vector<int> Group::generateRandomIndices(int n) {
vector<int> indices(stockList.size());
iota(indices.begin(), indices.end(), 0);
shuffle(indices.begin(), indices.end(), std::mt19937{ std::random_device{}() });
indices.resize(n);
return indices;
}
void Group:: addStocksFromIndices(const vector<int>& indices, Group& group){
for(int i = 0; i < indices.size(); i++){
group.setStock(i,getStock(indices[i]));
}
}
void Group::addStock(const Stock& stock) {
stockList.push_back(stock);
N++;
}
Group Group:: randomSelect(int n){
if (n > N) {
throw std::invalid_argument("n must be smaller or equal to the number of elements in stockList");
}
Group rs(n);
vector<int> randomIndices = generateRandomIndices(n);
addStocksFromIndices(randomIndices,rs);
return rs;
}
void Group::display(){
for (unsigned int n = 0; n < getN(); n++){
getStock(n).display();
}
}
void Group::display(ofstream& foutput) {
foutput << "ticker,dayZero" << endl;
for (unsigned int n = 0; n < getN(); n++) {
Stock temp = getStock(n);
foutput << temp.getTicker() << "," << temp.getZero() << std::endl;
}
}
}