Create an integer array of size on the heap, and insert -1 into every element Increase the size of your array by 1 element: allocate space for a new array copy the values over make the last value 1,...




  1. Create an integer array of size on the heap, and insert -1 into every element




  2. Increase the size of your array by 1 element:





    1. allocate space for a new array




    2. copy the values over




    3. make the last value 1, instead of negative 1.





  3. Double the size of your array, copying the values from the first half to the second half




  4. Reduce the size of the array by half, only keeping the first half values




  5. Remove the first element from the array, and shrink the array by one. The resulting array should be exactly like the previous, only missing the first element.






#include #include #include #include #include #define INIT_SIZE 20 #define TEST1 1 #define TEST2 1 #define TEST3 1 #define TEST4 1 #define TEST5 1 #if TEST1 int * test1(int size); #endif #if TEST2 int * test2(int * nums, int size); #endif #if TEST3 int * test3(int * nums, int size); #endif #if TEST4 int * test4(int * nums, int size); #endif #if TEST5 int * test5(int * nums, int size); #endif int main(){ srand(time(NULL)); printf("\t-----------------------------------\n"); printf("\t- Dynamic Arrays -\n"); printf("\t-----------------------------------\n"); printf("\t========= After each test you should print our your array using a loop ===========\n"); printf("\t========= All loops should ONLY use pointer arithmetic to iterate ===========\n\n"); int * test_array = NULL; #if TEST1 printf("\n\t=========Test #1: Create an integer array of size ===========\n"); printf("\t== Write a function that returns an integer array of INIT_SIZE elements all containing -1 ==\n\n"); test_array = test1(INIT_SIZE); for(int * ptr = test_array; ptr < test_array+init_size;="" ptr++){="" assert(*ptr="=" -1);="" }="" #endif="" #if="" test2="" printf("\n\t="========Test" #2:="" 'grow'="" the="" array="" by="" 1,="" and="" insert="" the="" value="" 1="" at="" the="" end="==========\n\n");" int="" *="" ptr2="test2(test_array," init_size);="" assert(ptr2="" !="test_array);" test_array="ptr2;" for(;="" ptr2="">< test_array+init_size;="" ptr2++){="" assert(*ptr2="=" -1);="" }="" assert(*ptr2="=" 1);="" #endif="" #if="" test3="" printf("\n\t="========Test" #3:="" double="" the="" size="" of="" the="" array="==========\n\n");" printf("\n\t="========Test" #3:="" copy="" the="" values="" from="" the="" old="" array="" to="" the="" new="" array="==========\n\n");" printf("\n\t="========Test" #3:="" fill="" the="" remaining="" indexes="" in="" the="" new="" array="" with="" the="" first="" half="" values="==========\n\n");" int="" *="" ptr3="test3(test_array," init_size+1);="" assert(ptr3="" !="test_array);" test_array="ptr3;" for(int="" i="0;" i="">< (init_size+1)*2;="" i++){="" if(i="=" 20="" ||="" i="=" 41){="" assert(ptr3[i]="=" 1);="" }else{="" assert(ptr3[i]="=" -1);="" }="" }="" #endif="" #if="" test4="" printf("\n\t="========Test" #4:="" reduce="" the="" size="" of="" the="" array="" by="" half="==========\n\n");" printf("\n\t="========Test" #4:="" copy="" over="" the="" first="" half="" values="==========\n\n");" int="" *="" ptr4="test4(test_array," (init_size+1)*2);="" assert(ptr4="" !="test_array);" test_array="ptr4;" for(;="" ptr4="">< test_array+init_size;="" ptr4++)="" assert(*ptr4="=" -1);="" assert(*ptr4="=" 1);="" #endif="" #if="" test5="" printf("\n\t="========Test" #5:="" remove="" a="" the="" first="" element="" from="" your="" array="==========\n\n");" printf("\n\t="========Test" #5:="" the="" array="" size="" should="" shrink="" by="" exactly="" 1="==========\n\n");" int="" *="" ptr5="test5(test_array," init_size+1);="" assert(ptr5="" !="test_array);" test_array="ptr5;" for(;="" ptr5="">< test_array+(init_size-1); ptr5++) assert(*ptr5 == -1); assert(*ptr5 == 1); #endif free(test_array); printf("\n\t=========run your code with valgrind to ensure there are no errors ===========\n\n"); return 0; } test_array+(init_size-1);="" ptr5++)="" assert(*ptr5="=" -1);="" assert(*ptr5="=" 1);="" #endif="" free(test_array);="" printf("\n\t="========Run" your="" code="" with="" valgrind="" to="" ensure="" there="" are="" no="" errors="==========\n\n");" return="" 0;="">
Apr 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here