diff --git a/Sheet1/data_1.txt b/Sheet1/data_1.txt new file mode 100644 index 0000000..49315c2 --- /dev/null +++ b/Sheet1/data_1.txt @@ -0,0 +1,500 @@ +141 +261 +87 +430 +258 +298 +425 +120 +496 +707 +244 +786 +75 +394 +4 +221 +2 +190 +143 +269 +175 +139 +599 +902 +940 +222 +483 +377 +524 +265 +69 +437 +174 +27 +955 +431 +962 +763 +8 +681 +706 +646 +553 +219 +773 +229 +371 +891 +857 +403 +319 +609 +911 +910 +592 +333 +854 +443 +905 +34 +533 +717 +180 +337 +188 +322 +404 +549 +49 +553 +275 +242 +244 +155 +957 +936 +819 +729 +176 +361 +189 +2 +317 +700 +626 +544 +440 +288 +502 +762 +763 +577 +748 +646 +124 +505 +348 +93 +148 +199 +673 +432 +695 +257 +10 +533 +280 +947 +907 +393 +25 +672 +838 +972 +57 +451 +583 +687 +720 +651 +727 +374 +582 +117 +58 +980 +285 +595 +963 +186 +194 +342 +933 +391 +274 +152 +398 +375 +132 +436 +92 +615 +11 +574 +790 +236 +449 +570 +62 +497 +643 +222 +838 +972 +847 +506 +279 +747 +237 +958 +621 +601 +173 +91 +256 +859 +912 +700 +726 +230 +577 +811 +404 +989 +90 +321 +512 +61 +726 +557 +530 +830 +859 +790 +318 +453 +753 +110 +110 +270 +525 +973 +711 +312 +292 +851 +912 +640 +256 +89 +839 +585 +949 +62 +585 +286 +828 +191 +443 +394 +827 +677 +208 +319 +134 +672 +571 +170 +148 +477 +909 +553 +33 +54 +806 +452 +383 +790 +365 +533 +712 +872 +329 +651 +975 +76 +588 +414 +310 +264 +759 +996 +187 +782 +196 +993 +803 +425 +729 +499 +809 +357 +74 +591 +911 +194 +433 +750 +40 +947 +764 +559 +184 +498 +518 +995 +855 +963 +679 +404 +935 +480 +232 +397 +706 +559 +757 +996 +963 +536 +964 +116 +52 +305 +581 +531 +902 +541 +432 +543 +713 +17 +801 +143 +479 +257 +370 +662 +170 +279 +199 +196 +327 +881 +472 +404 +180 +969 +408 +845 +616 +377 +878 +785 +465 +814 +899 +430 +335 +597 +902 +703 +378 +735 +955 +543 +541 +312 +72 +182 +93 +464 +10 +916 +643 +2 +31 +209 +455 +128 +9 +728 +355 +781 +437 +437 +50 +50 +92 +595 +242 +842 +858 +964 +489 +221 +227 +537 +763 +348 +462 +640 +918 +162 +716 +578 +434 +885 +394 +179 +634 +625 +328 +803 +1000 +981 +128 +233 +24 +608 +111 +408 +885 +549 +370 +209 +441 +957 +125 +471 +857 +44 +692 +979 +284 +134 +686 +910 +611 +900 +194 +755 +347 +419 +156 +820 +625 +739 +806 +68 +951 +498 +756 +743 +832 +157 +458 +619 +933 +836 +896 +583 +583 +855 +35 +886 +408 +37 +747 +155 +144 +606 +255 +325 +402 +407 +387 +610 +167 +189 +95 +324 +770 +235 +741 +693 +825 +828 +294 +310 +524 +326 +832 +811 +557 +263 +681 +234 +457 +385 +539 +992 +756 +981 +235 +529 +52 +757 +602 +858 +989 +930 +410 +1 +541 +208 +220 +326 +96 +748 +749 +544 +339 +833 +553 +958 +893 +357 +547 +347 +623 +797 +746 +126 +823 +26 +415 +732 +782 +368 diff --git a/Sheet1/mainEx1.cpp b/Sheet1/mainEx1.cpp index e69de29..15583a8 100644 --- a/Sheet1/mainEx1.cpp +++ b/Sheet1/mainEx1.cpp @@ -0,0 +1,85 @@ +#include // for input/output +#include // for pow (used in geometric mean) +#include + +using namespace std; +void computeMeans(int a, int b, int c, double &arith, double &geom, double &harm) { //doesn't return anything + arith = (a + b + c) / 3.0; + geom = pow(a * b * c, 1.0 / 3.0); + harm = 3.0 / ( (1.0 / a) + (1.0 / b) + (1.0 / c) ); +} +void computeMeansVector(const vector &v, double &arith, double &geom, double &harm) { + int n = v.size(); // number of elements in the vector + // If the vector is empty, avoid division by zero + if (n == 0) { + arith = geom = harm = 0.0; + return; + } + double sum = 0.0; + double product = 1.0; + double reciprocalSum = 0.0; + + for (int i = 0; i < n; ++i) { + sum += v[i]; + product *= v[i]; + reciprocalSum += 1.0 / v[i]; + } + + arith = sum / n; + geom = pow(product, 1.0 / n); + harm = n / reciprocalSum; +} + +int main() { + int x, y, z; + double am, gm, hm; // to store the results + double amV, gmV, hmV; // to store the results vector case + vector values; + + // Example input 1 + x = 1; y = 4; z = 16; + computeMeans(x, y, z, am, gm, hm); + cout << "Input: (1, 4, 16)" << endl; + cout << "Arithmetic mean: " << am << endl; + cout << "Geometric mean: " << gm << endl; + cout << "Harmonic mean: " << hm << endl << endl; + + values = {1, 4, 16}; + computeMeansVector(values, amV, gmV, hmV); + cout << "Input: (1, 4, 16)" << endl; + cout << "Arithmetic mean: " << amV << endl; + cout << "Geometric mean: " << gmV << endl; + cout << "Harmonic mean: " << hmV << endl << endl; + + // Example input 2 + x = 2; y = 3; z = 5; + computeMeans(x, y, z, am, gm, hm); + cout << "Input: (2, 3, 5)" << endl; + cout << "Arithmetic mean: " << am << endl; + cout << "Geometric mean: " << gm << endl; + cout << "Harmonic mean: " << hm << endl << endl; + + values = {2, 3, 5}; + computeMeansVector(values, amV, gmV, hmV); + cout << "Input: (2, 3, 5)" << endl; + cout << "Arithmetic mean: " << amV << endl; + cout << "Geometric mean: " << gmV << endl; + cout << "Harmonic mean: " << hmV << endl << endl; + + // Example input 3 + x = 1000; y = 4000; z = 16000; + computeMeans(x, y, z, am, gm, hm); + cout << "Input: (1000, 4000, 16000)" << endl; + cout << "Arithmetic mean: " << am << endl; + cout << "Geometric mean: " << gm << endl; + cout << "Harmonic mean: " << hm << endl << endl; + + values = {1000, 4000, 16000}; + computeMeansVector(values, amV, gmV, hmV); + cout << "Input: (1000, 4000, 16000)" << endl; + cout << "Arithmetic mean: " << amV << endl; + cout << "Geometric mean: " << gmV << endl; + cout << "Harmonic mean: " << hmV << endl << endl; + + return 0; +} diff --git a/Sheet1/mainEx2.cpp b/Sheet1/mainEx2.cpp new file mode 100644 index 0000000..9d6191a --- /dev/null +++ b/Sheet1/mainEx2.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +using namespace std; + +int main() { + vector 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; +}