/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */

    assert(heap != 0);

    /* iterators for heap */
    int parentIt;

    //int posIt = sizeDynArr(heap); //same result
    int posIt = heap->size;

    /* add val to end of array, then adjust position */
    addDynArr(heap, val);

    /* percolate from leaves to root and swap if/where appropriate */
    while (posIt != 0)
    {
        parentIt = (posIt-1)/2;
        if (compare(getDynArr(heap, posIt), getDynArr(heap, parentIt) ) == -1) //posIt is < parent, so swap
        {
            swapDynArr(heap, posIt, parentIt);
            posIt = parentIt;                                                  //onward towards the root
        }
        else                                                                   //posIt is > or = to parent, NO swap
        {
            return;
        }
    }

}
Example #2
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val, comparator compare)
{
	addDynArr(heap, val);
	int i = heap->size;
	int itterator = i-1;
	_adjustHeap(heap, i, (itterator-1)/2, compare);
}
int main(int argc, char* argv[]) {	
	DynArr* b;
	int n, i;
	double t1, t2;

	for(n=1000; n < 200000; n=n*2) /* outer loop */
	{

	  
	b = createDynArr(n); 
		
	for( i = 0 ; i < n; i++) {
		addDynArr(b, (TYPE)i); /*Add elements*/
	}		
	
	t1 = getMilliseconds();/*Time before contains()*/
	
	for(i=0; i<n; i++) {
		containsDynArr(b, i);		
	}	
	
	t2 = getMilliseconds();/*Time after contains()*/
	
	printf("Time for running contains() on %d elements: %g ms\n", n, t2-t1);
  
	/* delete DynArr */
	deleteDynArr(b);
 	}	
	return 0;
}
Example #4
0
File: calc.c Project: dot1q/Misc
void logg10(struct DynArr *stack)
{
        double log;
        log = log10(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, log);
        printf(" \nlog10\n");
}
Example #5
0
File: calc.c Project: dot1q/Misc
void logg (struct DynArr *stack)
{
        double logger;
        logger = log(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, logger);
        printf(" \nln\n");
}
Example #6
0
File: calc.c Project: dot1q/Misc
void expo (struct DynArr *stack)
{
        double ex;
        ex = exp(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, ex);
        printf(" \nexp\n");
}
Example #7
0
File: calc.c Project: dot1q/Misc
void sqf (struct DynArr *stack)
{
        double sqVar;
        sqVar = sqrt(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, sqVar);
        printf(" \nsqrt\n");
}
Example #8
0
File: calc.c Project: dot1q/Misc
void abso (struct  DynArr *stack)
{
        double abso;
        abso = fabs(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, abso);
        printf(" \nabs\n");
}
Example #9
0
File: calc.c Project: dot1q/Misc
void cube (struct DynArr *stack)
{
        double cube;
        cube = pow(stack->data[stack->size-1], 3);
        popDynArr(stack);
        addDynArr(stack, cube);
        printf(" \ncube\n");
}
Example #10
0
File: calc.c Project: dot1q/Misc
void square (struct DynArr *stack)
{
        double square;
        square = pow(stack->data[stack->size-1], 2);
        popDynArr(stack);
        addDynArr(stack, square);
        printf(" \nsquare\n");
}
Example #11
0
/* 	Push an element onto the top of the stack

	param:	v		pointer to the dynamic array
	param:	val		the value to push onto the stack
	pre:	v is not null
	post:	size increases by 1
			if reached capacity, capacity is doubled
			val is on the top of the stack
*/
void pushDynArr(DynArr *v, TYPE val)
{
	/* DONE: You will write this function */
	// Since the 'top' of the stack is the last element in the array,
	// we can use addDynArray for this functionalit

	addDynArr(v, val);

}
Example #12
0
File: main2.c Project: vb3/homework
int main (int argc, const char * argv[])
{
  	TYPE task1, task2, task3, task4, task5, task6, task7, task8, task9, task10;
	DynArr mainList;
	int i;
	initDynArr(&mainList, 10);

	/* create tasks */
	task1 = createTask(9, "task 1");
	task2 = createTask(3, "task 2");
	task3 = createTask(2, "task 3");
	task4 = createTask(4, "task 4");
	task5 = createTask(5, "task 5");
	task6 = createTask(7, "task 6");
	task7 = createTask(8, "task 7");
	task8 = createTask(6, "task 8");
	task9 = createTask(1, "task 9");
	task10 = createTask(0, "task 10");
	
	/* add tasks to the dynamic array */
	addDynArr(&mainList, task1);
	addDynArr(&mainList, task2);
	addDynArr(&mainList, task3);
	addDynArr(&mainList, task4);
	addDynArr(&mainList, task5);
	addDynArr(&mainList, task6);
	addDynArr(&mainList, task7);
	addDynArr(&mainList, task8);
	addDynArr(&mainList, task9);
	addDynArr(&mainList, task10);

	/* sort tasks */
	sortHeap(&mainList);

	/* print sorted tasks from the dynamic array */
	for (i = 0; i < mainList.size; i++)
	{
	  	printf("%d\n", mainList.data[i].priority);
	}
    
    printList(&mainList);

	return 0;
}
Example #13
0
File: calc.c Project: dot1q/Misc
void power (struct DynArr *stack)
{	
        double power;
	checkArrSize(stack);
        power = pow( stack->data[stack->size-2],  stack->data[stack->size-1]);
        popDynArr(stack);
	popDynArr(stack);
        addDynArr(stack, power);
        printf(" \n^\n");
}
Example #14
0
File: calc.c Project: dot1q/Misc
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their quotient is pushed back onto the stack.
*/
void divide(struct DynArr *stack)
{
	double divide;
        checkArrSize(stack);
        divide = stack->data[stack->size-2] / stack->data[stack->size-1];
        popDynArr(stack);
        popDynArr(stack);
        addDynArr(stack, divide);
        printf(" /\n");
}
Example #15
0
File: calc.c Project: dot1q/Misc
void multiply (struct DynArr *stack)
{
        double multiply;
        checkArrSize(stack);
        multiply = stack->data[stack->size-2] * stack->data[stack->size-1];
        popDynArr(stack);
        popDynArr(stack);
        addDynArr(stack, multiply);
        printf(" \n*\n");
}
Example #16
0
File: calc.c Project: dot1q/Misc
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their sum is pushed back onto the stack.
*/
void add(struct DynArr *stack)
{
	double sum;
	checkArrSize(stack);
	sum = stack->data[stack->size-2] +  stack->data[stack->size-1];
	popDynArr(stack);
	popDynArr(stack);	
	addDynArr(stack, sum);
	//printf("size of stack is %d and temp sum is %f and stack value at size is %f\n",stack->size, sum, stack->data[stack->size-1]);
	printf(" +\n");
}
Example #17
0
File: calc.c Project: dot1q/Misc
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their difference is pushed back onto the stack.
*/
void subtract(struct DynArr *stack)
{
	/* FIXME: You will write this function */
	double minus;
	minus =  stack->data[stack->size-2] - stack->data[stack->size-1];
	popDynArr(stack);
	popDynArr(stack);
	addDynArr(stack, minus);
	//printf("size of stack is %d and temp minus is %f and stack value at size is %f\n",stack->size, minus, stack->data[stack->size-1]);
	printf(" -\n");
}
Example #18
0
/* Resizes the underlying array to be the size cap

	param: 	v		pointer to the dynamic array
	param:	cap		the new desired capacity
	pre:	v is not null
	post:	v has capacity newCap
*/
struct DynArr* _dynArrSetCapacity(struct DynArr *v, int newCap)
{
    struct DynArr *temp = createDynArr(newCap);
    int x;

    temp->capacity = newCap;
    temp->size = v->size;

    for (x = 0; x < v->size; x++) {
        addDynArr(temp, v->data[x]);
    }

    return temp;
}
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    int parent;
    int pos = sizeDynArr(heap);
    addDynArr(heap, val);
    while (pos != 0){
        parent = (pos - 1) / 2;
        if (compare(getDynArr(heap, pos), getDynArr(heap, parent)) == -1){
            swapDynArr(heap, parent, pos);
            pos = parent;
        }
        else return;
    }
}
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
 */
void addHeap(DynArr *heap, TYPE val)
{
    int parent;

    addDynArr(heap, val);
    int pos = sizeDynArr(heap)-1;
    while(pos != 0) {
        parent = (pos-1)/2;
        if (compare(getDynArr(heap,pos),getDynArr(heap,parent)) == -1) {
            swapDynArr(heap, pos, parent);
            pos = parent;
        }
        else {
            return;
        }
    }
}
Example #21
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    int parent;
int pos = sizeDynArr(heap);
addDynArr(heap, val); /* adds to end – now need to adjust position */
	while(pos != 0){
		parent = (pos-1) / 2;
		if ( heap->data[pos] < heap->data[parent]){
			swapDynArr(heap, pos, parent);
			pos = parent;
		}
		else return;
	}


}
Example #22
0
/* Resizes the underlying array to be the size cap

param: 	v		pointer to the dynamic array
param:	cap		the new desired capacity
pre:	v is not null
post:	v has capacity newCap
*/
void _dynArrSetCapacity(DynArr *v, int newCap)
{
	assert(v != 0);
	assert(newCap > v->capacity);

	DynArr* temp = newDynArr(newCap); //create a new array
	int size = sizeDynArr(v);	//get the size of the array

								//copy elements from old array into new array
	for (int i = 0; i < size; i++)
	{
		addDynArr(temp, v->data[i]);
	}

	freeDynArr(v); //deallocate memory tied to old array
	*v = *temp; //replace old array with new array by overwriting the original structure
	free(temp); //deallocate memory tied to the temporary struct
}
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    addDynArr(heap, val);

	int curPos = sizeDynArr(heap) - 1;

	while(curPos > 0)
	{
		int parent = (curPos - 1) / 2;
		if(compare(getDynArr(heap, curPos), getDynArr(heap, parent)) == -1)
		{
			swapDynArr(heap, curPos, parent);
			curPos = parent;
		}
		else
			break;
	}

}
Example #24
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
	int pos = sizeDynArr(heap);

	addDynArr(heap, val);

	while(pos){
		int parent = (pos-1)/2;
		int left = pos*2 + 1;
		int right = pos*2 + 2;
		int child = _smallerIndexHeap(heap, left, right);
		if(getDynArr(heap, child) < getDynArr(heap, pos)){
			swapDynArr(heap, parent, pos);
			pos = parent;
		}
		else
			return;
	}
}
Example #25
0
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE node)
{
    int currIdx;
    int parIdx;
    TYPE parVal;

    addDynArr(heap, node);
    currIdx = sizeDynArr(heap) - 1;
    parIdx = (currIdx - 1) / 2;
    parVal = heap->data[parIdx];

    while(currIdx > 0 && compare(node, parVal) == -1 ){
        swapDynArr(heap, currIdx, parIdx);
        currIdx = parIdx;
        parIdx = (currIdx - 1) / 2;
        parVal = heap->data[parIdx];
    }

}
/*	Add a node to the heap

	param: 	heap	pointer to the heap
	param: 	node	node to be added to the heap
	pre:	heap is not null
	post:	node is added to the heap
*/
void addHeap(DynArr *heap, TYPE val)
{
    /* FIXME */
    assert(heap != 0);
    int parent;

    int pos = sizeDynArr(heap);     //set entry point to last element of array
    addDynArr(heap, val);           //add value to array. Resize array if needed

    while(pos != 0)                //while not at root element
    {
        parent = (pos - 1) / 2;     //parent of current node
        if(compare(getDynArr(heap, pos), getDynArr(heap, parent)) == -1)    //if pos < parent
        {
            swapDynArr(heap, parent, pos);  //swap nodes at pos and parent index
            pos = parent;           //current position is the new parent
        }
        else return;                //if pos >= parent
    }
}
int main(int argc, char* argv[]) {	
	DynArr* b;
	int n, i;
	double t1, t2;
	#ifdef MEMORY_TEST_INCLUDED
	/* variables to hold memory used before and after creating DynArr */	
	long m1, m2;
	/* memory used BEFORE creating DynArr */
	m1 = getMemoryUsage();
	#endif

	if( argc != 2 ) return 0;
	  
	b = createDynArr(1000); 
	n = atoi(argv[1]); /*number of elements to add*/
		
	for( i = 0 ; i < n; i++) {
		addDynArr(b, (TYPE)i); /*Add elements*/
	}		
	
	#ifdef MEMORY_TEST_INCLUDED
	/* memory used AFTER creating DynArr */
	m2 = getMemoryUsage();  
	printf("Memory used by DynArr: %ld KB \n", m2-m1);
	#endif
	
	t1 = getMilliseconds();/*Time before contains()*/
	
	for(i=0; i<n; i++) {
		containsDynArr(b, i);		
	}	
	
	t2 = getMilliseconds();/*Time after contains()*/
	
	printf("Time for running contains() on %d elements: %g ms\n", n, t2-t1);
  
	/* delete DynArr */
	deleteDynArr(b);
	
	return 0;
}
int main(int argc, char* argv[])
{
	#ifdef MEMORY_TEST_INCLUDED
	// Memory used BEFORE creating LinkedList
	long m1 = getMemoryUsage();
	#endif

	if (argc != 2)
	{
		printf("Usage: %s <number of elements to add>\n", argv[0]);
		return 1;
	}
	
	DynArr *a = newDynArr(1024);
	
	int numElements = atoi(argv[1]);
	int i;
	for (i = 0 ; i < numElements; i++)
	{
		addDynArr(a, (TYPE)i);
	}

	#ifdef MEMORY_TEST_INCLUDED
	// Memory used AFTER creating LinkedList
	long m2 = getMemoryUsage();
	printf("Memory used by Dynamic Array : %ld KB \n", m2 - m1);
	#endif

	double t1 = getMilliseconds(); // Time before contains()
	for (i = 0; i < numElements; i++)
	{
		containsDynArr(a, i);
	}
	double t2 = getMilliseconds(); // Time after contains()
	printf("Time for running contains() on %d elements: %g ms\n", numElements, t2 - t1);

	deleteDynArr(a);

	return 0;
}
Example #29
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = createDynArr(2);
	
	printf("\n\nTesting addDynArr...\n");
	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]\n");
	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("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	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("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	printf("Executing test functions...\n");
	printf("\nTesting addDynArr()\n");
	addDynArr_TEST(dyn);  //Testing the add functionality under 3 conditions
	printf("\nTesting popDynArr()\n");
	popDynArr_TEST(dyn);  //Testing the pop function under 2 conditions
	printf("\nTesting topDynArr()\n");
	topDynArr_TEST(dyn);  //Testing the top function under 2 conditions
	printf("\nTesting pushDynArr()\n");
	pushDynArr_TEST(dyn); //Testing the push function under 3 conditions
	printf("\nTesting containsDynArr()\n");
	containsDynArr_TEST(dyn); //Testing the contains function under 2 conditions
	printf("\nTesting removeDynArr()\n"); 
	removeDynArr_TEST(dyn); //Testing the remove function under 2 conditions
	return 0;
}
/* 	Push an element onto the top of the stack

	param:	v		pointer to the dynamic array
	param:	val		the value to push onto the stack
	pre:	v is not null
	post:	size increases by 1
			if reached capacity, capacity is doubled
			val is on the top of the stack
*/
void pushDynArr(DynArr *v, TYPE val)
{
	assert(v!=0);
	addDynArr(v, val);
}