Question: C Programming Write the following program Implement a program that inserts elements after the array, or deletes the trailing elements. As you insert and delete

C Programming

Write the following program Implement a program that inserts elements after the array, or deletes the trailing elements. As you insert and delete elements, the size of the array is dynamically adjusted as follows: Initial array size: 5, Minimum array size: 5 (does not decrease to less than 5 without elements) If you occupy more than 80% of the array size after inserting the elements, increase the size of the array. If you occupy less than 30% of the array size after the element deletion, reduce the array size to 50%. Input: Input 1 (Insert) or 2 (Delete) to select the operation first, 3 (Contents output), 4 (End) If you enter 1, then the number to be inserted is entered. If you enter 2, immediately delete the last element Whenever the array size changes, before and after the change, output the size separately Constraint Can not declare static array, declare global variable, release all memory before shutdown Ignore in delete operation when there is no element All functions on the next page should be implemented

void push_back (int ** a, int * size, int value); // This function adds value to the end of array a with size size. // Add the value through realloc and keep the size appropriate to the problem condition. // Because the address of the array may change after realloc // Enables changing the existing pointer address through int ** a instead of int * a.

void pop_back (int ** a, int * size); // This function deletes the last element of the array a whose size is size. // Delete the element through realloc and keep the size appropriate to the problem condition. // Because the address of the array may change after realloc // Enables changing the existing pointer address through int ** a instead of int * a.

My code:

#include #include

void push_back(int **a, int *size, int value)/*{{{*/ { int i, *p; int oldsize; i = *size - 1; *(*a + *size - 1) = value;

if (i >= 0.8 * *size) { *size = * size * 2; *a = (int *)realloc(*a, *size * sizeof(int)); printf("Change Size %d - > %d ", *size / 2, *size); } } /*}}}*/ void pop_back(int **a, int *size)/*{{{*/ { int i, count = 0; int oldsize; oldsize = *size; for (i = 0; i < *size; i++) { if (*(*a + i) = 1) count++; else *(*a + i - 2) = *(*a + i - 1); } if (i <= 0.3 * *size) { *size = *size / 2; *a = (int *)realloc(*a, *size * sizeof(int)); printf("Change Size %d - > %d ", oldsize, *size); } } /*}}}*/ void print_array(int **a, int *size)/*{{{*/ { int i; for (i = 0; i < *size; i++) printf("%d ", *(*a + i)); printf(" "); } /*}}}*/

int main()/*{{{*/ { int *a, size = 5, value, ctr; a = (int *)malloc(sizeof(int) * size); /*if (!a) printf("not allocated ");*/

while (true) { scanf("%d", &ctr); if (ctr == 1) { scanf("%d", &value); push_back(&a, &size, value); continue; } if (ctr == 2) pop_back(&a, &size); if (ctr == 3) print_array(&a, &size); if (ctr == 4) { printf("Termination. "); break; } } free(a);

return 0; } /*}}}*/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!