Question

HELP WITH C++ The following movies have recently come out: 1. Black Panther, PG-13 2. Avengers:...

HELP WITH C++

The following movies have recently come out:

1. Black Panther, PG-13

2. Avengers: Infinity War, PG-13

3. A Wrinkle In Time, PG

4. Ready Player One, PG-13

5. Red Sparrow, R

6. The Incredibles 2, G

To do: 1. Create a Movie class that stores the movie name and the MPAA rating (i.e. R, PG13, etc.). Write appropriate constructors, setters, and getters. 2. In your main function create an array of type Movie that stores the movie information. 3. Write a sort function (it could be bubble sort, selection sort, etc., any sort of your choice) that sorts the movies alphabetically by name. 4. In your main function call the sort function and output the names and ratings in name-sorted order.

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

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

#include <iostream>
#include <iomanip>
using namespace std;
class Movie
{
   private:
   string name;
   string rating;
   public:
   Movie(){}
   Movie(string name1, string rating1){
       name = name1;
       rating = rating1;
   }
   void setName(string s){
       name = s;
   }
   void setRating(string r){
       rating = r;
   }
   string getName(){
       return name;
   }
   string getRating(){
       return rating;
   }
};

void sortByName(Movie movies[], int n);

int main(){
   Movie movies[6];
   movies[0] = Movie("Black Panther", "PG-13");
   movies[1] = Movie("Avengers: Infinity War", "PG-13");
   movies[2] = Movie("A Wrinkle In Time", "PG");
   movies[3] = Movie("Ready Player One", "PG-13");
   movies[4] = Movie("Red Sparrow", "R");
   movies[5] = Movie("The Incredibles 2", "G");
  
   sortByName(movies, 6);
   //display sorted data
   cout << "Movies sorted by name are " << endl;
   for(int i = 0; i < 6; i++){
       cout << left << setw(25) << movies[i].getName() << setw(10) << movies[i].getRating() << endl;
   }
   return 0;
}

void sortByName(Movie movies[], int n){
   //bubble sort
   for(int i = 0; i < n-1; i++){
       for(int j = 0; j < n-i-1; j++){
           if(movies[j].getName() > movies[j+1].getName()){
               Movie temp = movies[j];
               movies[j] = movies[j+1];
               movies[j+1] = temp;
           }
       }
   }
}

output
----
Movies sorted by name are
A Wrinkle In Time PG
Avengers: Infinity War PG-13
Black Panther PG-13
Ready Player One PG-13
Red Sparrow R
The Incredibles 2 G

Add a comment
Know the answer?
Add Answer to:
HELP WITH C++ The following movies have recently come out: 1. Black Panther, PG-13 2. Avengers:...
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
  • Consider a class Movie that contains information about a movie. The class has the following attri...

    C++, this is an intro class please do not make it complicated. Consider a class Movie that contains information about a movie. The class has the following attributes The movie name The MPAA rating (for example, G, PG, PG-13, R) An array to store the movie ratings .The number of people that have rated this movie as a 1 (Terrible) .The number of people that have rated this movie as a 2 (Bad) .The number of people that have rated...

  • C++ A sample project is provided AverageGrade that is very similar to the MovieRatings project, but...

    C++ A sample project is provided AverageGrade that is very similar to the MovieRatings project, but it only is inputting one string (className) instead of both the movie name and the MPAA rating. Another difference is that the AverageGrade program scores grades A to F with point values from 4.0 to 0.0 (highest to lowest) while the MovieRatings scores the rating from 1 to 5 (lowest to highest). When using the AverageGrade program as a reference, make sure that the...

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