1
0

Negative int counter

This commit is contained in:
Captain ALM 2022-11-04 12:22:52 +00:00
parent df047b3197
commit 413ca3faef
3 changed files with 17 additions and 35 deletions

View File

@ -5,8 +5,7 @@
#include <iomanip> #include <iomanip>
using string = std::string; using string = std::string;
using dlist = std::list<double>; using ilist = std::list<int>;
using wordstat = std::map<string,dlist*>;
//Place in header! //Place in header!
template <typename T> template <typename T>
@ -21,27 +20,24 @@ void printlm(T m){
} }
int prompti(string,bool*); int prompti(string,bool*);
double promptd(string,bool*); int prompti(string,bool*);
string prompts(string,bool*); string prompts(string,bool*);
double avg(std::list<double>*); bool isneg(int);
double med(std::list<double>*);
int main() { int main() {
printlm("Get stats of inputs:"); printlm("Get the number of negatives from::");
bool s1 {}; bool s {};
bool s2 {}; ilist lst {};
wordstat stat {}; do {
do {, auto v = prompti("Enter an integer: ", &s);
auto v1 = prompts("Enter a word: ", &s1); if (s) {
auto v2 = promptd("Enter a double: ", &s2); lst.push_back(v);
if (s1 && s2) {
if (stat[v1] == null) {
stat[v1] = new dlist {};
} }
stat[v1].push_back(v2); } while (s);
} printm("Number of negatives (Defined): ");
} while (s1 && s2); printlm(count_if(lst.cbegin(), lst.cend(), isneg));
printm("Number of negatives (Lambda): ");
printlm(count_if(lst.cbegin(), lst.cend(), [] (int x) { return x < 0;}));
return 0; return 0;
} }
@ -84,20 +80,6 @@ string prompts(string message, bool *status) {
return tr; return tr;
} }
double avg(std::list<double> *values) { bool isneg(int x) {
if (values->size() < 1) return 0.0; return x < 0;
double sum {};
for (const auto c : *values) sum += c;
return sum / values->size();
}
double med(std::list<double> *values) {
if (values->size() < 1) return 0.0;
sort(values->begin(), values->end());
auto hw {values->size()/2};
if (values->size()%2 == 1) {
return values->at(hw);
} else {
return (values->at(hw - 1) + values->at(hw)) / 2.0;
}
} }

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.