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

1 - Objective The purpose of this assignment is for students to gain a better un

ID: 3743703 • Letter: 1

Question

1 - Objective The purpose of this assignment is for students to gain a better understanding of pointers and dynamic allocation, along with reinforcing their knowledge of arrays.

2 - Problem Write a program that:

- Creates a dynamic array of integers (int) starting size 2 + This will be used to store user input elements.

- Create a menu interface with the options: Print Element, Add Element, Delete Element, Return Size, and Exit. This will be how the user interacts with the array. (Note: You can look over the Sample Output section to see how the user interacts with the menu)

+ Print Elements:

+ This option will print all the elements currently stored in the array.

+ If there are no elements in the array, it prints out “No elements” (without quotes)

+ Add Element:

+ This option asks the user to input a number to store into the array, then inserts the number in the array.

+ If the array does not have room to add another element, the array should be expanded to 2 times its current size before adding the element.

+ Print a message “Array expanded” (without quotes) whenever the size doubles.

+ Anytime the array size is changed, you should copy the old array elements into the new one and deallocate the old one to prevent memory leaks.

+ There should be no gaps/garbage values between elements in the array.

+ Delete Element:

+ This option should ask the user for a number to remove from the array.

+ If the specified number does not exist inside the array, the output should be “Not there” (without quotes).

+ Anytime an element is deleted from the beginning or middle of the array, all other elements in the array should be shifted over to fill the empty gap.

+ Anytime the array size is changed, you should copy the old array elements into the new one and deallocate the old one to prevent memory leaks.

+ If the number of elements is less than or equal to half the size of the array, the array should be shrunk down to half its current size.

+ Print a message “Array shrinked” whenever the array size is decreased.

+ Return Size:

+ This option prints the current size of the array along with the number of elements stored within it. The output should be formatted as: “S: #, E: #” (without quotes), where # is the number for size (S) and elements (E) of the array.

+ Exit:

+ This option deallocates all dynamic variables and ends the program.

Sample Output

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: a

Enter element: 1

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: a

Enter element: 2

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: r

S: 2, E: 2

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: a

Enter element: 3

Array expanded

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: r

S: 4, E: 3

(p): Print elements

(a):Add element

(d): Delete element

(r): Return size

(e): Exit

Enter option: e

Process finished with exit code 0

Explanation / Answer

main.cpp

#include<iostream>

using namespace std;
void print(int *arr,int E) //function to print the array elements
{
if(E==0) //if E=0, then there are no elements
cout<<"No elements "<<endl;
else //if E!=0
{
for(int i=0;i<E;i++) //Displaying elements one by one
cout<<arr[i]<<" ";
cout<<endl;
}
}
int *add(int *arr,int &s,int &e,int num) //function for adding number(num) in array
{
if(s==e) //if s=e, then array is full
{
int *arr2=new int[2*s]; //creating a new array of size 2*s
for(int i=0;i<s;i++) //copying arr to arr2
arr2[i]=arr[i];
arr2[s]=num; //adding the element to be added to arr2
delete []arr; //deallocating memory from arr
s=2*s; //increasing size to twice as much
e++; //incrementing e by 1
cout<<"Array expanded ";
return arr2; //returning new array
}
else //if array is not full
{
arr[e]=num; //adding the num to the end of array
e++;
return arr;
}
}
int *del(int *arr,int &s,int &e,int num) //Function for deleting num from array
{
int ind=-1; //flag variable for index of element to be deleted
for(int i=0;i<e;i++) //loop for finding the index of the element to be deleted
{
if(num==arr[i])
{
ind=i;
break;
}
}
if(ind==-1) //if ind hasn't changed, then element does not exist
{
cout<<"Not here ";
return arr;
}
else //element exists in array
{
for(int i=ind+1;i<e;i++) //shifting all elements ahead of that elements left by one
arr[i-1]=arr[i];
e--; //decrementing e by 1
if(e<=s/2) //if number of elements is <= size, then shrink the array
{
int *arr2=new int[s/2]; //creating new array with s=s/2;
for(int i=0;i<e;i++) //copying arr to arr2
arr2[i]=arr[i];
delete []arr; //deleting arr
s=s/2;

cout<<"Array shrinked ";

return arr2; //returning new array
}
else return arr; //otherwise returning old array
}

}
int main()
{
int S=2,E=0; //for storing size and no of elements in array
int *arr=new int[2]; //creating array of size 2
char ch; //character for user input
while(1) //iterating loop until user exits
{
cout<<"(p): Print elements (a):Add element (d): Delete element (r): Return size (e): Exit Enter option: ";
cin>>ch; //User input
int num; //for add and delete functions
switch(ch)
{
case 'p': //for printing array
print(arr,E); //calling print()
break;
case 'a': //For adding element in array
cout<<"Enter Element: ";
cin>>num; //Taking from user number to be added
arr=add(arr,S,E,num);
break;
case 'd': //for deleting an element from array
cout<<"Enter Element: ";
cin>>num; //taking from user the number to be deleted
arr=del(arr,S,E,num);
break;
case 'r': //for returning size and no of elements
cout<<"S: "<<S<<", E: "<<E<<endl;
break;
case 'e': //for exiting the program
cout<<"Process finished with exit code 0 ";
delete []arr;
return 0;

}
}
}