Challenge: Passing Grades
Completed
Read n scores and a pass mark p. Using count_if with a lambda that captures p, print how many passed (score ≥ p). Then remove the failing scores with the erase-remove idiom and print the passing ones on one line (or none if nobody passed).
Input
n, then n integers, then p.
Output
Passed: 3 70 85 90
Example
Input
5 70 40 85 55 90 60
Output
Passed: 3 70 85 90
Editor
C++
Hints
[p](int s) { return s >= p; } captures the pass mark.
v.erase(remove_if(v.begin(), v.end(), rule), v.end());
Solution explanation
The lambdas capture p by value, so the same pass mark is used for counting and for removing.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> scores(n);
for (int& s : scores) cin >> s;
int p;
cin >> p;
int passed = count_if(scores.begin(), scores.end(), [p](int s) { return s >= p; });
cout << "Passed: " << passed << "\n";
scores.erase(remove_if(scores.begin(), scores.end(), [p](int s) { return s < p; }), scores.end());
if (scores.empty()) {
cout << "none\n";
} else {
for (size_t i = 0; i < scores.size(); i++) {
if (i > 0) cout << " ";
cout << scores[i];
}
cout << "\n";
}
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module