Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please, finish this program, by using C language. there are three files, two used to structure and one used to test. The goal of this

please, finish this program, by using C language. there are three files, two used to structure and one used to test.

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:

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

/* 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

2. dynamicArray.c

/* dynArr.c: Dynamic Array implementation. */
#include
#include
#include
#include "dynamicArray.h"
/* ************************************************************************
Dynamic Array Functions
************************************************************************ */
/* Initialize (including allocation of data array) dynamic array.
param: v pointer to the dynamic array
param: cap capacity of the dynamic array
pre: v is not null
post: internal data array can hold capacity elements
post: v->data is not null
*/
void _initDynArr(DynArr *v, int capacity)
{
assert(capacity > 0);
assert(v!= 0);
v->data = malloc(sizeof(TYPE) * capacity);
assert(v->data != 0);
v->size = 0;
v->capacity = capacity;
}
/* Allocate and initialize dynamic array.
param: cap desired capacity for the dyn array
pre: none
post: none
ret: a non-null pointer to a dynArr of cap capacity
and 0 elements in it.
*/
DynArr* createDynArr(int cap)
{
DynArr *r;
assert(cap > 0);
r = malloc(sizeof( DynArr));
assert(r != 0);
_initDynArr(r,cap);
return r;
}
/* Deallocate data array in dynamic array.
param: v pointer to the dynamic array
pre: v is not null
post: d.data points to null
post: size and capacity are 0
post: the memory used by v->data is freed
*/
void freeDynArr(DynArr *v)
{
assert(v!=0);
if(v->data != 0)
{
free(v->data); /* free the space on the heap */
v->data = 0; /* make it point to null */
}
v->size = 0;
v->capacity = 0;
}
/* Deallocate data array and the dynamic array ure.
param: v pointer to the dynamic array
pre: v is not null
post: the memory used by v->data is freed
post: the memory used by d is freed
*/
void deleteDynArr(DynArr *v)
{
assert (v!= 0);
freeDynArr(v);
free(v);
}
/* Get the size of the dynamic array
param: v pointer to the dynamic array
pre: v is not null
post: none
ret: the size of the dynamic array
*/
int sizeDynArr(DynArr *v)
{
assert(v!=0);
return v->size;
}
/* Adds an element to the end of the dynamic array
param: v pointer to the dynamic array
param: val the value to add to the end of the dynamic array
pre: the dynArry is not null
post: size increases by 1
post: if reached capacity, capacity is doubled
post: val is in the last utilized position in the array
*/
void addDynArr(DynArr *v, TYPE val)
{
// TODO: Implement this function
}
/* Get an element from the dynamic array from a specified position
param: v pointer to the dynamic array
param: pos integer index to get the element from
pre: v is not null
pre: v is not empty
pre: pos < size of the dyn array and >= 0
post: no changes to the dyn Array
ret: value stored at index pos
*/
TYPE getDynArr(DynArr *v, int pos)
{
// TODO: Please write this function, remember to return a value with the type TYPE
}
/* Put an item into the dynamic array at the specified location,
overwriting the element that was there
param: v pointer to the dynamic array
param: pos the index to put the value into
param: val the value to insert
pre: v is not null
pre: v is not empty
pre: pos >= 0 and pos < size of the array
post: index pos contains new value, val
*/
void putDynArr(DynArr *v, int pos, TYPE val)
{
// TODO: Please write this function
}
/* Swap two specified elements in the dynamic array
param: v pointer to the dynamic array
param: i,j the elements to be swapped
pre: v is not null
pre: v is not empty
pre: i, j >= 0 and i,j < size of the dynamic array
post: index i now holds the value at j and index j now holds the value at i
*/
void swapDynArr(DynArr *v, int i, int j)
{
// TODO: Please write this function
}
/* Remove the element at the specified location from the array,
shifts other elements back one to fill the gap
param: v pointer to the dynamic array
param: idx location of element to remove
pre: v is not null
pre: v is not empty
pre: idx < size and idx >= 0
post: the element at idx is removed
post: the elements past idx are moved back one
*/
void removeAtDynArr(DynArr *v, int idx){
int i;
assert(v!= 0);
assert(idx < v->size);
assert(idx >= 0);
//Move all elements up
/* My loop does not execute when idx == size-1
* so I don't have to worry about coping an element outside the array
* into that idx!
*/
for(i = idx; i < v->size-1; i++){
v->data[i] = v->data[i+1];
}
v->size--;
}
/* Returns boolean (encoded in an int) demonstrating whether or not the
dynamic array stack has an item on it.
param: v pointer to the dynamic array
pre: v is not null
post: none
ret: >0 if empty, otherwise 0
*/
int isEmptyDynArr(DynArr *v)
{
assert(v!= 0);
return !(v->size);
/* alternatively:
if(v->size == 0)
return 1;
else return 0;
*/
}
/*
Print the dynamic array contents. This requires that PRINT_STR be defined.
RAM: Fix this to take a printType function pointer!
*/
void printDynArr(DynArr *v)
{
int i;
printf(" Array size = %d ", v->size);
printf("Array capacity = %d ", v->capacity);
printf("Array Contents: ====================== ");
for(i=0; i < v->size; i++)
{
printf("DA[%d] == ",i);
printf("%c", v->data[i]);
printf(" ");
}

}

3. testDynArray.c

/* testDynArray.c
* This file is used for testing the dynamicArray.c file.
* This test suite is by no means complete or thorough.
* More testing is needed on your own.
*/
#include
#include
#include "dynamicArray.h"
void assertTrue(int predicate, char *message)
{
printf("%s: ", message);
if (predicate)
printf("PASSED ");
else
printf("FAILED ");
}
// this main function contains some
int main(int argc, char* argv[]){
DynArr *dyn;
dyn = createDynArr(2);
printf(" Testing addDynArr... ");
addDynArr(dyn, 3);
addDynArr(dyn, 4);
addDynArr(dyn, 10);
addDynArr(dyn, 5);
addDynArr(dyn, 6);
printf("The array's content: [3,4,10,5,6] ");
assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
printf(" Testing putDynArr... Calling putDynArr(dyn, 2, 7) ");
putDynArr(dyn, 2, 7);
printf("The array's content: [3,4,7,5,6] ");
assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
printf(" Testing swapDynArr... Calling swapDynArr(dyn, 2, 4) ");
swapDynArr(dyn, 2, 4);
printf("The array's content: [3,4,6,5,7] ");
assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
printf(" Testing removeAtDynArr... Calling removeAtDynArr(dyn, 1) ");
removeAtDynArr(dyn, 1);
printf("The array's content: [3,6,5,7] ");
assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
printf("Testing shrinking ");
DynArr* d = createDynArr(10);
for(int i=0;i<10;i++){
addDynArr(d,i+'a');
}
printDynArr(d);
for(int i=0;i<8;i++){
removeAtDynArr(d,0);
}
printDynArr(d);
assertTrue((d->capacity)<10, "Test resize");
printf("DONE");
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago