Question
Write a C++ program that will perform operations on a pointer array of structs. struct Film { int id; // the unique id of the
Write a C++ program that will perform operations on a pointer array of structs.
struct Film {
int id; // the unique id of the film
string name; // the film's name
string description; // the film's description
int runningTime; // the film's running time in minutes <= 180
FilmRating rating; // the film's rating, from the enum FilmRating
};
enum FilmRating {G, PG, PG_13, NC_17, R, UNRATED};
Both the struct and the enum are in the global scope.
Your program should perform the following operations:
Initialize an array pointer of Film with at least 4 elements; use any film names, ratings, running times, and descriptions you like, real or imagined. Print the results from your original definition using printFilms (see below) . See an example at the end of this document.
Using dynamic memory allocation, write a function resize that conforms to the following signature
Film * resize (Film *films, int currentSize, int newSize)
This function should
Allocate new memory in the indicated amount (newSize)
Copy the elements in films into the newly allocated memory
Delete the old films array memory
Return the newly created array pointer memory
Using dynamic memory allocation, write a function reverse that conforms to the following signature
Film * reverse (Film *films, int currentSize)
This function should
Allocate new memory in the indicated amount (currentSize)
Copy the elements in films into the newly allocated memory in reverse order
Define and use the following helper functions in your program
string filmRatingToString(FilmRating rating): Returns a readable version of the rating constant, e.g., with an input of G returns the string G;
void printFilm (Film film): Prints the struct film to the console in a readable format,
void printFilms (Film *films): Prints a pointer array of structs in a readable format;
Testing Your Program
After initializing your pointer array with Films, print the films. Label the output appropriately
Create a new pointer array of Films by performing a reverse call with the original array.
Perform a resize on the Film pointer array, extending its size by 1. Add a new film at the end of the pointer array. Print the films. Label the output accordingly.
Perform a resize on the Film pointer array, extending its size by 2. Add 2 new films at the end of the pointer array. Print the films. Label the output accordingly.
Example: Assignment for Pointer Array of 4 films
films[0].id = 0;
films[0].name = "Under World";
films[0].description = "Vampires vs. Werewolves";
films[0].runningTime = 134;
films[0].rating = R;
films[1].id = 1;
films[1].name = "Bambi";
films[1].description = "A young deer grows up";
films[1].runningTime = 70;
films[1].rating = G;
films[2].id = 2;
films[2].name = "Blade Runner";
films[2].description =
"A Blade Runner must pursue and try to terminate four replicants";
films[2].runningTime = 117;
films[2].rating = R;
films[3].id = 3;
films[3].name = "Limitless";
films[3].description =
"A mysterious pill helps a man use 100% of his brain";
films[3].runningTime = 105;
films[3].rating = PG_13;
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