0

For pset1 "Greedy", i believe I have successfully solved it. However, I get errors. I can't seem to find out the problem in my code either, which makes it even more frustrating.

For the first part of my code, I create float values of 'q', meaning quarter, 'd' meaning dime, etc. Then I get the user's input value and store it in a float. Next is where I get my error:

if (c < q)
    {
        return 0;
    } else {
        do
        {
            c = c - q;
            count++;
        } while (c >= q);
    }

What I want my code to do is to see if the user's input (c) is less than a quarter (q). If it is, stop the program, but if it isn't, subtract 1 quarter from the amount of change (c) and update the amount of coins used (count). I also tell my program to keep doing this until the amount change is less than a quarter. I want to repeat this code for dimes, nickels, and pennies, but I still get errors. How can I fix this?

2

A return statement ends the execution of a function immediately at the point it gets executed. If the user initially inputted a value that is less than q, your function will return and the program control will never reach the statements below. For more information about that, you may watch the short on functions!

As a tip, it is not recommended to work with floats since they are not precise. Watch the walkthroughs on float and the walkthrough on greedy from the pset specification page for more information!

You must log in to answer this question.

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