CSCI3110 Project 1: Dynamic Memory Allocation Solved

35.00 $

Category:

Description

5/5 - (2 votes)

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 you’ve 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 array’s 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).
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 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
not read the input file more than once and you may not read the values into a container to process
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. 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, count
of items read, and the average of the values read so far, and invoke allocateArray to expand the
array 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 the
array), 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 reached
print 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 of
integers 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 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. A pointer to a dynamic array of integers (the array being outgrown) or NULL/nullptr
ii. 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 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:
3. Test your program – Use different input files and choose different starting array sizes and expansion factors. See
rubric 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 Name
CSCI 3110-00X (your section #)
Project #X
Due: 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. Rubric
Requirement Points Off
main: Input filename -5
main: Output filename -5
main: Validation of initial array size -10
main: Initial array allocation (allocated from function call) -10
main: File processing loop -10
main: Subsequent array allocation (timing and correctness of call) -10
main: Intermediate averages (timing and correctness of call) -5
main: All required output to stdout and file -10
main: Deallocate memory -5 (each time)
allocateArray: Initial allocation and return -5
allocateArray: Subsequent allocation (new size and correctness) -5
allocateArray: Subsequent allocation (copy) -10
allocateArray: Subsequent allocation (deallocation) -5
allocateArray: Subsequent allocation (modified and returned values) -10
calcAvg: Correctness and return -10
general: Function signatures per specification (name, return type, number/type of parameters) -5 (per item)
general: Does not compile (on ranger using Linux g++ compiler) -25
general: Other TBD

  • proj1-nplykj.zip