Topic: Data structure Language: c++, Movie Data: Write a program that uses a str
ID: 665111 • Letter: T
Question
Topic: Data structure Language: c++, Movie Data: Write a program that uses a structure named MovieData to store the following information about a movie: The program should create two MovieData variables, store values in their members, and pass each one, in turn, to a function that displays the information about the movie in a clearly formatted manner. Must have at least one dynamic allocation: eg. MovieData *movie1 = new... o Must have at least one static declaration, eg. MovieData movie 1; o Must set all the fields of the dynamically allocated movie structure. o Must set all the fields of the statically declared movie structure. o Must call a FUNCTION to correctly display the dynamically allocated movie structure. o Must call a FUNCTION (could be the same one) to correctly display the statically allocated movie structure. Notes: Please explain the steps with comments. Thanks.Explanation / Answer
#include <iostream>
#include<string.h>
using namespace std;
//declaring structure with the given fields
struct MovieData {
char title[30]; //a string to store title of movie
char director[30]; //a string to store name of the director of movie
char released[20]; //a string to store release date of movie
int runsfor; //an integer to store running time(in minutes) of movie
};
//function to display the values of the members of structure having static declaration
void displaymovie(MovieData md)
{
cout<<"Movie 1 : ";
cout<<"Movie Title : "<<md.title<<" ";
cout<<"Directed By : "<<md.director<<" ";
cout<<"Released on : "<<md.released<<" ";
cout<<"Running Time : "<<md.runsfor<<" minutes ";
}
//function to display the values of the members of structure having dynamic declaration(function overloading)
void displaymovie(MovieData* md)
{
cout<<"Movie 2 : ";
cout<<"Movie Title : "<<md->title<<" ";
cout<<"Directed By : "<<md->director<<" ";
cout<<"Released on : "<<md->released<<" ";
cout<<"Running Time : "<<md->runsfor<<" minutes ";
}
int main() {
struct MovieData movie1; //static declaration
struct MovieData *movie2 = new MovieData; //dynamic declaration
//Setting all fields of static stuct object
strcpy(movie1.title, "The Dark Knight"); //Setting title of the movie
strcpy(movie1.director,"Christopher Nolan"); //Setting director of the movie
strcpy(movie1.released,"July 14, 2008"); //Setting release date of the movie
movie1.runsfor = 152; //Setting running time of the movie
displaymovie(movie1); //displaying values of static object
//Setting all fields of dynamically allocated stuct object
strcpy(movie2->title,"The Dark Knight Rises"); //Setting title of the movie
strcpy(movie2->director,"Christopher Nolan"); //Setting director of the movie
strcpy(movie2->released,"July 16, 2012"); //Setting running time of the movie
movie2->runsfor = 165; //Setting running time of the movie
displaymovie(movie2); //displaying values of dynamically allocated object
return 0;
}