Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Fundamental 2. chapter 9 pointers You will write a program that reads in data from a text file and stores the data in a

Programming Fundamental 2. chapter 9 pointers You will write a program that reads in data from a text file and stores the data in a dynamic array. Your program should then calculate and display the average, median and mode of the data read in. See the sample output at the end of this document. You are provided with the data file and a start cpp file for your program. You are not required to use the starter file and you may make as many changes to the starter file as you would like. However, you must follow the requirements below and your output must match the output at the end of this document when using the provided data file (without hard-coding). Requirements You must use a dynamic array to store the data that you read in from the data file. The data file is a list of integers. The first integer in the list is the count of the integers that follow. Each integer that follows is the number of movies watched by a single student. When you read in the data, it would make since to read in the first number, create a dynamic array of integers that is the size of that number, and then read the remaining integers into your array. You must pass pointers to your functions and functions should perform pointer arithmetic. You are NOT allowed to use index operators, [], anywhere in your program except when creating your dynamic array. You should have at least five functions: - Read data from file - Calculate the average - Calculate the median - Calculate the mode - Sort an array of integers - Swap two integer values You have been provided prototypes for these functions in the starter cpp file. Note: you have been provided with two different funtion prototypes for reading data. You should only use one of these or write your own. The second prototype goes with a definition that is more challenging to write than the first. Make sure you use good programming style. Comment all your variables, function definitions, and any calculations. Make good use of whitespace.

starter file

****************************** Name Date File Name template.cpp Description ********************************/

/*********************************************************** You may make any changes to this code as you would like. Including changing function prototypes and the code in main. Note: There are two overloads of readMovieData(). You only need to define the one that you are going to use in your program. ************************************************************/

// Headers #include #include #include #include #include using namespace std;

// Global variables const string FILENAME = "moviecount.txt";

// Function declarations int readMovieData(int *& movies, string fileName); int readMovieData(int ** movies, string fileName); double calculateAverage(int * movies, int size); double calculateMedian(int * movies, int size); int* calculateMode(int * movies, int size, int& numModes); void sort(int *& movies, int size); void swap(int * a, int * b);

int main() { int numStudents; // number of students who watch movied int * movieCounts = nullptr; // an array of integers to store counts of movies for each student

// Read in the movie data try { //numStudents = readMovieData(movieCounts, FILENAME); // call to first prototype numStudents = readMovieData(&movieCounts, FILENAME); // call to second prototype } catch (const char * message) // if file cannot be opened then catch the thrown exception { cout << message << endl; exit(EXIT_FAILURE); } cout << setprecision(2) << fixed << showpoint; // set decimal places to two for average and median

cout << "Total number of students who watched movies is " << numStudents << endl; cout << "The average number of movies watched by all students is " << calculateAverage(movieCounts, numStudents) << endl; cout << "The median number of movies watched by all students is " << calculateMedian(movieCounts, numStudents) << endl;

int numModes = 0; // number of modes in the list, there could be more than one int * modes = calculateMode(movieCounts, numStudents, numModes); // list of modes is returned and stored in modes cout << "The median number of movies watched by all students is "; if (numModes >= 1) { cout << modes[0]; // output the first mode } for (int i = 1; i < numModes; i++) // if there is more than one, output the remaining modes { cout << ", " << modes[i]; } cout << endl;

// Make sure we place the end message on a new line cout << endl;

// The following is system dependent. It will only work on Windows system("PAUSE");

/* // A non-system dependent method is below cout << "Press any key to continue"; cin.get(); */ return 0; }

/*********************************************************** Reads data in from fileName and stores in movies. movies will point to a dynamic array of integers PARAM: movies is a reference to a pointer to int fileName is a string that contains the name of the file to be read PRE: The file fileName exist in the project folder. The first value in the file is the number of values after the first value POST: movies points to a dynamic array of integers with the size equal to the first value in the file Each element in movies array corresponds to a value in the file NOTE: ************************************************************/ int readMovieData(int *& movies, string fileName) {

}

/*********************************************************** Reads data in from fileName and stores in movies. movies will point to a dynamic array of integers PARAM: movies is a pointer to a pointer to an int fileName is a string that contains the name of the file to be read PRE: movies contains the address of a pointer to an integer The file fileName exist in the project folder. The first value in the file is the number of values after the first value POST: *movies points to a dynamic array of integers NOTE: ************************************************************/ int readMovieData(int ** movies, string fileName) { }

/*********************************************************** Calculates the average of the elements in dynamic array movies PARAM: movies points to a dynamic array size is the number of elements in array pointed to by movies PRE: movies points to an array of size elements POST: average is calculated and the value returned NOTE: ************************************************************/ double calculateAverage(int * movies, int size) { }

/*********************************************************** Calculates the median of the elements in the dynamic array movies PARAM: movies points to a dynamic array size is the number of elements in array pointed to by movies PRE: movies points to an array of size elements POST: median is calcualted and the value returned NOTE: To make it easier to find the center (or two center) values, movies is sorted ************************************************************/ double calculateMedian(int * movies, int size) { }

/*********************************************************** Description PARAM: movies points to a dynamic array size is the number of elements in array pointed to by movies numModes is a reference variable used to return the number of modes found in movies PRE: movies points to an array of size elements POST: Each mode value is stored in a dynamic array of integers with size of size The dynamic array of modes is returned NOTE: std::fill was used to clear the array when a new mode larger than other modes was found To make it easier to determine the number of like values, movies is sorted ************************************************************/ int * calculateMode(int * movies, int size, int & numModes) { }

/*********************************************************** Bubble sort PARAM: movies points to a dynamic array size is the number of elements in array pointed to by movies PRE: movies points to an array of size elements POST: movies is sorted in ascending order NOTE: ************************************************************/ void sort(int *& movies, int size) { }

/*********************************************************** Swap to values PARAM: a is a pointer to an int b is a pointer to an int PRE: a and b point to valid memory locations POST: the values that a and b point to are swapped NOTE: ************************************************************/ void swap(int * a, int * b) { }

moviecount.txt file

28 14 16 19 6 9 47 43 28 35 16 30 26 47 12 14 47 41 16 44 22 20 31 45 34 31 44 4 28

notes please read the question. IT is asking for a specific way to allocate the memory and also it is asking to perform pointer arithmetic and not to use the index[] operators.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago