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.

Programming Exercise

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:

  • Ask the user to enter how many movies they wish to process. The number specified by the user will be used for the dynamic memory allocation.
  • Allocate memory for an array of the MovieData structs, using the size value from the user input.
  • Populate the array with input from the user.
  • Display the array contents.
  • Find the LONGEST movie in the array (the movie with the longest runningTime value).
  • Output the complete data record for the movie with the longest running time (as shown in the Sample Output section of this document).
  • Before the program exits, it should delete the dynamically allocated array of MovieData structs.

Program Design

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);

Function: displayMovie

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.

Function: populateMovieDataArray

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.

Function: displayMovieDataArray

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:

  • Display the hexadecimal address of each array element (that is, each struct in the array),
  • Call the displayMovie function to display the details about the movie.

(Observe the Sample Output section of this document as an example of the formatting.)

Function: findLongestMovie

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.

Sample Output

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.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.