-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_double.cpp
More file actions
194 lines (140 loc) · 4.41 KB
/
vector_double.cpp
File metadata and controls
194 lines (140 loc) · 4.41 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <ctime>
#include <algorithm>
#include <cmath>
using namespace std;
double arith_avg( vector<double>& lst );
double median(vector<double>& lst);
double graph_avg(vector<double>& lst);
double arith_avg_file( ifstream& fin );
void read_csv_file( ifstream& csv_data, vector<double>& lst );
bool myfunction (double i, double j) { return (i<j); }
main()
{
vector<double> SS;
double ergebnis;
clock_t start = clock();
/* Berechnen und ausgeben. */
/* ifstream myfile1 ("plop.csv");
ergebnis = arith_avg_file(myfile1);
cout << "File : " << ergebnis << endl; */
/* Berechnen und ausgeben. */
ifstream myfile2 ("plop.csv");
if (myfile2.is_open())
{
// Lesen.
read_csv_file(myfile2, SS);
// Sortieren.
sort (SS.begin(), SS.end(), myfunction);
// Mittelwerte berechnen.
// ergebnis = arith_avg(SS);
// ergebnis = median(SS);
ergebnis = graph_avg(SS);
cout << "Vector: " << ergebnis << endl;
}
else cout << "Unable to open file" << endl;
clock_t ends = clock();
cout << "Time elapsed " << (double) (ends - start) / CLOCKS_PER_SEC << endl;
}
/* --------------------------------------------- */
/* Arithmetischen Mittelwert berechnen (Vector). */
/* ----------------------------------------------*/
double arith_avg(vector<double>& lst) {
double avg = 0;
double summe = 0;
// Über das Array iterieren.
// cout << "Loop by index:" << endl;
int ii;
for(ii=0; ii < lst.size(); ii++)
{
//cout << lst[ii] << endl;
summe += lst[ii];
}
avg = ( summe / lst.size() );
return avg;
}
/* --------------------------------------------- */
/* Median berechnen (Vektor). */
/* ----------------------------------------------*/
double median(vector<double>& lst) {
long anzahl = 0;
long index = 0;
double lwert;
double rwert;
double wert;
anzahl = lst.size();
if ( anzahl % 2 ) { // Falls: eine ungerade Anzahl an Elementen.
// Index des Elementes in der Mitte bestimmen.
index = ((anzahl + 1 ) / 2) - 1;
return lst[index];
}
else { // Falls: eine gerade Anzahl an Elementen.
lwert = lst[(anzahl/2)-1]; // Linker Mittelwert.
rwert = lst[(anzahl/2)]; // Rechter Mittelwert.
wert = 0.5 * ( lwert + rwert ); // Die Hälfte davon.
return wert;
}
}
/* --------------------------------------------- */
/* Graphisches Mittel (Vektor). */
/* ----------------------------------------------*/
double graph_avg(vector<double>& lst) {
double produkt = 1;
double anzahl = 0;
anzahl = lst.size();
// Über das Array iterieren.
int ii;
for(ii=0; ii < lst.size(); ii++)
{
cout << lst[ii] << endl;
produkt *= lst[ii];
}
//cout << produkt << " ";
//cout << anzahl << endl;
return pow(produkt, (1/anzahl));
}
/* --------------------------------------------- */
/* Arithmetischen Mittelwert berechnen. */
/* Werte nicht zwischenspeichern. */
/* --------------------------------------------- */
double arith_avg_file( ifstream& csv_data ) {
double summe = 0;
double avg = 0;
double anzahl = 0;
string line;
while(getline(csv_data,line))
{
stringstream lineStream(line);
string cell;
while(getline(lineStream, cell,','))
{
// You have a cell!!!!
//cout << cell << endl;
summe = summe + atof(cell.c_str());
anzahl++;
}
}
avg = summe / anzahl;
return avg;
}
/* --------------------------------------------- */
/* Read CSV-File into Vector. */
/* --------------------------------------------- */
void read_csv_file( ifstream& csv_data, vector<double>& lst ) {
string line;
while(getline(csv_data,line))
{
stringstream lineStream(line);
string cell;
while(getline(lineStream, cell,','))
{
// You have a cell!!!!
//cout << cell << endl;
lst.push_back( atof(cell.c_str()) );
}
}
}