You will create an array manipulation program that allows the user to do pretty
ID: 3861900 • Letter: Y
Question
You will create an array manipulation program that allows the user to do pretty much whatever they want to an array.
Present the user with a menu, detect their choice, and provide them any needed follow up prompts that are needed.
When the program begins, you will prompt the user for an initial size of the array, then the values to fill the array. Your array will contain ints. For example, the user could say they want an array of size 5, then you prompt for the 5 values to put in the array.
After the initial array is filled, interact with the user via a menu until they want to quit.
Sample Menu
Additional Requirements
For each of the options the user has access to, create a function to handle the work involved.
int* insert(int arr[], int size, int value, int position)
Inserts the given value at the specified position
Creates a new array, copies all old value over adjusting indices as necessary
Returns a pointer to the new array
int* remove(int arr[], int size, int position)
Reomves the value at the given position
Creates a new array, copies all old value over adjusting indices as necessary
Returns a pointer to the new array
void print(int arr[], int size)
Prints array as required
The user will provide a position to insert a value
You must obtain a valid position before moving on
Obtain the value to insert and insert it into the array
NOTE: The array will be one element larger after
The user will provide a position to remove a value
You must obtain a valid position before moving on
Once you have a valid position, remove that value
NOTE: The array will be one element smaller after
Obtain a value from the user
Tell them how many times that value is in the array
Print the contents of the array in the following format:
Exits the program
Option Description InsertThe user will provide a position to insert a value
You must obtain a valid position before moving on
Obtain the value to insert and insert it into the array
NOTE: The array will be one element larger after
RemoveThe user will provide a position to remove a value
You must obtain a valid position before moving on
Once you have a valid position, remove that value
NOTE: The array will be one element smaller after
CountObtain a value from the user
Tell them how many times that value is in the array
PrintPrint the contents of the array in the following format:
[1, 3, 99]Exit
Exits the program
Explanation / Answer
#include<iostream>
using namespace std;
int* insert(int a[], int n, int value, int position)
{
int *b = new int[n+1];
int i;
for(i = 0; i < position; i++)
b[i] = a[i];
b[i] = value;
n = n+1;
for(int j = i+1; j < n; j++)
{
b[j] = a[j-1];
}
delete a;
a = b;
return a;
}
int* remove(int a[], int n, int position)
{
int *b = new int[n-1];
int i;
for(i = 0; i < position; i++)
b[i] = a[i];
n = n-1;
for(int j = i; j < n; j++)
{
b[j] = a[j+1];
}
delete a;
a = b;
return a;
}
int count(int a[], int n, int element)
{
int c = 0;
for(int i = 0; i < n; i++)
{
if (a[i] == element)
{
c++;
}
}
return c;
}
void print(int a[], int n)
{
cout << "[";
int i = 0;
for(i = 0; i < n-1; i++)
{
cout << a[i] << ", ";
}
if (i < n)
{
cout << a[i];
}
cout << "]" << endl;
}
int main()
{
cout << "Enter number of elements in array: ";
int n;
cin >> n;
int *a = new int[n];
for(int i = 0; i < n ; i++ )
{
cout << "Enter element " << (i+1) << ": ";
cin >> a[i];
}
while(true)
{
cout << "Make a selection:"<<endl;
cout << "1) Insert" <<endl;
cout << "2) Remove" << endl;
cout << "3) Count" <<endl;
cout << "4) Print" <<endl;
cout << "5) Exit" << endl;
cout << "Choice: ";
int choice;
cin >> choice;
if (choice < 1 || choice > 5)
{
cout << "Please choose from given options" << endl;
continue;
}
if (choice == 5)
{
break;
}
int element;
int pos;
switch(choice)
{
case 1:
cout << "Insert an element to be inserted: ";
cin >> element;
while(true)
{
cout << "Insert position of insertion between 0 and " << n << " : ";
cin >> pos;
if (pos < 0 || pos > n)
{
cout << "Please enter a correct position."<<endl;
}
else
{
break;
}
}
a = insert(a, n, element, pos);
n = n+1;
break;
case 2:
while(true)
{
cout << "Insert position of insertion between 0 and " << n-1 << " : ";
cin >> pos;
if (pos < 0 || pos >= n)
{
cout << "Please enter a correct position."<<endl;
}
else
{
break;
}
}
a = remove(a, n, pos);
n = n-1;
break;
case 3:
cout << "Enter an element to get its count: " ;
cin >> element;
cout << "Count is : " << count(a, n, element) << endl;
break;
case 4:
print(a, n);
break;
}
}
return 0;;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.