0

I can't seem to get the arithmetic in my greedy pset solution working. Please can someone point me in the right direction. Here is a screen shot of my code, and some of the errors showing up. Thanks!

Code:

change = round(change * 100);
int quarter = 25;
int dime = 10;
int nickle = 5;
int penny = 1;
int coin count = 0;

while (change > 0)
{
    if (change - quarter >= 0)
    {
        (change = change - quarter);
        (coin count = coin count + 1);
    }

    if (change - dime >= 0)
    {
        (change = change - dime);
        (coin count = coin count + 1);
    }

    if (change - nickle >= 0)
    {
        (change = change - nickle);
        (coin count = coin count + 1);
    }

    if (change - penny >= 0)
    {
        (change = change - penny);
        (coin count = coin count + 1);
    }
}

printf("%i", coin count)
1
  • The screenshot does not show up. Perhaps you should copy the errors and add it in? Commented Jan 30, 2017 at 12:22

3 Answers 3

1

There are simple edits that you can do to fix your code.

int coin count = 0; has a serious error in it. A variable name must be one word. There is nothing connecting coin to count, so many of the errors stem from this. A simple _ connecting coin to count will fix this error.

There are parantheses around your equations, and this is messing up your code. The terminal thinks that there is supposed to be an equation before/after the parentheses, and when it doesn't see any other equation or parentheses, it throws an error. Simply removing the parentheses will fix this error.

You do not have a ; after your printf at the end of your code. This is a simple edit.

Finally, your if statements have a serious error. In greedy, you want to use the biggest coins first. Let's walk through what happens in your code.

Let's say change, after rounding, is $1. Logically, we know the answer should be 4 coins (4 quarters), but your code outputs something different. Since 1 is less than 4, it enters the while loop. It hits your first if statement - and since 1 is greater than 0.25, a quarter is extracted. A quarter is the next biggest coin, and should be extracted, but your code moves on to the next if statement. It extracts a dime, a nickel, a penny, and the loop continues.

Obviously, something is very wrong here. Instead of your code hitting the biggest if statement and then looping again, your code hits all the loops. Look very closely at your if statements. Once you realize the mistake, it's a very simple edit.

0

Directly after subtracting the first quarter, you proceed to the first dime.

Either remove the outer while and turn your four ifs into whiles to process all quarters at once, then all dimes, or add an else before your last three ifs, so you skip the rest after one of the ifs has matched.

1
  • Thanks! many of the errors were coming from the fact that my coin counter integer was called "coin count". I changed this to just "coins" and it now works
    – JoshZ
    Commented Jan 30, 2017 at 12:45
0

I would bet that after all the corrections above, it still isn't always working correctly, particularly for $4.20. Look at the first line:

          change = round(change * 100);

Although it isn't shown, this tells me that change is a float. That means that the storage error still exists. If you print the value of change after this, using default formatting of %f, it would show 420. However, if you were to print to a large number of digits, you'd see that it was actually 419.99999999998752 (or something like that.) The point is that it's going to wreak havoc in getting the right number of coins.

Since this is covered in the class material, I'll leave it to you to figure out how to get around this problem. You have most of it in place anyways. ;-)

If this (or another answer) answers your question, please click on the check mark to accept the answer. Otherwise, it will remain in the unanswered question pool forever. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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