ex5 task5
This commit is contained in:
parent
95b3017475
commit
3d054f8ae7
94 changed files with 159884 additions and 6 deletions
93
ex5/code/task_2-5/task_2.cpp
Normal file
93
ex5/code/task_2-5/task_2.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include "task_2.h"
|
||||
#include <cassert> // assert
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <omp.h>
|
||||
|
||||
tuple<double, double> min_max_par(const vector<double> &v) {
|
||||
int min_val = v[0];
|
||||
int max_val = v[0];
|
||||
|
||||
#pragma omp parallel for reduction(min:min_val) reduction(max:max_val)
|
||||
for (size_t i = 0; i < v.size(); ++i) {
|
||||
if (v[i] < min_val) min_val = v[i];
|
||||
if (v[i] > max_val) max_val = v[i];
|
||||
}
|
||||
|
||||
return make_tuple(min_val,max_val);
|
||||
}
|
||||
|
||||
|
||||
tuple<double, double, double> means_par(const vector<double>& v){
|
||||
size_t n = v.size();
|
||||
double sum = 0;
|
||||
double logsum = 0;
|
||||
double invsum = 0;
|
||||
|
||||
#pragma omp parallel for shared(v,n) reduction(+:sum, logsum, invsum)
|
||||
for (size_t i = 0; i<n; ++i){
|
||||
sum += v[i];
|
||||
logsum += log(v[i]);
|
||||
invsum += 1.0/v[i];
|
||||
}
|
||||
|
||||
double arith = sum / static_cast<double>(n);
|
||||
double geo = exp(1.0/static_cast<double>(n) * logsum);
|
||||
double harm = static_cast<double>(n) / invsum;
|
||||
return make_tuple(arith, geo, harm);
|
||||
}
|
||||
|
||||
void fill_vector(istream& istr, vector<double>& v)
|
||||
{
|
||||
double d=0;
|
||||
while ( istr >> d) v.push_back(d); // Einlesen
|
||||
if (!istr.eof())
|
||||
{ // Fehlerbehandlung
|
||||
cout << " Error handling \n";
|
||||
if ( istr.bad() ) throw runtime_error("Schwerer Fehler in istr");
|
||||
if ( istr.fail() ) // Versuch des Aufraeumens
|
||||
{
|
||||
cout << " Failed in reading all data.\n";
|
||||
istr.clear();
|
||||
}
|
||||
}
|
||||
v.shrink_to_fit(); // C++11
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void read_vector_from_file(const string& file_name, vector<double>& v)
|
||||
{
|
||||
ifstream fin(file_name); // Oeffne das File im ASCII-Modus
|
||||
if( fin.is_open() ) // File gefunden:
|
||||
{
|
||||
v.clear(); // Vektor leeren
|
||||
fill_vector(fin, v);
|
||||
}
|
||||
else // File nicht gefunden:
|
||||
{
|
||||
cout << "\nFile " << file_name << " has not been found.\n\n" ;
|
||||
assert( fin.is_open() && "File not found." ); // exeption handling for the poor programmer
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void write_vector_to_file(const string& file_name, const vector<double>& v)
|
||||
{
|
||||
ofstream fout(file_name); // Oeffne das File im ASCII-Modus
|
||||
if( fout.is_open() )
|
||||
{
|
||||
for (size_t k=0; k<v.size(); ++k)
|
||||
{
|
||||
fout << v.at(k) << endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\nFile " << file_name << " has not been opened.\n\n" ;
|
||||
assert( fout.is_open() && "File not opened." ); // exeption handling for the poor programmer
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue