diff --git a/main.cpp b/main.cpp index b2223df..9da1f51 100644 --- a/main.cpp +++ b/main.cpp @@ -1,68 +1,61 @@ #include -#include +#include +#include +#include using string = std::string; -using stringv = std::string_view; -void printm(stringv); -void printlm(stringv); -int prompti(stringv); -string prompts(stringv); -string promptm(stringv*, int); - -int main() { - bool run {true}; - while (run){ - int input { prompti(promptm(new stringv[] { - "Enter an option:", - "0) Say hello.", - "*) Exit."}, 3)) }; - if (input == 0) - printlm("Hello."); - else{ - printlm("Bye!"); - run = false; - } - } - return 0; -} - -void printm(stringv m){ +//Placw in header! +template +void printm(T m){ std::cout << m; } -void printlm(stringv m){ +template +void printlm(T m){ printm(m); printm("\n"); } -int prompti(stringv message) { - int tr {}; +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(v.size()); + printm("The median is: "); + int halfway {static_cast(v.size()) / 2}; + if (static_cast(v.size())%2 == 1) + printlm(v.at(halfway)); + else + printlm((v.at(halfway - 1) + v.at(halfway)) / 2.0); + double avg {}; + for (int i = 0; i < static_cast(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 -1; + return 0; } + *status = true; return tr; } -string prompt(stringv message) { - string toret {""}; - std::cout << message; - if (!(std::cin >> toret)){ - std::cin.clear(); - std::cin.ignore(); - return ""; - } - return toret; -} - -string promptm(stringv msgs[], int l) { - string tr {""}; - for (int i = 0; i < l; i++) { - tr.append(msgs[i]); - tr.append("\n"); - } - return tr; -} diff --git a/main.o b/main.o index bf8ba7f..6a63b73 100644 Binary files a/main.o and b/main.o differ diff --git a/t b/t index bd3a67c..d264ff6 100755 Binary files a/t and b/t differ