1
0

number of 0s before 1 (count_if)

This commit is contained in:
Captain ALM 2022-11-04 12:30:49 +00:00
parent 413ca3faef
commit b8eff8c9ea
3 changed files with 12 additions and 8 deletions

View File

@ -22,10 +22,10 @@ void printlm(T m){
int prompti(string,bool*);
int prompti(string,bool*);
string prompts(string,bool*);
bool isneg(int);
bool num0b1(int);
int main() {
printlm("Get the number of negatives from::");
printlm("Get the number of 0s before 1 from:");
bool s {};
ilist lst {};
do {
@ -34,10 +34,8 @@ int main() {
lst.push_back(v);
}
} while (s);
printm("Number of negatives (Defined): ");
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;}));
printm("Number of 0s before 1: ");
printlm(count_if(lst.cbegin(),lst.cend(), num0b1));
return 0;
}
@ -80,6 +78,12 @@ string prompts(string message, bool *status) {
return tr;
}
bool isneg(int x) {
return x < 0;
bool num0b1(int x) {
static bool has1 {};
if (x == 1) has1 = true;
if (has1 || x != 0) {
return false;
} else {
return true;
}
}

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.