File(s) to be submitted: proj1.cpp***** This assignment must correctly compile and run on the ranger system using the g++ compiler *****Objectives: 1) Use pointers; 2) Dynamically allocate memory; 3) Read and write files; 4) Use value-returning functions.Project description:In this assignment you will have the opportunity to recall what youve previously learned about pointers, value returningfunctions, and file processing. You will write a small program that simulates just in time memory allocation, bydynamically allocating an array, and then reallocating it as the size of the input reaches the arrays capacity.Requirements:1. Your program must contain the three functions listed below, in a single file named proj1.cpp:a) main This function must appear first in your project cpp file, and will perform the following tasks: Read an input file (must be named nums.txt) having the following format:i. First line: An integer value representing the initial size of the arrayii. Second line: A floating value that is the percentage by which the array should be expandedwhen its capacity is reached (i.e., if a 25% expansion is desired, this value would be 0.25).iii. Remainder of the file: A set of integers (one per line) to be read into the array Validate number of array elements to allocate: This number must be between 100 and 350 and must bein increments of 50 (100, 150, 200, 250, ). If the value is outside these specifications, print the wordError and exit the program. If a valid array size is read, continue as shown below. Dynamically allocate an array with the number of elements specified, by calling the functionallocateArray (described below). After allocating the array, write to stdout and to the output file (mustbe named out.txt) the number of elements allocated. Read the remaining integers from the input file (one at a time), and add each to the array. ** You maynot read the input file more than once and you may not read the values into a container to processlater. You must process the file as you go.** Each time the array capacity is reached, call the calcAvg function and print (to stdout and the outputfile) the count of elements, and the average of the integers read up to that point (as a double); andinvoke function allocateArray to increase the size of the array by the factor specified in the input file,and continue reading the integers in the input file until all input has been processed. After the last integer in the file is read and placed into the array, output the following to stdout and theoutput file: the size of the array, the number of integers read, and the average of those integers. Example (assume lines one and two of the input file are 200 and 0.25 respectively):i. Allocate an array with 200 elements, and output this value to stdout and to the output file.ii. Once you have read and inserted the 200th integer into the array, output the array size, countof items read, and the average of the values read so far, and invoke allocateArray to expand thearray size by 25% The new size is 250 (200 * 1.25).iii. Continue to process the file. Upon inserting the 250th item (reaching the new capacity of thearray), repeat ii. above, to create an array of size 312 (250 * 1.25 truncated).iv. Repeat this process until the input file has no more integers. Each time the capacity is reachedprint the number of elements and average, and expand the array.v. Once the end of file is reached, print the array size (it will likely be greater than the number ofintegers read in), the number of elements read, and the average of those numbers. Prior to exiting the program, deallocate all dynamically allocated memory. You may not rely on programtermination for memory deallocation.b) allocateArray This function dynamically allocates memory for an array, and when necessary copies elementsfrom the old array to the new and deallocates the memory of the array being expanded. It returns a pointer tothe newly allocated array. Its details follow: Input parameters:i. A pointer to a dynamic array of integers (the array being outgrown) or NULL/nullptrii. A pointer to an integer representing the size of the array.iii. A double indicating the percentage amount (expressed as a decimal) by which the array shouldbe expanded (50% would be expressed as 0.5). Processing and output This function must be able to handle calls to create the initial array, as well as toexpand an existing array. In the initial case, it should be invoked with a NULL pointer initially (there isnot yet an array in existence), along with a pointer to the (initial) desired array size. In subsequent callsthe function should allocate a new array, larger by the percentage factor of the parameter). The newsize should be cast as int to truncate the fractional part. It should also copy the existing data to the newarray, deallocate the old array, update the size of the array, and return the integer pointer to the newlyallocated array.c) calcAvg This function accepts as input parameters the dynamic array (an int pointer), and the number ofvalues that have been read into it (an int), and returns the calculated average (as a double).2. Your output format MUST MATCH EXACTLY the output in this specification this means the verbiage, line spacing,character spacing (do not use tabs only a single space between elements) the actual values will vary, based on thecontents of the input file. Use cout unformatted for all output. The output below is from processing the sample outputfile provided with the assignment specification:3. Test your program Use different input files and choose different starting array sizes and expansion factors. Seerubric section.4. Code comments Add the following comments to your code: A section at the top of the source file(s) with the following identifying information:Your NameCSCI 3110-00X (your section #)Project #XDue: mm/dd/yy Below your name add comments in each file that gives an overview of the program or class. Place a one or two-line comment above each function that summarizes the purpose of the function.
5. RubricRequirement Points Offmain: Input filename -5main: Output filename -5main: Validation of initial array size -10main: Initial array allocation (allocated from function call) -10main: File processing loop -10main: Subsequent array allocation (timing and correctness of call) -10main: Intermediate averages (timing and correctness of call) -5main: All required output to stdout and file -10main: Deallocate memory -5 (each time)allocateArray: Initial allocation and return -5allocateArray: Subsequent allocation (new size and correctness) -5allocateArray: Subsequent allocation (copy) -10allocateArray: Subsequent allocation (deallocation) -5allocateArray: Subsequent allocation (modified and returned values) -10calcAvg: Correctness and return -10general: Function signatures per specification (name, return type, number/type of parameters) -5 (per item)general: Does not compile (on ranger using Linux g++ compiler) -25general: Other TBD
Reviews
There are no reviews yet.