0

I implemented my greedy with modulo and it works for many numbers but not for 0.01 and 0.15

:( input of 0.01 yields output of 1 \ expected output, but not "2058567041\n" :( input of 0.15 yields output of 2 \ expected output, but not "-1884468670\n"

What happens? I cannot find out.. I searched in the forum but couldn't find a similar problem. Thank you a lot!

int main(void) 
{
float y;
do {
    printf("O hai! How much change is owed?\n");
    y = get_float();
} while (y < 0);
int rem;
int coins = 0;
int temp;

y = y * 100;
int n = round(y);

if (n >= 25) {
    rem = n % 25;
    temp = n - rem;
    coins = temp / 25;
    n = n - temp;
}
if (n >=10) {
    rem = n % 10;
    temp = n - rem;
    coins = coins + temp / 10;
    n = n - temp;
}
if (n >=5) {
    rem = n % 5;
    temp = n - rem;
    coins = coins + temp / 5;
    n = n - temp;
}
if (n >=1) {
    rem = n % 1;
    temp = n - rem;
    coins = coins + temp / 1;
    n = n - temp;
}

printf("%i\n", coins);

}

2
  • Interesting. I tested it and it works fine. Are you sure that you're compiling the correct and the latest version of your code? Did you remember to save the file before compiling? – Cliff B Mar 18 '17 at 23:45
  • Thank you for taking the time to look at it! I tried now and it pass. Weird! I was sure I saved and recompiled the code, but now I doubt.. shoudn't do things late at night :D – eelsie Mar 19 '17 at 6:59
0

Answered in comments. Please click on the check mark to accept this answer to remove it from the unanswered question pool. Let's keep up on forum maintenance. ;-)

Interesting. I tested it and it works fine. Are you sure that you're compiling the correct and the latest version of your code? Did you remember to save the file before compiling? – Cliff B 17 hours ago

Thank you for taking the time to look at it! I tried now and it pass. Weird! I was sure I saved and recompiled the code, but now I doubt.. shoudn't do things late at night :D – eelsie 10 hours ago

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .