int *algorithm_1(int *arr, int *outputArr, int size, int value) { int i, k, *tempArr1, *tempArr2, *tempArr3; k = value; if (k == 1) { outputArr[0]++; return outputArr; } else if ((i = isCoin(arr, size, k))) { outputArr[i]++; return outputArr; } else { //loop through every integer from 1 to "value" for (i = 1; i < k; i++) { //create and initilize three temprorary arrays of size "size" to all 0's tempArr1 = createArr(size); tempArr2 = createArr(size); tempArr3 = createArr(size); initArr(tempArr1, size); initArr(tempArr2, size); initArr(tempArr3, size); //recursively call algorithm_1 on array from 1 -> i and i + 1 -> "value" tempArr1 = algorithm_1(arr, tempArr1, size, i); tempArr2 = algorithm_1(arr, tempArr2, size, k - i); //add the sum of all elements at each index of tempArr1 and temArr2 together, assign to tempArr3 addArr(tempArr1, tempArr2, tempArr3, size); //if this new sum of elements array (tempArr3) is less than the running min (outputArr) or the current output array is not a solution, then replace outputArr with tempArr3 if ((sumArray(tempArr3, size) < sumArray(outputArr, size)) || !coinCheck(outputArr, arr, size, k)) { copyArr(tempArr3, outputArr, size); } free(tempArr1); free(tempArr2); free(tempArr3); } } return outputArr; }
int main(void) { float amount_dollars; int amount_cents; int coin_value = 0; do { printf ("How much?: \n"); amount_dollars = GetFloat(); amount_cents = round(amount_dollars * 100); } while (!(amount_dollars > 0 )); remaining_change = amount_cents; do { if (remaining_change >= 25){ coin_value = 25; } else if (remaining_change >= 10){ coin_value = 10; } else if (remaining_change >= 5){ coin_value = 5; } else if (remaining_change < 5){ coin_value = 1; } coinCheck(coin_value); } while (remaining_change != 0); printf("%d\n", num_coins); }