please, finish this program, by using C language. there are three files, two use
ID: 3784555 • Letter: P
Question
please, finish this program, by using C language. there are three files, two used to structure and one used to test. the code which should be completed are in .c file. please do some test and show the results.
The goal of this assignment is to have you start working with the dynamic array data structure. There are three parts to the assignment, outlined below. Follow the instructions listed in each part, filling in and augmenting the appropriate places in the provided code, as requested.
Part I
You are given a partial dynamic array implementation in dynamicArray.c. Please implement the addDynArr, getDynArr, putDynArr and swapDynArr functions that are missing in this implementation.
Part II
Currently, the array will never shrink.
Your task is to modify the given functions (do not modify the prototypes) to shrink the array to half its capacity when a remove causes the size to be 1/3rd of the capacity. Please modify dynamicArray.c so that it correctly resizes the array as described above. When you make a change to a function please put a comment like so
// PART II: <description of change>
There is an additional file called testDynArray.c you may use to test your solution, it is highly recommended that you do additional testing!
Part III
Amortized Analysis of the Dynamic Array Consider the push() operation for a Dynamic Array. In the best case, the operation is O(1). This corresponds to the case where there was room in the space we have already allocated for the array. However, in the worst case, this operation slows down to O(n). This corresponds to the case where the allocated space was full and we must copy each element of the array into a new (larger) array. This problem is designed to discover runtime bounds on the average case when various array expansion strategies are used, but first some information on how to perform an amortized analysis is necessary.
Each time an item is added to the array without requiring reallocation, count 1 unit of cost. This cost will cover the assignment which actually puts the item in the array.
Each time an item is added and requires reallocation, count X + 1 units of cost, where X is the number of items currently in the array. This cost will cover the X assignments which are necessary to copy the contents of the full array into a new (larger) array, and the additional assignment to put the item which did not fit originally To make this more concrete, if the array has 8 spaces and is holding 5 items, adding the sixth will cost 1. However, if the array has 8 spaces and is holding 8 items, adding the ninth will cost 9 (8 to move the existing items + 1 to assign the ninth item once space is available).
When we can bound an average cost of an operation in this fashion, but not bound the worst case execution time, we call it amortized constant execution time, or average execution time. Amortized constant execution time is often written as O(1+), the plus sign indicating it is not a guaranteed execution time bound.
In a file called amortizedAnalysis.txt, please provide answers to the following questions:
How many cost units are spent in the entire process of performing 16 consecutive push operations on an empty array which starts out at capacity 8, assuming that the array will double in capacity each time new item is added to an already full dynamic array? Now try it for 32 consecutive push operations. As N (ie. the number of pushes) grows large, under this strategy for resizing, what is the big-oh complexity for push?
How many cost units are spent in the entire process of performing 16 consecutive push operations on an empty array which starts out at capacity 8, assuming that the array will grow by a constant 2 spaces each time new item is added to an already full dynamic array? Now try it for 32 consecutive push operations. As N (ie. the number of pushes) grows large, under this strategy for resizing, what is the big-oh complexity for push?
Finally, add, commit and push this file to the repository.
1. dynamicArray.h
#endif
2. dynamicArray.c
}
3. testDynArray.c
/* dynArr.h : Dynamic Array implementation. */ #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 /* RAM: All this type stuff This should go elsewhere! */ # define KT void * # define VT void * #define TYPE char //int #define TYPE_SIZE sizeof(char) #define LT(A, B) ((A) < (B)) #define EQ(A, B) ((A) == (B)) #define PRINT_STR "%df" #define CAST_STR char * /* function used to compare two TYPE values to each other */ int compare(TYPE left, TYPE right); typedef int (*comparator)(void *left, void*right); typedef struct DynArr DynArr; struct DynArr { TYPE *data; /* pointer to the data array */ int size; /* Number of elements in the array */ int capacity; /* capacity ofthe array */ }; struct DynArrIter; /* Dynamic Array Functions */ DynArr *createDynArr(int cap); void deleteDynArr(DynArr *v); int sizeDynArr(DynArr *v); void addDynArr(DynArr *v, TYPE val); TYPE getDynArr(DynArr *v, int pos); void putDynArr(DynArr *v, int pos, TYPE val); void swapDynArr(DynArr *v, int i, int j); void removeAtDynArr(DynArr *v, int idx); int isEmptyDynArr(DynArr *v); /* Utility function*/ void printDynArr(DynArr *v);#endif
Explanation / Answer
// inner block scopes #include using namespace std; int main () { int x = 10; int y = 20; { int x; // ok, inner scope. x = 50; // sets value to inner x y = 50; // sets value to (outer) y coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.