Question: I need to read from a file and store the contents from the file into an array, then use these functions: Side not also i
I need to read from a file and store the contents from the file into an array, then use these functions:
Side not also i CANT use vectors either, i know its a drag but i was told it be way too easy with vectors.
int * resize (int * list , int& capacity , int increaseAmount )
Where list will be the dynamic array, capacity is the maximum number of elements that can be stored in the array and increaseAmount will be the amount we will increase the array, we will also need to update the capacity with an updated amount, the way the function will be used in main will be
list = resize (list, capacity, increaseAmount );
Basically you pass in the array into the function, and then you will copy list into a new array of larger size and you will increment capacity by increaseAmount, you will have to deallocate list within the resize function which would cause list to be a dangling pointer temporarily but then it will be assigned a new array which will fix that problem
In your program, you will do the following
Prompt the user for the input file, you will keep re-prompting if the input file does not exist
Prompt the user for an increase amount (when resizing the array), keep re-prompting if the user enters
a invalid number or a number less than or equal to 0
You will size the array by the increase amount (use the resize function)
You will read a number from the file, if the content on the file is not a number, you will skip it (use
in.clear() and in.ignore(100, )), once the array is full, resize the array by increaseAmount
by calling resize function
Once all the elements have been read and stored into the array, you will sort the array and output the
following
The amount of integers in the file, i.e. the size of the array
Max element in the file
Min element in the file
The median (the size could be even or odd so make sure you compute the median correctly)
The mean
The list of all prime numbers (sorted)
The amount of wasted memory locations (capacity - size)
In order to sort the array, you will use the sort function which is part of the #include , the way you will sort the array is by writing
sort ( list , list + size );
Where size will be the number of actual elements in your array, size is not the capacity
Example Run
Enter input filename : Randomfile. txt
Input file not found
Enter input filename : Input . txt
Please enter the amount to resize the array : -15
Please enter the amount to resize the array : T
Please enter the amount to resize the array : 10
Num count : 10013
Min : 0
Max : 10000
Median : 4983.00
Mean : 4972.14
List of sorted prime numbers
5 7 11 13 23 23 31 31 31 41
41 41 41 47 47 47 53 53 59 59
67 73 83 97 103 107 127 131 139 149
149 149 151 151 157 163 167 181 181 191
191 197 199 211 223 227 227 229 251 257
263 269 269 271 271 271 281 281 281 283
307 311 313 313 317 331 337 337 347 349
353 353 367 367 373 379 383 383 383 389
397 397 419 431 439 439 449 461 463 463
487 499 503 509 509 521 541 547 547 547
Unused elements : 7
What I currently have so far, its embarassing:
#include
#include
#include
using namespace std;
int* resize(int * list, int& capacity, int increaseAmount)
{
if(list == NULL)
{
capacity = capacity + increaseAmount;//Update Capacity
// allocate Array of length capacity
// return pointer to the array
else
{
// allocate an array of larger size
// deep copy of list into larger array
// update capacity
// return pointer that points to the larger array
}
}
}
int main()
{
ifstream input;
string filename;
int x;
cout<<"Enter the file name"<< endl;
cin>>filename;
input.open(filename.c_str());
while(input.fail())
{
input.clear();
cout<<"Incorrect filename, please enter again";
cin>>filename;
input.open(filename.c_str());
}
cout << "Please enter the amount to resize the array:" << endl;
cin >> x;
while(x <= 0)
{
cout << "Please enter the amount to resize the array:" << endl;
cin >> x;
}
The input file is massive so i couldnt add it sorry.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
