#include <iostream> #include <sstream> #include <stdlib.h> #include <string> #in
ID: 3556530 • Letter: #
Question
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int prompt4Size(int, int);
int prompt4Value();
/* meanOfArray prototype using array notation
int meanOfArray(int [], int);
*/
int meanOfArray(const int *, int); // meanOfArray prototype using pointer notation
/* Startup allocation uses a pre-defined constant
const int INIT_ALLOCATED_SIZE = 25;
The run-time user re-allocation uses a local variable inside the main()
*/
// string to integer conversion method
double str2Int(string s) {
int i;
stringstream ss(s); //turn the string into a stream
ss >> i; //convert
return i;
}
void menu(int user_array_size, int allocated_size) {
std::cout << " ----- LAB3A COMMAND MENU ------------- "
<< " 1 - display user_array, size of: " << user_array_size << " "
<< " 2 - modify user_array content "
<< " 3 - modify user_array size (between 1 - " << allocated_size<< ") "
<< " 4 - display mean of user_array "
<< " 5 - INCREASE 4X the program_memory (up to 10000) "
<< " 6 - DECREASE 1/4 the program_memory (down to 1) "
<< " q - quit "
<< " Enter your choice: ";
}
int main()
{
int user_array_size = 10, requested_size=0,highlowcount=0;
int re_allocated_size=0, allocated_size=25;
int *iReallocatePtr=0; // for re-allocation array
int *itPtr, *itPtr2; // traversal pointers
// to manage the dynamic memory allocation
/* Replace all the C++ array subscription in this file with pointer method, e.g.
* int *iUserPtr=0; // to replacing the user_array[]
*/
int *iProgMemPtr = new int[allocated_size];
itPtr = iProgMemPtr; // use the traversal pointer
for(int i=0; i<allocated_size-1; i++)
{*(itPtr+i) = 0;}
*itPtr = -1; // mark the last item
// initialize the user_array as a test driver
for(int i=0; i<user_array_size; i++) *(iProgMemPtr+i) = 100 - i*10;
int location = 0; // command option 2 user entered a location to modify
int input_size = 0; // command option 3 user entered a new array size
// stay sentinel/flag to stay in menu loop!
bool stay = true;
// main menu while loop
while(stay) { // main menu while starts
string choice; // user choice input
string user_input;
int searchPos; // item position
int searchNum; // item value
int location,size,value;
menu(user_array_size, allocated_size);
std::cin >> choice;
std::cin.ignore();
if(choice.size() == 1) {
char ch = choice[0]; // only process the first character user input
switch(ch) { // main menu switch starts
case '1': // display user_array and its size
cout << " The user_array has " << user_array_size
<< " elements. And it contains: ";
for(int i=0; i < user_array_size - 1; i++)
cout << *(iProgMemPtr + i) << ", ";
cout << *(iProgMemPtr + (user_array_size -1)) << " ";
break;
case '2': // modify user_array content
cout << " Enter a location between 1 and " << user_array_size<<": ";
cin >> location;
cout << " The current value is " << *(iProgMemPtr+location-1) << "; ";
value = prompt4Value();
*(iProgMemPtr+location-1) = value;
break;
case '3': // modify user_array size
size = prompt4Size(user_array_size, allocated_size);
re_allocated_size = size;
if(size > user_array_size){
for(int i=user_array_size; i<size;i++)
*(iProgMemPtr+i) = 0;
}
cout << " resize from: " << user_array_size << " to: " << size << endl;
user_array_size = size;
break;
case '4': // mean of the user_array
cout << " The mean of user array (size of "
<< user_array_size << ") is " << meanOfArray(iProgMemPtr, user_array_size) << endl;
break;
case '5': // INCREASE ALLOCATION by 4X
{
/* steps to re-allocate the user array:
* a) allocate new;
* b) tranfer content;
* c) release old;
*/
if(allocated_size*4 > 10000) {
cout << " Can not allocate more, the upper limit reached! ";
break;
}
// step a) allocate new 4X the old allocated
re_allocated_size = allocated_size*4;
iReallocatePtr = new int[re_allocated_size];
// initialize new arrays
itPtr = iReallocatePtr;
for(int i=0; i<re_allocated_size; i++) *itPtr++ = 0;//re_allocated_size -i;
itPtr = iReallocatePtr;
// step b) transfer old to new
itPtr = iReallocatePtr; // traversal pointer for new
itPtr2 = iProgMemPtr; // traversal pointer for old
for(int i=0; i<user_array_size; i++) *itPtr++ = *itPtr2++;
// step c) release old
delete iProgMemPtr;
iProgMemPtr = iReallocatePtr;
allocated_size = re_allocated_size;
cout << " The allocated size is " << allocated_size << endl;
highlowcount = highlowcount + 1;
break;
}
case '6': // DECREASE ALLOCATION by 1/4
{
if(allocated_size/4 < 1) {
cout << " Can not allocate less, the lower limit reached! ";
break;
}
// step a) allocate new 4X the old allocated
re_allocated_size = allocated_size/4;
iReallocatePtr = new int[re_allocated_size];
// initialize new arrays
itPtr = iReallocatePtr;
for(int i=0; i<re_allocated_size; i++) *itPtr++ = 0;//re_allocated_size -i;
itPtr = iReallocatePtr;
// step b) transfer old to new
itPtr = iReallocatePtr; // traversal pointer for new
itPtr2 = iProgMemPtr; // traversal pointer for old
user_array_size = min(user_array_size, re_allocated_size);
for(int i=0; i<user_array_size; i++) *itPtr++ = *itPtr2++;
// step c) release old
delete iProgMemPtr;
iProgMemPtr = iReallocatePtr;
allocated_size = re_allocated_size;
cout << " The allocated size is " << allocated_size << endl;
highlowcount = highlowcount + 1;
break;
}
case 'q': // Sentinel to EXIT the main menu
stay = false;
break;
default:
cout << " Invalid Selection! .... Try again! ";
} // end of switch
} // end of menu for single character input
else
cout << " Only one character command Please! "
<< ".... Try again! ";
} // end of while
}
PROMPT PART
Explanation / Answer
This is what you should put for the user input
// prompt for the user input
int prompt4Size(int user_array_size, int allocated_size) {
string user_input;
cout << " The current size is "
<< user_array_size << "; Enter the new size between 1 and "
<< (allocated_size) << ": ";
cin >> user_input;
cin.ignore();
return str2Int(user_input);
}
int prompt4Value() {
string user_input;
cout << "What is the value? ";
cin >> user_input;
cin.ignore();
return str2Int(user_input);
}
int meanOfArray(const int *array, int array_size) {
double array_sum = 0.00;
for(int i = 0; i < array_size; i++) array_sum += *array++;
return array_sum/array_size;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.