2

I'm trying to solve greedy (pset1) problem. I wrote some code, it worked fine for certain numbers, but not others.

check50: https://sandbox.cs50.net/checks/4e3a6f4684d24ab9a200253999b68c3e

My code:

#include <stdio.h>
#include <cs50.h>

int main(void) {
    float n;
    do {
        printf("O hai! How much change is owed?\n");
        n = GetFloat();
    }
    while (n<0);
    int quarters = 0, dimes = 0, nickels = 0, pennies = 0;
        while(n>=0.25) {
            n-=0.25;
            quarters++;
        }
        while(n>=0.10) {
            n-=0.10;
            dimes++;
        }
        while(n>=0.5) {
            n-=5;
            nickels++;
        }
        while(n>=0.01) {
            n--;
            pennies++;
        }
        float total = quarters + dimes + nickels + pennies;
        printf("%.0f\n", total);
}

Thanks in advance.

1
1

your problem is rounding, greedy can work, sometimes without using rounding, but checking 0.41, is not a random choice, be sure to follow the specifications and you must use the function round ()

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

and then you can use

while(c>=25)

instead while(c>=0.25)

You must log in to answer this question.

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