From 78acaf3290d0a051c8034b496697bb2ab4436abf Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 7 Oct 2022 10:00:16 +0100 Subject: [PATCH 1/9] Fix for C++ 17 -> 11 --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 0d73854..848c531 100644 --- a/main.cpp +++ b/main.cpp @@ -11,7 +11,7 @@ string guessRank(int); int main() { std::mt19937 rand {std::random_device{}()}; - std::uniform_int_distribution clamp {1, 100}; + std::uniform_int_distribution clamp {1, 100}; string name {promptfs("Enter your name:")}; std::cout << "Hello " << name << ", welcome to guess the number.\n"; int guesses {}; From 3d2827578e9551cfd51991271030af3f11abe4cb Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 7 Oct 2022 12:10:31 +0100 Subject: [PATCH 2/9] Tutorial 1. Question 1. --- main.cpp | 106 ++++++------------------------------------------------- 1 file changed, 10 insertions(+), 96 deletions(-) diff --git a/main.cpp b/main.cpp index 848c531..bf50347 100644 --- a/main.cpp +++ b/main.cpp @@ -1,101 +1,15 @@ #include -#include -#include using string = std::string; -void ignoreTillLineEnd(); -string promptfs(string); -int promptfi(string); -string guessRank(int); -int main() -{ - std::mt19937 rand {std::random_device{}()}; - std::uniform_int_distribution clamp {1, 100}; - string name {promptfs("Enter your name:")}; - std::cout << "Hello " << name << ", welcome to guess the number.\n"; - int guesses {}; - int num {clamp(rand)}, ent {}; - do { - if (guesses++ > 0) { - if (ent > num) - std::cout << "You Guessed Too High!\n"; - else - std::cout << "You Guessed Too Low!\n"; - } - ent = promptfi("Enter your guess:"); - } - while (num != ent); - std::cout << "Well done " << name << ", you guessed correctly!\n"; - std::cout << "You took " << guesses << " guesses, rank " << guessRank(guesses) << " .\n"; - return 0; -} - -void ignoreTillLineEnd() { - std::cin.ignore(std::numeric_limits::max(), '\n'); -} - -string promptfs(string msg) { - bool once {}; - string toret {}; - do { - if (once) { - std::cin.clear(); - ignoreTillLineEnd(); - std::cout << "Input Failure!\n"; - } - std::cout << msg << "\n"; - std::cin >> toret; - once = true; - } - while (std::cin.fail()); - ignoreTillLineEnd(); - return toret; -} - -int promptfi(string msg) { - bool once {}; - int toret {}; - do { - if (once) { - std::cin.clear(); - ignoreTillLineEnd(); - std::cout << "Input Failure!\n"; - } - std::cout << msg << "\n"; - std::cin >> toret; - once = true; - } - while (std::cin.fail()); - ignoreTillLineEnd(); - return toret; -} - -string guessRank(int g) { - switch (g) { - case 0: - return "U"; - case 1: - return "A^"; - case 2: - return "A*"; - case 3: - return "A"; - case 4: - case 5: - return "B"; - case 6: - case 7: - return "C"; - case 8: - case 9: - case 10: - return "D"; - case 11: - case 12: - case 13: - return "E"; - default: - return "F"; - } +int main() { + std::cout << "Enter your age:\n"; + int age {0}; + std::cin >> age; + if (age < 18) + std::cout << age << " is too young.\n"; + else if (age > 30) + std::cout << age << " is too old.\n"; + else + std::cout << age << " is the right age.\n"; } From a7efeab624f5736185295f00c9f3dc74441d4175 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 7 Oct 2022 12:18:23 +0100 Subject: [PATCH 3/9] T1 Q2 --- main.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index bf50347..f25eae8 100644 --- a/main.cpp +++ b/main.cpp @@ -3,13 +3,12 @@ using string = std::string; int main() { - std::cout << "Enter your age:\n"; - int age {0}; - std::cin >> age; - if (age < 18) - std::cout << age << " is too young.\n"; - else if (age > 30) - std::cout << age << " is too old.\n"; - else - std::cout << age << " is the right age.\n"; + std::cout << "Enter your name:\n"; + string name; + std::cin >> name; + std::cout << "Hello, " << name << "!\n"; + for (int i {0}; i < static_cast(name.size()) + 8; i++) { + std::cout << "="; + } + std::cout << "\n"; } From 18c5f972c245eac65a5f7a727e6c912b51e3d5ea Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 7 Oct 2022 12:35:44 +0100 Subject: [PATCH 4/9] T1 Q3 --- main.cpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index f25eae8..5cb069b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,30 @@ #include +#include +#include -using string = std::string; - -int main() { - std::cout << "Enter your name:\n"; - string name; - std::cin >> name; - std::cout << "Hello, " << name << "!\n"; - for (int i {0}; i < static_cast(name.size()) + 8; i++) { - std::cout << "="; +int main() +{ + using std::cout; + using std::cin; + srand(time(nullptr)); + int gcount{ 1 }; + int n { 20 }; + int secret { rand()%n + 1 }; + cout << "Guess a number between 1 and " << n << ": "; + int guess; + cin >> guess; + while (guess != secret) + { + if (guess > secret) { + cout << "Number too big ; "; + } else { + cout << "Number too small ; "; + } + gcount++; + cout << "Guess again: "; + cin >> guess; } - std::cout << "\n"; + cout << "Correct!\n"; + cout << "Took " << gcount << " tries.\n"; + return 0; } From c89e5b4e6c228daebfa691a0ca8cfb1777710edd Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 14 Oct 2022 12:08:17 +0100 Subject: [PATCH 5/9] Formatting. --- main.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 5cb069b..2d16d8a 100644 --- a/main.cpp +++ b/main.cpp @@ -15,9 +15,12 @@ int main() cin >> guess; while (guess != secret) { - if (guess > secret) { + if (guess > secret) + { cout << "Number too big ; "; - } else { + } + else + { cout << "Number too small ; "; } gcount++; From 4c260479dd7f673e90b3c4c50e1205a1de63386e Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 14 Oct 2022 12:34:58 +0100 Subject: [PATCH 6/9] w2 q1 --- main.cpp | 90 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/main.cpp b/main.cpp index 2d16d8a..f75ac0e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,33 +1,63 @@ #include -#include -#include +#include +#include +#include -int main() -{ - using std::cout; - using std::cin; - srand(time(nullptr)); - int gcount{ 1 }; - int n { 20 }; - int secret { rand()%n + 1 }; - cout << "Guess a number between 1 and " << n << ": "; - int guess; - cin >> guess; - while (guess != secret) - { - if (guess > secret) - { - cout << "Number too big ; "; - } - else - { - cout << "Number too small ; "; - } - gcount++; - cout << "Guess again: "; - cin >> guess; - } - cout << "Correct!\n"; - cout << "Took " << gcount << " tries.\n"; - return 0; +using string = std::string; + +//Placw in header! +template +void printm(T m){ + std::cout << m; } + +template +void printlm(T m){ + printm(m); + printm("\n"); +} + +typedef std::vector::size_type vtype; + +double promptd(string,bool*); + +int main() { + std::setprecision(3); + printlm("Calculate median and average from sequence:"); + std::vector v{}; + bool s {}; + do { + auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s); + if (s) v.push_back(d); + } + while(s); + sort(v.begin(), v.end()); + printm("The count is: "); + printlm(static_cast(v.size())); + printm("The median is: "); + int halfway {static_cast(v.size()) / 2}; + if (static_cast(v.size())%2 == 1) + printlm(v.at(static_cast(halfway))); + else + printlm((v.at(static_cast(halfway - 1)) + v.at(static_cast(halfway))) / 2.0); + double avg {}; + for (vtype i = 0; i < v.size(); ++i) avg += v.at(i); + avg /= static_cast(v.size()); + printm("The average is: "); + printlm(avg); + return 0; +} + +double promptd(string message, bool *status) { + double tr {}; + *status = false; + std::cout << message; + if (!(std::cin >> tr)){ + std::cin.clear(); + std::cin.ignore(); + return 0; + } + *status = true; + return tr; +} + From 453902b44eae51909461df4081f32b905d8ccd7d Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 14 Oct 2022 12:41:10 +0100 Subject: [PATCH 7/9] w2 q2 --- main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index f75ac0e..35776cd 100644 --- a/main.cpp +++ b/main.cpp @@ -41,7 +41,11 @@ int main() { else printlm((v.at(static_cast(halfway - 1)) + v.at(static_cast(halfway))) / 2.0); double avg {}; - for (vtype i = 0; i < v.size(); ++i) avg += v.at(i); + if (v.size() > 2) { + for (vtype i = 1; i < v.size() - 1; ++i) avg += v.at(i); + } else { + for (vtype i = 0; i < v.size(); ++i) avg += v.at(i); + } avg /= static_cast(v.size()); printm("The average is: "); printlm(avg); From 5ca32463d70b650851b26a5c3f066c3e306e5eec Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 21 Oct 2022 12:46:45 +0100 Subject: [PATCH 8/9] w3 q1 --- main.cpp | 90 ++++++++++++++++++++++++++----------------------------- stats.cpp | 21 +++++++++++++ stats.h | 9 ++++++ 3 files changed, 73 insertions(+), 47 deletions(-) create mode 100644 stats.cpp create mode 100644 stats.h diff --git a/main.cpp b/main.cpp index 35776cd..4f3c93d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,67 +1,63 @@ #include #include -#include #include +#include "stats.h" using string = std::string; //Placw in header! template -void printm(T m){ - std::cout << m; +void printm(T m) +{ + std::cout << m; } template -void printlm(T m){ - printm(m); - printm("\n"); +void printlm(T m) +{ + printm(m); + printm("\n"); } typedef std::vector::size_type vtype; double promptd(string,bool*); -int main() { - std::setprecision(3); - printlm("Calculate median and average from sequence:"); - std::vector v{}; - bool s {}; - do { - auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s); - if (s) v.push_back(d); - } - while(s); - sort(v.begin(), v.end()); - printm("The count is: "); - printlm(static_cast(v.size())); - printm("The median is: "); - int halfway {static_cast(v.size()) / 2}; - if (static_cast(v.size())%2 == 1) - printlm(v.at(static_cast(halfway))); - else - printlm((v.at(static_cast(halfway - 1)) + v.at(static_cast(halfway))) / 2.0); - double avg {}; - if (v.size() > 2) { - for (vtype i = 1; i < v.size() - 1; ++i) avg += v.at(i); - } else { - for (vtype i = 0; i < v.size(); ++i) avg += v.at(i); - } - avg /= static_cast(v.size()); - printm("The average is: "); - printlm(avg); - return 0; -} - -double promptd(string message, bool *status) { - double tr {}; - *status = false; - std::cout << message; - if (!(std::cin >> tr)){ - std::cin.clear(); - std::cin.ignore(); +int main() +{ + std::setprecision(3); + printlm("Calculate the average and median of scores:"); + bool s {}; + std::vector v {}; + do + { + auto d = promptd("Enter a score (Enter a non number to finish entry) : ",&s); + if (s) v.push_back(d); + } + while(s); + printm("Score median is: "); + printlm(med(v, false)); + printm("Score median (No Extremes) is: "); + printlm(med(v, true)); + printm("Score average is: "); + printlm(avg(v, false)); + printm("Score average (No Extremes) is: "); + printlm(avg(v, true)); return 0; - } - *status = true; - return tr; +} + +double promptd(string message, bool *status) +{ + double tr {0.0}; + *status = false; + std::cout << message; + if (!(std::cin >> tr)) + { + std::cin.clear(); + std::cin.ignore(); + return 0.0; + } + *status = true; + return tr; } diff --git a/stats.cpp b/stats.cpp new file mode 100644 index 0000000..b526eb3 --- /dev/null +++ b/stats.cpp @@ -0,0 +1,21 @@ +#include "stats.h" +#include +#include + +double med(std::vector &scores,bool ignoreExtremes) { + const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0; + sort(scores.begin(), scores.end()); + using vec_size = std::vector::size_type; + const vec_size middle = (vec_size) (len / 2); + if (middle%2 == 1) + return scores[middle]; + else + return (scores[middle - 1] + scores[middle]) / 2.0; +} + +double avg(const std::vector &scores,bool ignoreExtremes) { + auto sum = 0.0; + using vec_size = std::vector::size_type; + for (vec_size i = (ignoreExtremes && scores.size() > 2) ? 1 : 0; i < scores.size() - (ignoreExtremes && scores.size() > 2) ? 1 : 0; i++) sum += scores[i]; + return sum / static_cast(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0); +} diff --git a/stats.h b/stats.h new file mode 100644 index 0000000..dcc76e9 --- /dev/null +++ b/stats.h @@ -0,0 +1,9 @@ +#ifndef STATS_H_INCLUDED +#define STATS_H_INCLUDED + +#include + +double med(std::vector &scores,bool ignoreExtremes); +double avg(const std::vector &scores,bool ignoreExtremes); + +#endif // STATS_H_INCLUDED From f8c0961b3fb0842e41ac7eca155a0a64040e5c08 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 21 Oct 2022 13:08:46 +0100 Subject: [PATCH 9/9] Update project file. --- CppTutorial.cbp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CppTutorial.cbp b/CppTutorial.cbp index 2f568c5..cbf7bda 100644 --- a/CppTutorial.cbp +++ b/CppTutorial.cbp @@ -33,6 +33,8 @@ + +