Need help with finishing the contents of my main() Here is what i have at the mo
ID: 3737214 • Letter: N
Question
Need help with finishing the contents of my main()
Here is what i have at the moment and i cant use vectors so keep that in mind.
#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
list = new int[capacity];
// return pointer to the array
return list;
}
else
{
int * temp;
temp = new int [capacity + increaseAmount];
for(int i = 0; i < capacity; i++)
{
temp[i] = list[i];
}
capacity += increaseAmount;
delete [] list;
list = temp;
temp = NULL;
return list;
}
}
}
int main()
{
ifstream input;
string filename;
int resizeAmt;
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 >> resizeAmt;
while(resizeAmt <= 0)
{
cout << "Please enter the amount to resize the array:" << endl;
cin >> resizeAmt;
}
//size the array by the increase amount (use the resize function)
/*
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 this
//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)
sort ( list , list + size );
//The amount of wasted memory locations
capacity - size;
Desired Output:
Enter input filename : Input . txt
Please enter the amount to resize the array : -9
Please enter the amount to resize the array : V
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
557 557 569 571 593 593 599 607 613 617
619 631 641 643 659 659 661 701 709 709
709 727 751 757 769 787 797 809 809 809
809 829 853 857 857 859 859 877 883 907
911 911 911 929 941 947 947 953 953 977
977 991 991 991 997 997 1013 1019 1021 1031
1033 1033 1033 1039 1049 1051 1051 1061 1061 1063
1069 1091 1097 1103 1103 1117 1123 1123 1153 1163
1163 1181 1187 1213 1217 1229 1229 1231 1249 1259
1277 1289 1297 1297 1297 1297 1303 1307 1319 1321
1321 1327 1327 1361 1373 1373 1381 1381 1409 1409
1429 1433 1439 1447 1447 1447 1447 1451 1453 1481
2
1481 1481 1487 1493 1493 1499 1523 1531 1531 1531
1543 1543 1543 1549 1549 1567 1567 1567 1597 1621
1627 1627 1627 1627 1667 1667 1669 1693 1697 1697
1699 1709 1733 1733 1741 1741 1747 1753 1753 1759
1777 1777 1789 1801 1801 1801 1823 1823 1861 1867
1871 1873 1873 1877 1879 1889 1889 1901 1907 1913
1931 1949 1949 1949 1951 1979 1979 2039 2053 2069
2081 2081 2083 2087 2087 2087 2113 2113 2131 2131
2137 2137 2141 2141 2143 2153 2153 2179 2203 2203
2221 2221 2239 2251 2267 2281 2287 2297 2309 2309
2309 2333 2333 2333 2333 2339 2339 2341 2341 2341
2351 2357 2381 2383 2389 2389 2399 2411 2423 2441
2441 2441 2441 2473 2477 2477 2521 2531 2539 2539
2539 2539 2539 2543 2551 2557 2617 2617 2621 2633
2647 2647 2657 2671 2687 2687 2689 2689 2693 2693
2699 2699 2711 2711 2711 2731 2731 2791 2791 2791
2797 2797 2801 2803 2803 2803 2803 2819 2833 2837
2857 2887 2897 2897 2903 2903 2909 2909 2917 2927
2957 2963 2963 2963 2969 2969 3001 3001 3011 3011
3019 3019 3061 3061 3079 3083 3089 3109 3121 3137
3167 3181 3191 3209 3209 3217 3217 3221 3257 3257
3259 3271 3299 3299 3299 3301 3313 3313 3313 3313
3319 3323 3329 3331 3343 3359 3359 3361 3373 3373
3389 3449 3457 3457 3457 3457 3463 3463 3469 3469
3469 3511 3517 3517 3529 3539 3541 3547 3547 3547
3559 3559 3571 3581 3581 3593 3607 3607 3607 3613
3623 3623 3643 3659 3659 3673 3677 3691 3691 3697
3709 3709 3709 3709 3719 3719 3727 3727 3733 3739
3739 3767 3767 3769 3779 3797 3803 3803 3803 3821
3821 3823 3823 3823 3833 3833 3847 3863 3881 3907
3911 3917 3919 3919 3923 3929 3931 3967 3967 3989
3989 4001 4003 4019 4019 4027 4027 4051 4051 4057
4073 4073 4073 4079 4079 4079 4099 4099 4111 4127
4133 4133 4139 4139 4139 4153 4157 4157 4159 4159
4159 4217 4219 4219 4229 4229 4229 4271 4271 4273
4273 4283 4283 4283 4297 4337 4337 4349 4357 4357
4363 4373 4391 4397 4397 4397 4421 4421 4423 4441
4441 4447 4451 4457 4457 4463 4463 4513 4513 4519
4519 4523 4547 4549 4549 4567 4583 4597 4643 4649
4651 4651 4651 4663 4673 4673 4703 4703 4721 4723
4723 4729 4729 4729 4733 4759 4783 4783 4787 4789
4789 4793 4799 4871 4889 4889 4889 4903 4919 4919
4919 4931 4931 4943 4951 4957 4957 4967 4967 4969
4987 4999 5003 5003 5003 5003 5009 5009 5021 5039
5039 5051 5059 5081 5081 5081 5081 5087 5099 5101
5101 5107 5113 5147 5147 5147 5153 5153 5153 5153
5153 5167 5179 5179 5189 5189 5209 5209 5209 5227
5231 5237 5261 5261 5279 5279 5279 5279 5297 5303
5309 5323 5323 5323 5333 5333 5333 5351 5351 5387
5399 5413 5413 5417 5417 5417 5419 5431 5431 5437
5437 5441 5441 5443 5449 5449 5449 5471 5477 5477
5479 5483 5483 5483 5483 5501 5519 5519 5527 5531
5531 5531 5557 5569 5623 5623 5641 5647 5683 5693
5693 5737 5741 5741 5741 5741 5743 5743 5743 5749
3
5749 5779 5779 5791 5791 5801 5807 5807 5813 5813
5821 5827 5827 5839 5839 5843 5843 5843 5843 5849
5849 5849 5849 5857 5867 5869 5881 5923 5923 5953
5981 5987 6011 6037 6047 6053 6053 6067 6079 6089
6089 6091 6113 6113 6113 6121 6121 6121 6173 6197
6203 6203 6211 6217 6221 6229 6229 6229 6229 6247
6247 6257 6257 6257 6263 6269 6271 6287 6287 6299
6299 6311 6317 6317 6323 6337 6353 6353 6353 6359
6359 6361 6361 6373 6373 6373 6373 6373 6379 6379
6389 6389 6421 6427 6449 6449 6449 6451 6469 6473
6473 6481 6521 6521 6547 6551 6563 6571 6577 6577
6581 6581 6607 6637 6653 6661 6673 6679 6679 6689
6691 6709 6709 6719 6719 6719 6737 6761 6763 6781
6791 6793 6823 6823 6827 6827 6827 6827 6829 6841
6857 6857 6863 6863 6863 6869 6871 6883 6899 6899
6907 6949 6959 6959 6961 6967 6967 6977 6997 6997
7013 7013 7019 7027 7027 7027 7039 7039 7043 7043
7103 7103 7109 7127 7127 7127 7151 7177 7177 7187
7187 7187 7187 7193 7193 7193 7207 7211 7213 7213
7237 7243 7283 7297 7307 7309 7321 7331 7331 7333
7333 7351 7369 7411 7417 7433 7477 7487 7507 7517
7523 7529 7541 7547 7561 7573 7573 7573 7577 7583
7589 7589 7591 7591 7639 7669 7673 7699 7703 7703
7717 7727 7727 7757 7759 7817 7829 7867 7873 7933
7951 7951 7951 7951 7993 8009 8009 8011 8011 8017
8039 8059 8069 8069 8081 8087 8089 8093 8093 8101
8111 8111 8111 8117 8117 8161 8179 8191 8219 8219
8221 8231 8293 8297 8317 8317 8329 8387 8419 8419
8423 8429 8447 8447 8501 8501 8501 8501 8501 8501
8513 8527 8543 8543 8563 8563 8573 8573 8597 8609
8623 8641 8647 8647 8663 8663 8669 8669 8669 8681
8681 8689 8693 8693 8707 8707 8713 8719 8719 8737
8741 8741 8741 8753 8761 8779 8783 8783 8803 8803
8819 8831 8839 8861 8863 8863 8887 8893 8923 8929
8933 8941 8951 8951 8951 8963 8963 8971 8999 8999
9001 9007 9007 9011 9013 9029 9049 9049 9059 9067
9091 9091 9103 9127 9127 9127 9133 9137 9157 9157
9157 9181 9187 9187 9199 9203 9209 9221 9227 9227
9239 9241 9257 9281 9281 9283 9293 9311 9319 9337
9343 9349 9371 9377 9391 9391 9397 9403 9403 9413
9421 9439 9461 9467 9479 9491 9491 9497 9497 9497
9511 9511 9521 9533 9533 9547 9547 9613 9619 9623
9623 9623 9629 9629 9631 9649 9649 9649 9661 9661
9677 9679 9679 9689 9721 9733 9739 9739 9743 9749
9749 9767 9767 9767 9769 9769 9781 9791 9791 9791
9803 9817 9829 9833 9833 9923 9923 9929 9931 9941
9949 9973
Unused elements : 7
Explanation / Answer
here is your modified program : ------------->>>>>>>>
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
int* resize(int * list, int& capacity, int increaseAmount)
{
if(list == NULL)
{
capacity = capacity + increaseAmount;//Update Capacity
// allocate Array of length capacity
list = new int[capacity];
// return pointer to the array
return list;
}else{
int * temp;
temp = new int [capacity + increaseAmount];
for(int i = 0; i < capacity; i++){
temp[i] = list[i];
}
capacity += increaseAmount;
delete [] list;
list = temp;
temp = NULL;
return list;
}
}
bool isPrime(int n){
for(int i = 2;i<n;i++){
if(n%i == 0){
return false;
}
}
return true;
}
int main()
{
ifstream input;
string filename;
int resizeAmt;
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 >> resizeAmt;
while(resizeAmt <= 0){
cout << "Please enter the amount to resize the array:" << endl;
cin >> resizeAmt;
}
string str;
char *p;
int data;
int size = 0;
int capacity = 0;
int *list = resize(NULL,capacity,resizeAmt);
input>>str;
while(!input.eof()){
data = strtol(str.c_str(),&p,10);
//for checking it is digit or not
if(*p == 0){
list[size] = data;
size++;
}else{
}
if(size == capacity){
list = resize(list,capacity,resizeAmt);
}
input>>str;
}
//size the array by the increase amount (use the resize function)
/*
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
*/
sort(list,list+size);
//Once all the elements have been read and stored into the array, you will sort the array and output this
//The amount of integers in the file, i.e. the size of the array
cout<<" NUM COUNT : "<<size;
//Max element in the file
cout<<" MAX : "<<list[size];//
//Min element in the file
cout<<" MIN : "<<list[0];//
//The median (the size could be even or odd so make sure you compute the median correctly)
if(size%2 == 0){
cout<<" MEDIAN : "<<(double)(list[size/2] + list[(size/2)+1])/2.0;
}
else{
cout<<" MEDIAN : "<<list[(int)(size/2) + 1];
}//
//The mean
long long int total = 0;
for(int i = 0;i<=size;i++){
total += list[i];
}
cout<<" MEAN : "<<((double)total/(double)(size));//
//The list of all prime numbers (sorted)
cout<<" List Of sorted prime numbers : ";//
for(int i = 0;i<=size;i++){
if(isPrime(list[i])){
cout<<" "<<list[i];
}
}
//The amount of wasted memory locations
cout<<" Unused elements "<<(capacity - size);
}