.ZIP

# Assignment # 6 (Chapter 10, Page 702, #7 -with modifications)

Contents

Introduction

Approach 1 - Use "regular pointers"

Approach 2 - use Smart Pointers

Sample output from my version of Assignment 5

Sample program for this week - Problem #1, page 701

Solution with Regular pointers

Code for mtolappa_week11_sample_regular_pointers.cpp

Sample program's adherence to Programming Standards Document

Output from sample program

Solution with Unique Pointers (Smart pointers)

Code for mtolappa_week11_sample_unique_pointers.cpp

Sample program's adherence to Programming Standards Document

Output from sample program

Introduction
The purpose of this assignment is to provide you with experience in working with pointers and dynamic memory allocation. The problem description is as follows. Additional specifications are shown in blue text.

This assignment is not implemented using classes. It is written in a single program

7. Movie Statistics

Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average of the values entered.

Create the following functions:

● printArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should print out the contents of the array.

● getAverage. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the average as a double value.

● sortArray. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It sorts the array sent into it and returns nothing.

● getMaximum. This function should take in the dynamically created array (i.e. pointer to dynamically created array) and the size of the array as parameters. It should return the maximum value within the array as an int value.

Make sure that user enters positive numbers for # of students and number of movies seen by each student

Note that you only have to calculate average, not median and mode of the values entered.

This assignment does not require you to create classes. Code the assignment in a single cpp file.

Approach 1 - Use "regular" pointers
In this approach, an integer pointer variable is declared. An array (sized at the number entered by the user) is created and the memory address of this array is placed in the pointer variable. With this approach, remember to deallocate the dynamically created array.

Approach 2 - use Smart Pointers
You may use unique_ptr to declare a smart pointer to hold the address of the dynamically created array. The syntax to declare such a pointer and to set it to point to an array is as follows:

1
2
unique_ptr<int[] dynamic_array;
dynamic_array = make_unique<int[](5);

The statements shown above create a 5 element array dynamically and place the address of this array in the dynamic pointer. In this assignment, a variable is used instead of the numeric literal "5".

When using smart pointer, remember the following 2, very important, issues:

● Smart points MUST be passed into function by reference

● Dynamic arrays managed by unique pointer do not have to be "deleted".

Sample output from my version of Assignment 5
User input is shown in bold, blue, highlighted text.

The main function's pseudocode is as follows:

In int main

● ask user for number of student to be surveyed

● ensure that the number is 0 (Do not check for non-numeric input, test with numbers)

● Setup dynamic array

● ask user to enter movies seen by each of the students

● ensure that number of movies seen by each student is 0 (Do not check for non-numeric input, test with numbers)

● Invoke these functions:

● printArray function, passing in the appropriate arguments

● sortArray function, passing in the appropriate arguments

● printArray functions after the sortArray function has been invoked

● getAverage function, passing in the appropriate arguments, and print out the value returned by this function

● getMaximum function, passing in the appropriate arguments, and print out the value returned by this function