/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap) { int last = sizeDynArr(heap) - 1; putDynArr(heap, 0, getDynArr(heap, last)); removeAtDynArr(heap, last); _adjustHeap(heap, last, 0); }
/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap, comparator compare) { int last = sizeDynArr(heap)-1; //assert(last !=0); /*at least one element!*/ putDynArr(heap,0,getDynArr(heap,last)); removeAtDynArr(heap,last); /*remove last element*/ _adjustHeap(heap,last,0, compare); }
/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap) { /* FIXME */ assert(heap->size != 0); int max = sizeDynArr(heap); //last element of array putDynArr(heap, 0, getDynArr(heap, (max - 1))); //set array index 0 to last element removeDynArr(heap, (max - 1)); //remove last element of array _adjustHeap(heap, max, 0); //adjust heap from root to last }
/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap) { /* FIXME */ int last = sizeDynArr(heap)-1; assert (heap != 0); /* make sure we have at least one element */ /* Copy the last element to the first position */ if(last!=0) {putDynArr(heap, 0, getDynArr(heap, last)); } removeAtDynArr(heap, last); /* Remove last element.*/ _adjustHeap(heap, last-1, 0);/* Rebuild heap */ }
/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap) { int last; assert( sizeDynArr( heap ) > 0 ); last = sizeDynArr( heap ) - 1; /* Copy the last element to the first */ putDynArr( heap, 0, getDynArr( heap, last ) ); /* Remove last element */ removeAtDynArr( heap, last ); /* Rebuild heap */ _adjustHeap( heap, last, 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; }
// this main function contains some int main(int argc, char* argv[]){ DynArr *dyn; dyn = newDynArr(2); int i; 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"); return 0; }
void pushDynArr(dynArr_p da, TYPE elem) { putDynArr(da, da->size, elem); }
//Brute force/Divide and Conquer //To make change for A cents: //If there is a K-cent coin, then that one coin is the minimum //Otherwise, for each value i < K, //Find the minimum number of coins needed to make i cents //Find the minimum number of coins needed to make K - i cents //Choose the i that minimizes this sum DynArr * changeslow(DynArr *V, int A) { // Locals DynArr *MyCoins; // Return array of coin counts DynArr *countsAr; // Array of counts for each i DynArr *lower; // Return from lower half DynArr *upper; // Return from upper half DynArr *compiledAr[A]; // Array of pointers to each of the i solutions int i = 0; int j = 0; int iMin = 0; // Initialize the dynamic arrays that are // the size of V MyCoins = createDynArr(sizeDynArr(V)); lower = createDynArr(sizeDynArr(V)); upper = createDynArr(sizeDynArr(V)); for(i = 0; i < sizeDynArr(V); i++) { addDynArr(MyCoins, 0); addDynArr(lower, 0); addDynArr(upper, 0); } // Initialize the array that is used to hold all the i counts // Adding 1 to be able to loop from 1 in the main recursion loop countsAr = createDynArr(A + 1); for(i = 0; i < A + 1; i++) { addDynArr(countsAr, 0); } // Base case check if the A value has a coin // exactly equal to its value for(i = 0; i < sizeDynArr(V); i++) { if(getDynArr(V, i) == A) { putDynArr(MyCoins, i, getDynArr(MyCoins, i) + 1); putDynArr(countsAr, i, 1); return MyCoins; } } // Loop through all i from 1 to A and recurse on // A - i and A for(i = 1; i < A + 1; i++) { // Loop to pairwise sum the two for(j = 0; j < sizeDynArr(V); j++) { putDynArr(MyCoins, j, getDynArr(lower, j) + getDynArr(upper, j)); } // Loop to get the sum of the coins in MyCoins just created for(j = 0; j < sizeDynArr(MyCoins); j++) { putDynArr(countsAr, i, getDynArr(countsAr, i) + getDynArr(MyCoins, j)); } // Put the combined solution into the array of pointers compiledAr[i] = MyCoins; } // Find the minimum in the countsAr array for(i = 0; i < A + 1; i++) { if(iMin > getDynArr(countsAr, i)) iMin = i; } // Since we are using the i after the for we need to dec 1 return compiledAr[i -1]; }