commit 6be81d2d733ef8332185089ebdca06054be81406 Author: jakob.schratter Date: Wed Oct 22 15:16:25 2025 +0200 Upload files to "ex1A_mean_values" diff --git a/ex1A_mean_values/main.cpp b/ex1A_mean_values/main.cpp new file mode 100644 index 0000000..03da30f --- /dev/null +++ b/ex1A_mean_values/main.cpp @@ -0,0 +1,35 @@ +#include "means.h" +#include +#include +using namespace std; + +int main(int argc, char **argv) +{ + double arithmetic_mean, geometric_mean, harmonic_mean; + + // Fixed version + calculate_means(1, 4, 16, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + + calculate_means(2, 3, 5, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + + calculate_means(1000, 4000, 16000, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + cout << "--------------------------------" << endl; + + + + // Scalable version + calculate_means(vector {1, 4, 16}, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + + calculate_means(vector {2, 3, 5}, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + + calculate_means(vector {1000, 4000, 16000}, arithmetic_mean, geometric_mean, harmonic_mean); + cout << arithmetic_mean << ", " << geometric_mean << ", " << harmonic_mean << endl; + + + return 0; +} diff --git a/ex1A_mean_values/means.cpp b/ex1A_mean_values/means.cpp new file mode 100644 index 0000000..903a9e2 --- /dev/null +++ b/ex1A_mean_values/means.cpp @@ -0,0 +1,30 @@ +#include "../ex1A_mean_values/means.h" +#include +#include + +void calculate_means(int a, int b, int c, double &am, double &gm, double &hm) +{ + am = (a + b + c)/3.0; + gm = exp((log(a)+log(b)+log(c))/3); + hm = 3.0/(1.0/a + 1.0/b + 1.0/c); +} + +void calculate_means(std::vector numbers, double &am, double &gm, double &hm) +{ + int n = numbers.size(); + + am = 0.; + gm = 0.; + hm = 0.; + + for (int i = 0; i < n; ++i) + { + am += numbers[i]; + gm += log(numbers[i]); + hm += 1.0/numbers[i]; + } + + am /= n; + gm = exp(gm/n); + hm = n/hm; +} \ No newline at end of file diff --git a/ex1A_mean_values/means.h b/ex1A_mean_values/means.h new file mode 100644 index 0000000..4a12964 --- /dev/null +++ b/ex1A_mean_values/means.h @@ -0,0 +1,23 @@ +#pragma once +#include + +/** + This function calculates arithmetic mean, geometric mean and harmonic mean of three integers. + @param[in] a first integer + @param[in] b second integer + @param[in] c third integer + @param[out] am arithmetic mean + @param[out] gm geometric mean + @param[out] hm harmonic mean +*/ +void calculate_means(int a, int b, int c, double &am, double &gm, double &hm); + +/** + This function calculates arithmetic mean, geometric mean and harmonic mean of an integer vector. + @param[in] numbers vector containing integers + @param[out] am arithmetic mean + @param[out] gm geometric mean + @param[out] hm harmonic mean +*/ +void calculate_means(std::vector numbers, double &am, double &gm, double &hm); +