Dynamic Programming - Minimum number of coins in C - c

I have looked through various questions on the site and I haven't managed to find anything which implements this by the following reasoning (so I hope this isn't a duplicate).
The problem I'm trying to solve via a C program is the following:
As the programmer of a vending machine controller your are required to compute the minimum number of coins that make up the required change to give back to customers. An efficient solution to this problem takes a dynamic programming approach, starting off computing the number of coins required for a 1 cent change, then for 2 cents, then for 3 cents, until reaching the required change and each time making use of the prior computed number of coins. Write a program containing the function ComputeChange(), that takes a list of valid coins and the required change. This program should repeatedly ask for the required change from the console and call ComputeChange() accordingly. It should also make use of “caching”, where any previously computed intermediate values are retained for subsequent look-up.
After looking around online to find how others have solved it, I found the following example applied with pennies, nickels and dimes:
Which I tried to base my code upon. But first of all, my code isn't halting, and secondly, I'm not sure if I'm incorporating the caching element mentioned in the rubric above. (I'm not really sure how I need to go about that part).
Can anyone help find the flaws in my code?
#include <stdio.h>
#include <limits.h>
int computeChange(int[],int,int);
int min(int[],int);
int main(){
int cur[]={1,2,5,10,20,50,100,200};
int n = sizeof(cur)/sizeof(int);
int v;
printf("Enter a value in euro cents: ");
scanf("%d", &v);
printf("The minimum number of euro coins required is %d", computeChange(cur, v, n));
return 0;
}
int computeChange(int cur[], int v, int n){
if(v < 0)
return -1;
else if(v == 0)
return 0;
else{
int possible_mins[n], i;
for(i = 0; i < n; i++){
possible_mins[i]=computeChange(cur, v-cur[i], n);
}
return 1+min(possible_mins, n);
};
}
int min(int a[], int n){
int min = INT_MAX, i;
for(i = 0; i < n; i++){
if((i>=0) && (a[i]< min))
min = a[i];
}
return min;
}
Any assistance will be greatly appreciated.

OP's supplied Change() algorithm incurs lots of recursion, even with the if(v < 0) return INT_MAX; correction. So much recursion that even small-ish values take millions of recursive calls.
A simple improvement is to "cache" the best solution found so far. Then when an intermediate solution is already worse than the best, no need to continue that path.
int computeChange(int cur[], int v, int n, int count_now, int *bestcount) {
if (count_now >= *bestcount) {
return INT_MAX;
}
if (v < 0) {
return INT_MAX;
}
if (v == 0) {
*bestcount = count_now;
return 0;
}
int min_count = INT_MAX;
for (int i = 0; i < n; i++) {
int count = computeChange(cur, v - cur[i], n, count_now+1, bestcount);
if (count < min_count) {
min_count = count + 1;
}
}
return min_count;
}
int bc = INT_MAX;
computeChange(cur, v, n, 0, &bc));
A secondary improvement is to attempt using large coins first
// int count = computeChange(cur, v - cur[i], n, count_now+1, bestcount);
int count = computeChange(cur, v - cur[n-i-1], n, count_now+1, bestcount);

So the below is the code snippet for your problem using memoization and dynamic programming. The complexity is O(Val*numTypesofCoins).
In the end, change[val] will give you the min number of coins for val.
int change [MAX];
int cur[]={1,2,5,10,20,50,100,200};
int n = sizeof(a)/sizeof(int);
int val= //whatever user enters to get the num of coins required.
for (i=0; i <= val; i++) {
change[i] = INT_MAX;
}
for (i=0; i < n; i++) { // change for the currency coins should be 1.
change[cur[i]] = 1;
}
for (i=1; i <= val; i++) {
int min = INT_MAX;
int coins = 0;
if (change[i] != INT_MAX) { // Already got in 2nd loop
continue;
}
for (j=0; j < n; j++) {
if (cur[j] > i) { // coin value greater than i, so break.
break;
}
coins = 1 + change[i - cur[j]];
if (coins < min) {
min = coins;
}
}
change[i] = min;
}

if you have the sum of say x and the coins of denominations say a1, a2, a3, a4..(in decreasing order)
then the answer is simply->
x/a1+(x%a2)/a3+((x%a2)%a3)/a4+...
This should hopefully give the answer

Related

C program to find integer closest to 10

I am writing a program that will take any number of integers. The program will end when the terminal 0 has been entered. It will then output the number closest to 10 (except for the terminal character). If there are several numbers closest to 10 then it should output the last number entered.
My current code does read the numbers from the input stream, but I don't know how to implement the logic so that the program will give me the number that is closest to 10.
I know, that I need to keep track of the minimum somehow in order to update the final result.
#include <stdio.h>
int main() {
int n = 1;
int number = 1;
int numberArray[n];
int resultArray[n];
int min;
int absMin;
int result;
int finalResult;
while (number != 0) {
scanf("%d", &number);
numberArray[n] = number;
n++;
}
for (int i = 0; i < n; i++) {
min = 10 - numberArray[i];
if (min < 0) {
absMin = -min;
}
else {
absMin = min;
}
resultArray[i] = absMin;
result = resultArray[0];
if (resultArray[i] < result) {
finalResult = resultArray[i];
}
}
printf("%d\n", finalResult);
return 0;
}
here's a simple code I wrote
One thing I must say is you can't simply declare an array with unknown size and that's what you have done. Even if the no. of elements can vary, you either take input the number of elements from the user OR (like below) create an array of 100 elements or something else according to your need.
#include <stdio.h>
#define _CRT_NO_WARNINGS
int main() {
int n = 0;
int number = 1;
int numberArray[100];
int resultArray[100];
int minNumber;
int *min;
do {
scanf("%d", &number);
numberArray[n] = number;
n++;
}
while (number != 0);
resultArray[0] = 0;
min = &resultArray[0];
minNumber = numberArray[0];
for (int i = 0; i < n-1; i++) {
if(numberArray[i]>=10){
resultArray[i] = numberArray[i] - 10;
}
if(numberArray[i]<10){
resultArray[i] = 10 - numberArray[i];
}
if(resultArray[i] <= *min){
min = &resultArray[i];
minNumber = numberArray[i];
}
}
printf("\n%d",minNumber);
return 0;
}
I have improved your script and fixed a few issues:
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main()
{
int n;
int number;
int numberArray[n];
while (scanf("%d", &number) && number != 0) {
numberArray[n++] = number;
}
int currentNumber;
int distance;
int result;
int resultIndex;
int min = INT_MAX; // +2147483647
for (int i = 0; i < n; i++) {
currentNumber = numberArray[i];
distance = fabs(10 - currentNumber);
printf("i: %d, number: %d, distance: %d\n", i, currentNumber, distance);
// the operator: '<=' will make sure that it will update even if we already have 10 as result
if (distance <= min) {
min = distance;
result = currentNumber;
resultIndex = i;
}
}
printf("The number that is closest to 10 is: %d. It is the digit nr: %d digit read from the input stream.\n", result, resultIndex + 1);
return 0;
}
Reading from the input stream:
We can use scanf inside the while loop to make it more compact. Also, it will loop one time fewer because we don't start with number = 1 which is just a placeholder - this is not the input - we don't want to loop over that step.
I used the shorthand notation n++ it is the post-increment-operator. The operator will increase the variable by one, once the statement is executed (numberArray entry will be set to number, n will be increased afterwards). It does the same, in this context, as writing n++ on a new line.
Variables:
We don't need that many. The interesting numbers are the result and the current minimum. Of course, we need an array with the inputs as well. That is pretty much all we need - the rest are just helper variables.
Iteration over the input stream:
To get the result, we can calculate the absolute distance from 10 for each entry. We then check if the distance is less than the current minimum. If it is smaller (closer to 10), then we will update the minimum, the distance will be the new minimum and I have added the resultIndex as well (to see which input is the best). The operator <= will make sure to pick the latter one if we have more than one number that has the same distance.
I have started with the minimum at the upper bound of the integer range. So this is the furthest the number can be away from the result (we only look at the absolute number value anyway so signed number don't matter).
That's pretty much it.

coin change program using dynamic programming knapsack with repetitions allowed

I have written the below code to implement the coin change problem: you are given n types of coin denominations of values v(1) < v(2) < ... < v(n) (all integers). Assume v(1) = 1, so you can always make change for any amount of money C. Give an algorithm which makes change for an amount of money C with as few coins as possible.
I modified the knapsack with repetitions allowed problem by setting all the values of each coin to -1. The program should then return the maximum value such that the weight of the required coins(denominations) add up to the size variable(required change). I cannot figure where i have went wrong. I should be getting an answer of -2 implying i need two coins but i'm getting -1 as the answer. Code:
#include <stdio.h>
#define max(a,b) (a > b ? a : b)
int matrix[100][100] = {0};
int knapsack(int index, int size, int weights[],int values[]){
int take,dontTake;
take = dontTake = 0;
if (matrix[index][size]!=0)
return matrix[index][size];
if (index==0){
if (weights[0]<=size){
matrix[index][size] = values[0];
return values[0];
}
else{
matrix[index][size] = 0;
return 0;
}
}
if (weights[index]<=size)
take = values[index] + knapsack(index, size-weights[index], weights, values); //knapsack(index) and not //knapsack(index-1)
dontTake = knapsack(index-1, size, weights, values);
matrix[index][size] = max (take, dontTake);
return matrix[index][size];
}
int main(){
int nItems = 4;
int knapsackSize = 10;
int weights[4] = {5,4,6,3};
int values[4] = {-1,-1,-1,-1};
printf("Max value = %dn",knapsack(nItems-1,knapsackSize,weights,values));
return 0;
}
Where am i going wrong and how can i fix this?
It is simple because, -1 > -2 and you are taking the maximum between the 2 choices at every level.
EDIT : I have impelmented a solution in which values are taken as positive, also i have made minor changes to the code, if there is something that you do not understand feel free to ask.
#include <stdio.h>
#define min(a,b) (a < b ? a : b)
#define INF 10000000
int matrix[100][100] = {0};
int knapsack(int index, int size, int weights[],int values[]){
int take = INF;
if (index == -1){
if(size == 0) return 0;
else return INF;
}
if (matrix[index][size]!=-1)
return matrix[index][size];
for(int itemcount = 0;(itemcount * weights[index]) <= size;itemcount++){
if ((weights[index] * itemcount) <= size)
take = min(take, (values[index] * itemcount) + knapsack(index - 1, size - (itemcount * weights[index]), weights, values)); //knapsack(index) and not //knapsack(index-1)
}
matrix[index][size] = take;
return matrix[index][size];
}
int main(){
int nItems = 4;
int knapsackSize = 10;
int weights[4] = {5,4,6,3};
int values[4] = {1,1,1,1};
for(int i = 0;i < 100;i++) for(int j = 0;j < 100;j++) matrix[i][j] = -1;
printf("Min value = %d\n",knapsack(nItems-1,knapsackSize,weights,values));
return 0;
}
Link to solution on Ideone : http://ideone.com/TNycZo
Here i have taken infinity as a large integer, to find minimum values, if the answer is infinity that means it is not possible to create such a denomination.

C factorial Table (1!-5!) Using For Loop

This my first post on here. I'd like to ask about a problem that I am trying to do for homework.
I'm supposed to be constructing a for loop for the "first 5 factorials" and display results as a table. I followed an example in the book, and I have my for loop and my operations set up, but I don't know what to do to produce the loop in the table. Here is my program:
#include <stdio.h>
int main(void) {
//Problem: Display a range for a table from n and n^2, for integers ranging from 1-10.
int n, factorialnumber, i;
printf("TABLE OF FACTORIALS\n");
printf("n n!\n");
printf("--- -----\n");
for (n = 1; n <= 10; n++) {
factorialnumber = factorialnumber * n;
printf("\n %i = %i", factorialnumber, n);
}
return 0;
}
I know the printf here is wrong. What would I type?
BTW, I'm using codeblocks.
The problem is that you didn't initialize the variables (e.g. factorialnumber). If it has an initial value of 6984857 let's say, the whole algorithm would be messed up.
Try this :
#include <stdio.h>
int main(void) {
//Problem: Display a range for a table from n and n^2, for integers ranging from 1-10.
int i, factorialnumber = 1;
int n = 10; // Max number to go through
printf("TABLE OF FACTORIALS\n");
printf("i i!\n");
printf("--- -----\n");
for (i = 1; i <= n; i++) {
factorialnumber *= i;
printf("%d! = %d\n", i, factorialnumber);
}
return 0;
}

I'm coding a brute-force algorithm on change making and I'm a little stuck

I need to write a program that uses brute-force method to find out how to make change the most efficiently. I'm a little confused and I'm curious if I'm on the right track. I'm writing it in C.
It does not use a greedy algorithm.
It's just confusing me is all. In the end it should output the most efficient change as toonie, loonie, quarter, dimes, nickels, pennies, in that order. (Like 1 1 0 0 1 0.)
Am I on the right track? I'm a little confused as to what I'm doing, six for loops is apparently the key, and I'm adding each iteration, but as to what's going on conceptually I'm a little confused.
#include <stdio.h>
int main(int argc, char *argv[]) {
//Input
int amount = 336;
int bestSolution = amount;
//Coins
int toonies = 0, loonies = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
int amountAfterToonies, amountAfterLoonies, amountAfterQuarters, amountAfterDimes, amountAfterNickels;
//Counters
int i, j, k, l, m, n;
for (i = 0; i < amount / 200; i++) { //Finds amount
toonies++;
amountAfterToonies = amount % 200;
for (j = 0; j < amountAfterToonies / 100; j++) {
loonies++;
amountAfterLoonies = amountAfterToonies % 100;
for (k = 0; k < amountAfterLoonies / 25; k++) {
quarters++;
amountAfterQuarters = amountAfterLoonies % 25;
for (l = 0; l < amountAfterQuarters / 10; l++) {
dimes++;
amountAfterDimes = amountAfterQuarters % 10;
for (m = 0; m < amountAfterDimes / 5; m++) {
nickels++;
amountAfterNickels = amountAfterDimes % 5;
for (n = 0; n < amountAfterNickels; n++) {
pennies++;
sum = toonies + loonies + quarters + dimes + nickels + pennies;
if (sum < bestSolution) {
bestSolution = sum;
}
}
}
}
}
}
}
printf("%d %d %d %d %d %d\n", toonies, loonies, quarters, dimes, nickels, pennies);
printf("%d\n", bestSolution);
return 0;
}
You don't find most efficiently way. You find all ways.
I suggest you something like:
toonies=amount/200;
amount%=200;
loonies=amount/100;
amount%=100;
//and so on
(Even if you want to keep the loops, separate them - there is no reason to use nested loops)
The algorithm depends on whether or not a "greedy" algorithm will work. Greedy works in the US and I think in Canada, but would not work, eg, if there were a 15-cent piece or a $3 bill.
For "greedy", you take the total amount of change received, repeatedly subtract the largest bill/coin until the amount is less than the bill/coin value, then bump down to the next smaller bill/coin.
Several ways to do it, but it could be done efficiently in two nested loops and an array containing the bill/coin values. Or you could have N sequential loops, one for each bill/coin value. In this case the loops would not be nested. Basically, each loop would be
while (amountDue >= bill_coin_values[i]) {
bills_coins_to_dispense[i]++;
amountDue -= bill_coin_values[i];
}
(Of course, the above loop can be replaced with modular division, but that kind of confuses the issue at this stage of development.)
But stick that loop in a loop that increments i through the list of values, and you have to two loop version.
Ie, the outer loop would be something like:
int numSizes = 7;
int bill_coin_values[] = {200, 100, 50, 25, 10, 5, 1};
int bills_coins_to_dispense[7];
for (int i = 0; i < numSizes; i++) {
bills_coins_to_dispense[i] = 0;
<Above loop>
}
Your solution will find all possible solutions.
What do you mean by the most efficient?
The step you need to add is to record whatever you consider to be the most efficient solution and present that as the answer at the end.
Edit - Correction; you're on the right track but if you track through the logic you first posted, you'll see you never get to adding pennies. This is because on the first iteration where you actually get a valid result, amountAfterDimes / 5 is 0 so you never add any pennies and therefore never find the correct answer.
You need to allow your search to try 0 of each coin to allow it to descend to the correct answer.
Maybe it's the Loonies and Toonies added to insomnia ... but this was too fun not to answer ...
#include <stdio.h>
struct coin {
char *name;
int value;
int number;
};
#define NUMCOINS 7
int main(int argc, char *argv[]) {
//Input
int amount = 336;
//Coins
struct coin coins[NUMCOINS]={{"toonies", 200, 0},{"loonies",100,0},{ "four bit", 50,0},{ "two bit", 25,0},{"short bit",10,0},{ "nickels",5,0},{"pennies",1,0}};
//Counters
int i;
for(i=0;i<NUMCOINS;i++)
for (;amount >= coins[i].value; amount-=coins[i].value,coins[i].number++);
for(i=0;i<NUMCOINS;i++)
printf("%s %d \n", coins[i].name, coins[i].number);
return 0;
}

why is my 3n+1 problem solution wrong

I have recently started reading "Programming Challenges" book by S. Skiena and believe or not I am kind of stuck in the very first problem.
Here's a link to the problem: 3n+1 problem
Here's my code:
#include <stdio.h>
long get_cycle(long input){
if (input == 1){
return 1;
}
else{
if (input & 1){
return 2 + get_cycle((3*input+1)>>1);
}
else{
return 1 + get_cycle(input >> 1);
}
}
}
long get_range_cycle(int k, int j){
int i;
int max = 0;
int current_cycle;
int to = k > j ? k : j;
int from = k < j ? k : j;
for (i=from; i<=to; ++i){
current_cycle = get_cycle(i);
if (current_cycle > max){
max = current_cycle;
}
}
return max;
}
int main(){
long p, q;
long re[100][3];
int i = 0;
while (scanf("%ld %ld",&p,&q) == 2){
re[i][0] = p;
re[i][1] = q;
re[i][2] = get_range_cycle(p,q);
++i;
}
int j;
for (j=0; j<i; ++j){
printf("%ld %ld %ld\n",re[j][0],re[j][1],re[j][2]);
}
}
what is wrong with my code? the input and out is exactly the same with sample.But the submission result is always run time error!
You're code seems to assume maximum 100 lines in the input file - the sample data they are testing on might be bigger? They make no explicit claim wrt the maximum set size of the input data.
I believe that the problem you seek answer for is in the answer #Elemental . If you fix that, however, your solution will time out.
What you should do is to build up a list of all answers between 0 and 1000000. This can be done in linear time (I will not give you the full answer).

Resources