d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > The Change Problem :( > All The Ways To Make Change
1236Next
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 12:38pm
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
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 01:01pm
use the "break" statement in the switch-case.... else "type" will always be "1"

if you want a tabulator in your printf, then type a "\t" instead of making a tab there. i've never seen that dude ;)

if you have a return somewhere, the code below won't be executed. thus in your coin() function, it makes no sense to write "else if" :)

if you have just one command after an if(..), then you can omit the brackets: {}

so your function for coin could look something like this: (still improvable, but imo much better than your version)

Code
int coin(int num) {
 if (num == 1)
   return 1;

 if ( num == 2 )
   return 5;

 if ( num == 3 )
   return 10;

 if ( num == 4 )
   return 25;

 return 0;
}
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 01:11pm
I disagree with the first statement, each case sets the type and the type variable is passed from the loop in the main to tell the switch statement where to start. It decrements downward to one like it should though.

The rest well, yeah I could clean it up a bit thanks I always forget about the /t for some reason hitting tab is easier for me.

Let me know if you find out why it is missing certain combinations. Right now low inputs like 5 or 10 work just fine but then I get to 17 and beyond and the number it misses gradually increase.

You can use the print statements to see what going on, I'm still working on it after a few hours :( .

Again I appreciate all input !!

This post was edited by Nom_Nomz on Nov 29 2012 01:12pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 01:12pm
oh and in your switch-case, if type is 1, you set type to one. thats not necessary

(you're right maybe right about your disagreement ^^ i haven't looked at the logic yet)

This post was edited by Richter on Nov 29 2012 01:15pm
Member
Posts: 632
Joined: Apr 2 2010
Gold: 50.00
Nov 29 2012 01:13pm
How can you disagree with the first statement? You need break statements in a switch.
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 01:15pm
Quote (BaghdadAssUp @ 29 Nov 2012 15:13)
How can you disagree with the first statement? You need break statements in a switch.


If I put in break statements it doesn't cycle through all of the possibilities.
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 01:16pm
Quote (Richter @ 29 Nov 2012 15:12)
oh and in your switch-case, if type is 1, you set type to one. thats not necessary

(you're right about your disagreement ^^)


yeah I just copy pasted it all like a champ
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 01:17pm
problem could be, that you want that it goes multiple times through the switch-case... but you only go through once?

here's your code in clean version: (without breaks in the switch)

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--)
 waySum += coinCounter(amount, i);

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);

if (money < 0)
 return 0;

if (money == 0)
 return 1;

int sum = 0;

switch (type) {
 case 4:
  money = money - coin(type);
  sum += coinCounter(money, type);
 case 3:
  money = money - coin(type);
  sum += coinCounter(money, type);
 case 2:
  money = money - coin(type);
  sum += coinCounter(money, type);
 case 1:
  money = money - coin(type);
  sum += coinCounter(money, type);
}
return sum;
}

int coin(int num) {
if (num == 1)
 return 1;

if (num == 2)
 return 5;

if (num == 3)
 return 10;

if (num == 4)
 return 25;

return 0;
}


This post was edited by Richter on Nov 29 2012 01:18pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 01:19pm
Quote (Richter @ 29 Nov 2012 15:17)
problem could be, that you want that it goes multiple times through the switch-case... but you only go through once?


yeah I am thinking it might need a loop of some kind that would be based on the total number of the current amount of large coins that can fit in money

for example

25 / 10 = 2

deduct 2 * 10 from money and run through

deduct 1 * 10 from money and run through

for each case ?

This post was edited by Nom_Nomz on Nov 29 2012 01:21pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 01:22pm
well i guess we need a recursive-function or something like this...

as long as the sum of the coin is higher than 25, there will be called the same function again, four times, with a smaller total value.... u know how i mean? ^^

edit: and if the sum is more or equal than 10, there will be called only 3 sub-functions

note: there will be huge amount of ways and you'll have stack overflows sooner or later xD

This post was edited by Richter on Nov 29 2012 01:23pm
Go Back To Programming & Development Topic List
1236Next
Add Reply New Topic New Poll