forked from bwplotka/s2-modeling-and-simulations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1.cpp
154 lines (123 loc) · 2.97 KB
/
exercise1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <gmpxx.h>
#include <iostream>
#include <sstream>
#define SUCCESS 0
#define FAILURE 1
#define MAX_ARR_SIZE 16777216
#define MAX_PRECISION 65536
#define LOG_INFO
using namespace std;
/**
* Variance calculation.
*/
mpf_class varVal(const mpf_class* arr, const size_t& size, const size_t& acc,
const mpf_class& meanValueSquare) {
mpf_class sum2(0, acc), length(size, acc), result(0, acc);
mpf_class temp(0, acc);
for (int i=0; i< length; i++) {
temp = arr[i] - meanValueSquare;
sum2 += temp;
}
result = (sum2/length);
return result;
}
/**
* Not yet implemented.
*/
mpf_class periodVal(const mpf_class* arr, size_t size, size_t acc) {
mpf_class result(0, acc);
// TODO: DO task3 INLINE.
return result;
}
/**
* Not used, since we want to use this loop to do some initial calculations.
*/
mpf_class* getArrStream(size_t acc, size_t& size) {
mpf_class* arr = new mpf_class[MAX_ARR_SIZE];
size_t i = 0;
string line;
#ifdef LOG_INFO
cout << "Enter input in one line:" << endl;
#endif
std::getline(cin, line);
stringstream lineStream(line);
mpf_class input_val;
input_val.set_prec(acc);
while (true)
{
if(!(lineStream >> input_val)) break;
arr[i] = input_val;
i++;
}
size = i;
return arr;
}
/**
* Debug only.
*/
void debugPrintArr(const mpf_class* arr, long size) {
cout << "Debug array print:" << endl;
for (long i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
/**
* Print usage.
*/
void printUsage(char **argv) {
cout <<"usage: "<< argv[0] <<" <accuracy>\n";
}
/**
* Main function.
* Current time for test2: 11s
*/
int main (int argc, char **argv) {
// Cin && cout optimization.
std::ios_base::sync_with_stdio(false);
// Max acc: 2^16 = 65536
// Max size of array: 2^24 = 16777216
// Max elem | value |: 2^64 = 18446744073709551616
int accuracy = MAX_PRECISION; // Default accuracy.
if ( argc > 1 ) {
if (!strcmp(argv[1], "-h")) {
printUsage(argv);
return SUCCESS;
}
accuracy = atoi(argv[1]);
if (accuracy < 1 && accuracy > MAX_PRECISION)
return FAILURE;
}
mpf_class meanValue(0, accuracy);
mpf_class meanValueSquare(0, accuracy);
mpf_class sum(0, accuracy);
// Main sequence.
mpf_class* arr = new mpf_class[MAX_ARR_SIZE];
size_t size = 0;
string line;
#ifdef LOG_INFO
cout << "Enter input in one line:" << endl;
#endif
std::getline(cin, line);
stringstream lineStream(line);
mpf_class input_val;
input_val.set_prec(accuracy);
while (true)
{
if(!(lineStream >> input_val)) break;
sum += input_val;
arr[size] = input_val*input_val;
size++;
}
meanValue = sum / size;
meanValueSquare = meanValue * meanValue;
#ifdef LOG_INFO
cout << "Starting prog <size of arr: " << size << ">" << endl;
#endif
cout.precision(accuracy);
cout << meanValue << endl;
cout << varVal(arr, size, accuracy, meanValueSquare) << endl;
// TODO: cout << periodVal << endl;
delete[](arr);
return 0;
}