1 - Objective The purpose of this assignment is for students to gain a better un
ID: 3744437 • 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
Refer to code below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char opt; //for getting character input
int *arr = (int*)malloc(2*sizeof(int));
int num;
int i=0;//index for storing the number in array
int size=2;//size of array;
while(1)
{
printf("(p): Print elements ");
printf("(a): Add element ");
printf("(d): Delete element ");
printf("(r): Return size ");
printf("(e): Exit ");
printf("Enter option: ");
scanf(" %c",&opt);
//exit if opt is e
if(opt=='e')
break;
else if(opt=='p')
{
if(i==0)
{
printf("No elements ");
}
for(int j=0;j<i;j++)
{
printf("%d ",arr[j]);
}
printf(" ");
}
else if(opt=='a')
{
printf("Enter element: ");
scanf("%d",&num);
if(size==0)
{
arr = (int*)malloc(1*sizeof(int));
size=1;
printf("Array expanded ");
}
if(i==size)
{
arr = (int*)realloc(arr,2*size);
size=2*size;
printf("Array expanded ");
}
arr[i] = num;
i++;
}
else if(opt=='d')
{
printf("Enter element: ");
scanf("%d",&num);
int index=-1;
for(int j=0;j<i;j++)
{
//searching for input number in array
if(arr[j]==num)
{
index = j;
break;
}
}
if(index==-1)
{
printf("Not there ");
}
else
{
for(int j=index;j<i-1;j++)
{
arr[j]=arr[j+1];
}
i--;
//if numbers in array are half the size of array then shrink
if(i==size/2)
{
printf("Array shrinked ");
arr = (int*)realloc(arr,size/2);
size=size/2;
}
}
}
else if(opt=='r')
{
printf("S: %d E: %d ",size,i);
}
else
{
// if the input is not any of p,a,d,r,e
printf("Please enter valid input ");
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.