# My Pset1 greedy code produces no errors and the wrong answer

I changed all the "remaining" to cents and its didn't work. I still can't see the logic why I made the made the change. I want to understand this. Here is my code:

``````#include <cs50.h>
``````

# include

int main(void)

{ float change = 0; int q = 25; int d = 10; int n = 5; int cents = 0; int final_coin_count = 0;

do { printf("How much change is owed "); change = get_float(); if (change < 0 || change == 0) printf ("You can not have less than zero change \n"); }

while (change <= 0);

``````  int cents = round(change * 100);

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

int dime_count = (cents / d);
cents = cents % d;

int nickel_count = (cents / n);
cents = cents % n;

final_coin_count = quarter_count + dime_count + nickel_count + cents;
``````

{ printf("%i\n", final_coin_count);

}

}

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.

• Do you mean all I have to do is type "cents" where ever I have typed "remaining" ? That's how I am reading this. Commented May 20, 2017 at 21:23
• exactly. cents will keep a running total as you remove coins. Commented May 20, 2017 at 22:02
• It sounded simple. I edited the code and as i expected it didn't work. If the total number of quarters is calculated but not being subtracted from cents should there not be a line of code like int cents = round(change * 100); int quarter_count = (cents / q); remaining = cents - quarter_count or something to tell remaining to subtract? Commented May 20, 2017 at 22:26
• Without seeing exactly what you did, I don't know what you did wrong. However, look at my edited answer for the changes to quarters and repeat for nickels and dimes. (I believe I said to replace just remaining with cents. Guess I should have said not to change anything else.) Commented May 21, 2017 at 1:38