Skip to main content
added 165 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

Believe it or not, you got the usage of everything in your code correct. You just have a logic problem that needs to be addressed. In each case, you are calculating the number of coins of a given type in cents. In other words, you're calculating the total number of quarters, then the total number of dimes in cents as if no quarters were removed, then the same for nickels.

For example, say the amount was $0.40. It would count 1 quarter, then it would count 4 dimes = 40 cents, then 8 nickels = 40 cents. Interestingly, it gets the pennies right. If you still don't follow, try printing out the number of each coin type and run it for several values of cents.

Simply put, after the total number of quarters are calculated, their value is not subtracted from cents before calculating the number of dimes needed on the remaining balance. The code does the same thing again for nickels.

If you were to use cents as a running total instead of keeping the original amount in cents, it would work. (In other words, replace every usage of remaining with cents.)

  int cents = round(change * 100);

  int quarter_count = (cents / q);
  cents = cents % q;

  //same changes for dimes and nickels.

Simple enough? ;-)

BTW, you are one of the few people that realized that when you get to pennies, whatever was left is the right number. Most people run the calculation anyways without thinking about it, so bonus kudos to you there!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

Believe it or not, you got the usage of everything in your code correct. You just have a logic problem that needs to be addressed. In each case, you are calculating the number of coins of a given type in cents. In other words, you're calculating the total number of quarters, then the total number of dimes in cents as if no quarters were removed, then the same for nickels.

For example, say the amount was $0.40. It would count 1 quarter, then it would count 4 dimes = 40 cents, then 8 nickels = 40 cents. Interestingly, it gets the pennies right. If you still don't follow, try printing out the number of each coin type and run it for several values of cents.

Simply put, after the total number of quarters are calculated, their value is not subtracted from cents before calculating the number of dimes needed on the remaining balance. The code does the same thing again for nickels.

If you were to use cents as a running total instead of keeping the original amount in cents, it would work. (In other words, replace every usage of remaining with cents.)

Simple enough? ;-)

BTW, you are one of the few people that realized that when you get to pennies, whatever was left is the right number. Most people run the calculation anyways without thinking about it, so bonus kudos to you there!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

Believe it or not, you got the usage of everything in your code correct. You just have a logic problem that needs to be addressed. In each case, you are calculating the number of coins of a given type in cents. In other words, you're calculating the total number of quarters, then the total number of dimes in cents as if no quarters were removed, then the same for nickels.

For example, say the amount was $0.40. It would count 1 quarter, then it would count 4 dimes = 40 cents, then 8 nickels = 40 cents. Interestingly, it gets the pennies right. If you still don't follow, try printing out the number of each coin type and run it for several values of cents.

Simply put, after the total number of quarters are calculated, their value is not subtracted from cents before calculating the number of dimes needed on the remaining balance. The code does the same thing again for nickels.

If you were to use cents as a running total instead of keeping the original amount in cents, it would work. (In other words, replace every usage of remaining with cents.)

  int cents = round(change * 100);

  int quarter_count = (cents / q);
  cents = cents % q;

  //same changes for dimes and nickels.

Simple enough? ;-)

BTW, you are one of the few people that realized that when you get to pennies, whatever was left is the right number. Most people run the calculation anyways without thinking about it, so bonus kudos to you there!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

Believe it or not, you got the usage of everything in your code correct. You just have a logic problem that needs to be addressed. In each case, you are calculating the number of coins of a given type in cents. In other words, you're calculating the total number of quarters, then the total number of dimes in cents as if no quarters were removed, then the same for nickels.

For example, say the amount was $0.40. It would count 1 quarter, then it would count 4 dimes = 40 cents, then 8 nickels = 40 cents. Interestingly, it gets the pennies right. If you still don't follow, try printing out the number of each coin type and run it for several values of cents.

Simply put, after the total number of quarters are calculated, their value is not subtracted from cents before calculating the number of dimes needed on the remaining balance. The code does the same thing again for nickels.

If you were to use cents as a running total instead of keeping the original amount in cents, it would work. (In other words, replace every usage of remaining with cents.)

Simple enough? ;-)

BTW, you are one of the few people that realized that when you get to pennies, whatever was left is the right number. Most people run the calculation anyways without thinking about it, so bonus kudos to you there!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.