The problem is to have a user enter a value and then to find all the ways using pennies, nickels, dimes, and quarters that it can be given. Here is my code. It works for the first few entries but something is missing that makes it's error larger the higher the value entered.
Any help would be greatly appreciated.
Code
#include <stdio.h>
int coinCounter(int, int);
int coin(int);
int main() {
int amount;
int i;
int waySum = 0;
printf("Please enter the amount you wish to check in cents: ");
scanf("%d", &amount);
for (i = 4; i >= 1; i--) {
//printf("%d\n", waySum);
waySum = waySum + coinCounter(amount, i);
//printf("%d\n", waySum);
}
printf("There are %d ways to make change with the amount %d\n", waySum, amount);
return 0;
}
int coinCounter(int money, int type) {
printf("%d\n", money);
//printf("%d\n", type);
if ( money < 0 ) {
return 0;
}
else if ( money == 0 ) {
return 1;
}
else {
int sum = 0;
switch (type) {
case 4: type = 4;
money = money - coin(type);
sum += coinCounter(money, type);
case 3: type = 3;
money = money - coin(type);
sum += coinCounter(money, type);
case 2: type = 2;
money = money - coin(type);
sum += coinCounter(money, type);
case 1: type = 1;
money = money - coin(type);
sum += coinCounter(money, type);
}
return sum;
}
}
int coin(int num) {
if ( num == 1 ) {
return 1;
}
else if ( num == 2 ) {
return 5;
}
else if ( num == 3 ) {
return 10;
}
else if ( num == 4 ) {
return 25;
}
else {
return 0;
}
}
This post was edited by Nom_Nomz on Nov 29 2012 12:56pm