Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use list not vector! Implement the removeBad function: #include #include #include #include #include using namespace std; vector destroyedOnes; class Movie { public: Movie(int r) :
Use list not vector! Implement the removeBad function:
#include#include
#include #include #include using namespace std; vector destroyedOnes; class Movie { public: Movie(int r) : m_rating(r) {} ~Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; }; // Remove the movies in li with a rating below 55 and destroy them. // It is acceptable if the order of the remaining movies is not // the same as in the original list. void removeBad(list & li) { } void test() { int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 }; list x; for (int k = 0; k < 8; k++) x.push_back(new Movie(a[k])); assert(x.size() == 8 && x.front()->rating() == 85 && x.back()->rating() == 10); removeBad(x); assert(x.size() == 4 && destroyedOnes.size() == 4); vector v; for (list ::iterator p = x.begin(); p != x.end(); p++) { Movie* mp = *p; v.push_back(mp->rating()); } // Aside: In C++11, the above loop could be // for (auto p = x.begin(); p != x.end(); p++) // { // Movie* mp = *p; // v.push_back(mp->rating()); // } // or // for (auto p = x.begin(); p != x.end(); p++) // { // auto mp = *p; // v.push_back(mp->rating()); // } // or // for (Movie* mp : x) // v.push_back(mp->rating()); // or // for (auto mp : x) // v.push_back(mp->rating()); sort(v.begin(), v.end()); int expect[4] = { 70, 80, 85, 90 }; for (int k = 0; k < 4; k++) assert(v[k] == expect[k]); sort(destroyedOnes.begin(), destroyedOnes.end()); int expectGone[4] = { 10, 15, 20, 30 }; for (int k = 0; k < 4; k++) assert(destroyedOnes[k] == expectGone[k]); for (list ::iterator p = x.begin(); p != x.end(); p++) delete *p; // Deallocate remaining movies. } int main() { test(); cout << "Passed" << endl; }
For this problem, you will turn a file named bad.cpp with the body of the removeBad function, from its "void" to its "}", no more and no less. Your function must compile and work correctly when substituted into the program above.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started