1. Engineering
  2. Computer Science
  3. c...

Question: c...

Question details
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 returning functions, and file processing. You will write a small program that simulates just in time memory allocation, by dynamically 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 array ii. Second line: A floating value that is the percentage by which the array should be expanded when its capacity is reached (i.e., if a 25% expansion is desired, this value would be 0.25) ii. 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 be in increments of 50 (100, 150, 200, 250, .) If the value is outside these specifications, print the word Error 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 function allocateArray (described below). After allocating the array, write to stdout and to the output file (must be 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 may later. 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 output file) the count of elements, and the average of the integers read up to that point (as a double); and invoke 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 the output 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. ii. Allocate an array with 200 elements, and output this value to stdout and to the output file Once you have read and inserted the 200th integer into the array, output the arFay size count of items read, and the average of the values read so far, and invoke allocate Array to expand the array size by 25%-The new size is 250 (200 * 1.25) ii. Continue to process the file. Upon inserting the 250th item (reaching the new capacity of the array), repeat ii. above, to create an array of size 312 (250 125 truncated). Repeat this process until the input file has no more integers. Each time the capacity is reached print the number of elements and average, and expand the array. iv.
Once the end of file is reached, print the array size (it will likely be greater than the number of integers read in), the number of elements read, and the average of those numbers. v. Prior to exiting the program, deallocate all dynamically allocated memory. You may not rely on program termination for memory deallocation. b) allocateArray This function dynamically allocates memory for an array, and when necessary copies elements from the old array to the new and deallocates the memory of the array being expanded. It returns a pointer to the newly allocated array. Its details follow .Input parameters i. ii. ii. A A pointer to a dynamic array of integers (the array being outgrown) or NULL/nullptr A pointer to an integer representing the size of the array double indicating the percentage amount (expressed as a decimal) by which the array should be 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 to expand an existing array. In the initial case, it should be invoked with a NULL pointer initially (there is not yet an array in existence), along with a pointer to the (initial) desired array size. In subsequent calls the function should allocate a new array, larger by the percentage factor of the parameter). The new size should be cast as int to truncate the fractional part. It should also copy the existing data to the new array, deallocate the old array, update the size of the array, and return the integer pointer to the newly allocated array c) calcAvg-This function accepts as input parameters the dynamic array (an int pointer), and the number of values 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 the contents of the input file. Use cout unformatted for all output. The output below is from processing the sample output file provided with the assignment specification: 200 301.075 250 335.604 312 350.266 390 285.554 87 234.848 08 585 252.32
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution