Question

C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using...

C++

Implement the removeBad function:

        #include <list>
        #include <vector>
        #include <algorithm>
        #include <iostream>
        #include <cassert>
        using namespace std;

        vector<int> 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 50 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<Movie*>& li)
        {
        }

        void test()
        {
            int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 };
            list<Movie*> 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<int> v;
            for (list<Movie*>::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<Movie*>::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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cassert>
using namespace std;

vector<int> 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 50 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<Movie*>& li)
{   
//iterate through list
for(list<Movie*>::iterator it=li.begin();it!=li.end();){
if((*it)->rating()<50){//if rating less than 50
Movie *movie=*it;//retrive the movie
it=li.erase(it);//erase from list move to next iterator
delete movie;//delete the removed movie
}else{
++it;//move to next element
}
}
}

void test()
{
int a[8] = { 85, 80, 30, 70, 20, 15, 90, 10 };
list<Movie*> 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<int> v;
for (list<Movie*>::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<Movie*>::iterator p = x.begin(); p != x.end(); p++)
delete *p; // Deallocate remaining movies.
}

int main()
{
test();
cout << "Passed" << endl;
}

output:

sh-4.2$ gt+ -o main *.cpp sh-4.2$ main Passed sh-4.2$

Add a comment
Know the answer?
Add Answer to:
C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert>...

    Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert> using namespace std; Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS. Pre-condition: a.size() == b.size(), and a.size() > 0 Post-condition: Returns a vector equal to {min(a[0],b[0]), min(a[1],b[1]), min(a[2],b[2]), ..., min(a[n],b[n])}, where n == a.size(). For example, min_vec({3, 4, 1}, {2, 5, 2}) returns the new vector {2, 4, 1}. These are the function headers: vector<int> min_vec(const vector<int>&...

  • This is a c++ code /**    Write a function sort that sorts the elements of...

    This is a c++ code /**    Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std;...

    C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std; void fillVector(vector <double> &); double average(const vector <double> &); int main() { return 0; } void fillVector(vector <double> &v) { double d; while (cin >> d) { v.push_back(d); } } double average(const vector <double> &v) { double sum = 0.; for (int i = 0; i < v.size(); i++) { sum += v.at(i); } return sum / v.size(); } standardDeviation() //stuck on this part...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • Change the the following C++ codes to list the "marks" reported Alphabetically. --------------------------------------------------------------------------------------------------------- #include <iostream> #include<string.h>...

    Change the the following C++ codes to list the "marks" reported Alphabetically. --------------------------------------------------------------------------------------------------------- #include <iostream> #include<string.h> using namespace std; void display(int marks[10]); int main() { int marks[10]; int i;x for (i=0; i<10; i++) { cout << "Enter marks : "; cin >> marks[i]; } display(marks); return 0; } // Create the function sort to sort the marks in alphabetical order void display(int marks[10]) { cout <<"Displaying marks in Ascending Order " << endl; for (int i = 0; i <10;...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT