Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

main – Prompt the user for the initial number of array elements to allocate. Thi

ID: 3879532 • Letter: M

Question

main – Prompt the user for the initial number of array elements to allocate. This number must be between 100 and 300 in increments of 50 (100, 150, 200, …), and is stored as an integer. If the user enters a value outside these specifications, print an error message and exit with code 1 - e.g. exit(1); • Dynamically allocate an array with the number of elements requested by the user, by calling the function makeArray (described below). After allocating the array, write to stdout and the output file (out.txt) a line of the form (see sample output file):

Open input file nums.txt (must be so named and you must use fstream) and read the integers in that file (one integer per line). ** Process the file as you go. You may not read it more than once ** • Anytime the count of the items inserted into the array exceeds 95% of its allocated size (cast this value as an int), call the calcAvg function and output the average of the integers read up to that point (as a double); and invoke function makeArray to increase the size of the array by 50%, up to a maximum of 600 elements. In no case should you allocate more than 600 elements. Example: i. The user asks for 200 initial elements. Upon inserting the 191st item (which exceeds 0 .95 * 200 = 190), calculate and output the average of the integers read so far, and invoke the function to make the array size 300 (200 * 150%). The output at this stage should be in the form: Array size now X - Average so far is YY.ZZ ii. Continue to process the file. Upon inserting the 286th item (which exceeds 0.95 * 300 = 285), repeat this process, to create an array of size 450 (300 * 150%). iii. Repeat this process until the file has no more integers – You are guaranteed to have fewer than 570 integers in the file (so as to prevent any expansion beyond 600 elements). • The number of integers read, and the calculated average must be output before exiting the program: X integers were read and the average was YYY.ZZZ • main should deallocate any remaining dynamically allocated memory prior to exiting.

b) makeArray – This function allocates memory for the larger array, deallocates the memory for the dynamic array being expanded, and returns a pointer to the new larger array. It is invoked to create the initial array, as well as to allocate the new, larger array, copy the items into it, and return a pointer to the calling code. Its details follow: • Input parameters: i. A pointer to a dynamic array of integers (the array being outgrown) or NULL/nullptr ii. An integer representing the size of the array. iii. A multiplier (double) which is applied to the value in ii above to arrive at the final size of the new array. • Processing and output– This function must be able to handle calls to create the initial array, as well as to expand the array. It should be invoked with a NULL pointer initially (there is not yet an array in existence), along with array size, and a multiplier of 1.0. In subsequent calls it should receive as inputs the pointer to the array being expanded, its size, and a multiplier (how much larger to make the array – 150% in this case). It should copy the data over to the larger array, deallocate the small array, and return an integer pointer to the new array. It should also enforce the maximum array size (600 elements).

c) calcAvg – This function accepts as input parameters the dynamic array (integer pointer), and the number of values that have been read into it (integer), and returns the calculated average (as a double).

The OUTPUT SHOULD LOOKS LIKE THIS:

ALLOCATED 200 ELEMENTS

ARRAY SIZE IS NOW 300 - AVERAGE SO FAR IS 288.183

ARRAY SIZE IS NOW 450 - AVERAGE SO FAR IS 351.79

ARRAY SIZE IS NOW 600 - AVERAGE SO FAR IS 262.713

500 INTEGERS WERE READ AND AVERAGE WAS 229.742

Explanation / Answer


Given below is the code for the question. You have not provided the input file nums.txt. I test it with a small file with 10 numbers and works fine. Please try with your file and let me know if any issues.
Please do rate the answer if it was helpful. Thank you


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int* makeArray(int *arr, int &capacity, double multiplier);
double calcAvg(int *arr, int size);

int main()
{
ifstream infile("nums.txt");
if(infile.fail())
{
cout << "Error: Could not open input file nums.txt" << endl;
exit(1);
}
  
int capacity, size = 0 ;
  
cout << "Enter the initial capacity of the array between 100-300: ";
cin >> capacity;
  
while(capacity < 100 || capacity > 300)
{
cout << "Enter a value in range 100-300: ";
cin >> capacity;
}
  
int *arr = NULL, num;
arr = makeArray(arr, capacity, 1.0);
cout << "ALLOCATED " << capacity << " ELEMENTS" << endl;
  
cout << fixed << setprecision(3);
while(infile >> num)
{
arr[size++] = num;
if(size > 0.95 * capacity)
{
arr = makeArray(arr, capacity, 1.5);
cout << "ARRAY SIZE IS NOW " << capacity << " - AVERAGE SO FAR IS " << calcAvg(arr, size) << endl;
}
}
infile.close();
cout << size << " INTEGERS WERE READ AND AVERAGE WAS " << calcAvg(arr, size) << endl;
return 0;
}

int* makeArray(int *arr, int &capacity, double multiplier)
{
int newCapacity= capacity * multiplier;

if(newCapacity > 600)

newCapacity = 600;
int *newArr = new int[newCapacity];
  
if(arr != NULL)
{
for(int i = 0; i < capacity; i++)
newArr[i] = arr[i];
}
  
//deallocate old array
delete []arr;
capacity = newCapacity;
return newArr;
}

double calcAvg(int *arr, int size)
{
double avg = 0;
for(int i = 0; i < size; i++)
{
avg += arr[i];
}
  
if(size != 0)
avg /= size;
return avg;
}