The goal of this lab is to help you get familiar with C++ structs. It is also an opportunity to practice writing and debugging a program with multiple functions. Be sure that you read this entire document before you being coding.
1. Define a struct named MovieData to store the following information about a movie:
Field Name | Data Type | Description |
title | string | Movie Title |
director | string | Movie Director |
yearReleased | int | Year Released |
runningTime | double | Running time in minutes |
2. Write a program that uses dynamic memory allocation to create an array of MovieData structs, populates the array with data, and does some processing with that data:
Your program must have functions, as described on the next page of this document. the main function dynamically allocates the array of MovieData structs, and then calls other functions to perform the various tasks.
The list of function prototypes shown below provides guidance about how you must organize your program.
void displayMovie(MovieData *moviePtr);
void populateMovieDataArray(MovieData *arrayPtr, int arraySize);
void displayMovieDataArray(MovieData *arrayPtr, int arraySize);
MovieData *findLongestMovie(MovieData *arrayPtr, int arraySize);
This function has one input parameter:
moviePtr = address of a MovieData struct
The MovieData struct (pointed to by moviePtr) contains information about a particular movie. This function must output the data fields of the MovieData struct to cout, in a format similar to the examples in the Sample Output section of this document.
This function has two input parameters:
arrayPtr = address of beginning of array
arraySize = number of elements in the array.
The populateMovieDataArray function must contain a loop that prompts the user to enter data for each field of each struct in the array.
This function has two input parameters:
arrayPtr = address of beginning of array
arraySize = number of elements in the array.
The displayMovieDataArray function must contain a loop that displays the array contents, formatted as follows:
(Observe the Sample Output section of this document as an example of the formatting.)
This function has two input parameters:
arrayPtr = address of beginning of array
arraySize = number of elements in the array.
The findLongestMovie function must contain a loop that scans the array to identify the array element with the longest runningTime value.
The findLongestMovie function must return a pointer to the array element for the longest movie.
The table below contains sample output for the solution to this lab exercise. In this example, we have indicated which text the user types by showing it in a larger, bold font. In actuality, all text would appear in the same size font, with no bold characters
Enter desired array size: 4
arrayPtr = 00000283424A49E8
Enter Title 0: 2001: A Space Odyssey
Enter Director 0: Stanley Kubrick
Enter Year Released 0: 1968
Enter running time (minutes) 0: 142
Enter Title 1: Finding Nemo
Enter Director 1: Andrew Stanton
Enter Year Released 1: 2002
Enter running time (minutes) 1: 100
Enter Title 2: The Sound of Music
Enter Director 2: Robert Wise
Enter Year Released 2: 1965
Enter running time (minutes) 2: 174
Enter Title 3: Hidden Figures
Enter Director 3: Ted Melfi
Enter Year Released 3: 2016
Enter running time (minutes) 3: 127
00000283424A49E8: arrayPtr[0] =
Title : 2001: A Space Odyssey
Director : Stanley Kubrick
Released : 1968
Running Time: 142 minutes
00000283424A4A48: arrayPtr[1] =
Title : Finding Nemo
Director : Andrew Stanton
Released : 2002
Running Time: 100 minutes
00000283424A4AA8: arrayPtr[2] =
Title : The Sound of Music
Director : Robert Wise
Released : 1965
Running Time: 174 minutes
00000283424A4B08: arrayPtr[3] =
Title : Hidden Figures
Director : Ted Melfi
Released : 2016
Running Time: 127 minutes
Longest Movie in list:
Title : The Sound of Music
Director : Robert Wise
Released : 1965
Running Time: 174 minutes
Longest Movie is: 174 minutes long
DELETING array at arrayPtr = 00000283424A49E8
Exit the program.