Exercise 2, Sheet 1
This commit is contained in:
parent
ab852827e3
commit
570fdc4e38
1 changed files with 50 additions and 0 deletions
50
mainEx2.cpp
Normal file
50
mainEx2.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
vector<double> data;
|
||||
double x;
|
||||
|
||||
ifstream infile("data_1.txt");
|
||||
while (infile >> x) { //>> takes things from an input and saves as x
|
||||
data.push_back(x); //saves each x in data
|
||||
}
|
||||
infile.close();
|
||||
|
||||
int n = data.size();
|
||||
|
||||
double minVal = *min_element(data.begin(), data.end());
|
||||
double maxVal = *max_element(data.begin(), data.end());
|
||||
|
||||
double sum = 0.0, logSum = 0.0, recipSum = 0.0;
|
||||
for (int i = 0; i < n; ++i) { //for (initialization; condition; update)
|
||||
sum += data[i];
|
||||
logSum += log(data[i]);
|
||||
recipSum += 1.0 / data[i];
|
||||
}
|
||||
double arith = sum / n;
|
||||
double geom = exp(logSum / n);
|
||||
double harm = n / recipSum;
|
||||
|
||||
// standard deviation
|
||||
double variance = 0.0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
variance += (data[i] - arith) * (data[i] - arith);
|
||||
}
|
||||
double stdev = sqrt(variance / n);
|
||||
|
||||
ofstream outfile("out_1.txt");
|
||||
outfile << "Minimum: " << minVal << endl;
|
||||
outfile << "Maximum: " << maxVal << endl;
|
||||
outfile << "Arithmetic mean: " << arith << endl;
|
||||
outfile << "Geometric mean: " << geom << endl;
|
||||
outfile << "Harmonic mean: " << harm << endl;
|
||||
outfile << "Standard deviation: " << stdev << endl;
|
||||
outfile.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue