Please write the code for the removeBad function. Will thumbs up if correct!

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 6RQ
icon
Related questions
Question

Please write the code for the removeBad function. Will thumbs up if correct!

D#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cassert>
3
4.
using namespace std;
7
vector<int> destroyedOnes;
9
aclass Movie
{
public:
Movie(int r) : m_rating(r) {}
Movie() { destroyedOnes.push_back(m_rating); }
int rating() const { return m_rating; }
private:
int m_rating;
[};
10
11
12
13
14
15
16
17
18
B// Remove the movies in li with a rating below 50 and destroy them.It is acceptable
||// if the order of the remaining movies is not the same as in the original list.
avoid removeBad (list<Movie*>& li)
19
20
21
22
{
23
24
[}
25
26
avoid test() {
int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 };
list<Movie*> x;
27
28
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);
29
30
31
32
33
assert(x.size() == 4 && destroyedOnes.size() == 4);
vector<int> v;
for (list<Movie*>::iterator p = x.begin(); p != x.end(); p++)
34
35
36
37
{
Movie* mp = *p;
38
39
v. push_back(mp->rating());
40
sort(v.begin(), v.end());
int expect[4] = { 70, 80, 85, 90 };
for (int k = 0; k < 4; k++)
41
42
43
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]);
44
45
46
47
48
49
}
50
51
aint main() {
test();
cout <« "Passed" << endl;
52
53
54
return 0;
55
Transcribed Image Text:D#include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> 3 4. using namespace std; 7 vector<int> destroyedOnes; 9 aclass Movie { public: Movie(int r) : m_rating(r) {} Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; [}; 10 11 12 13 14 15 16 17 18 B// Remove the movies in li with a rating below 50 and destroy them.It is acceptable ||// if the order of the remaining movies is not the same as in the original list. avoid removeBad (list<Movie*>& li) 19 20 21 22 { 23 24 [} 25 26 avoid test() { int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 }; list<Movie*> x; 27 28 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); 29 30 31 32 33 assert(x.size() == 4 && destroyedOnes.size() == 4); vector<int> v; for (list<Movie*>::iterator p = x.begin(); p != x.end(); p++) 34 35 36 37 { Movie* mp = *p; 38 39 v. push_back(mp->rating()); 40 sort(v.begin(), v.end()); int expect[4] = { 70, 80, 85, 90 }; for (int k = 0; k < 4; k++) 41 42 43 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]); 44 45 46 47 48 49 } 50 51 aint main() { test(); cout <« "Passed" << endl; 52 53 54 return 0; 55
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Multiple table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,