0

After 2 days of getting the same result with pset1 greedy I am a bit desperate here to figure out what I am missing.

My pset1 greedy compiles properly but it doesn't yield correct results. I ran check50 and it seems like it only counts quarters correctly and doesn't add other coin counts to it.

Here is what I also tried, but with the same exact result:

  • having different integers for each coin denomination and then adding them all at the end
  • having separate names for leftover change after each of the denominations was counted
  • after checking for quarters, formulating the conditions as e.g., less than 25 and more than or equal to 10.
  • any combination of the above

I suspect there is something after the counting of quarters that I am not getting right. I have rewatched many shorts, walkthroughs and I can't find what else it is that I am getting wrong.

I would love it if someone could point me to the core of my problem!! Thanks so much for your time.

PS: I am not including the beginning of the code that has libraries and int main(void).

{ float change;

// ask how much change
do
{
    printf("How much change? ");
    change = GetFloat();
}
while(change <= 0);

int change_cents = 0;
int leftover = 0;
int coins = 0;

change_cents = round(change * 100);

// if user gives us number larger than or equal to quarter
// calculcate integer = how many quarters are in the change
// then calculate leftover                 
if (change_cents >= 25)
{
    coins = change_cents / 25;
    leftover = change_cents % 25;
}

// if leftover is larger than or equal to a dime
// calculate how many dimes are in the change
// calculate leftover
else if (leftover >= 10)
     {   
        coins = coins + (leftover / 10);
        leftover = leftover % 10;
     }

     // if leftover is larger than or equal to nickel
     // calculate how many nickels - there could be only 1 max
     // calculate leftover
     else if (leftover >= 5)
            {
                coins = coins + (leftover / 5);
                leftover = leftover % 5;
            }

         // leftover is equal to number of pennies
         else
            {
                coins = leftover;
            }

//print the number of total coins 
printf("%d\n", coins);

}

1 Answer 1

0

You need to subtract the value of the quarter every time the if loop is executed. You also don't really need int leftover. This is what you should try to follow:

do
{
if (//change is more than a quarter)
{
    do
    {
        //subtract change
        //add a coin
    }
    while (change >= 25);
}

if (//dime value)
{
    do
    {
        //subtract value;
        //add coins;
    }
    while(//change is between dime and quarter);
}
//do same thing for nickels and pennies
}
while (change > 0)
4
  • Thank you for answering.I thought I was doing that by calculating the value of the leftover. So e.g., after I have number of quarters, I calculate leftover value (with modulo). Then after I have number of dimes, I updated the leftover value again etc. Is that an incorrect way to think about what you are suggesting?
    – ZuzkaP
    Commented Mar 1, 2015 at 17:00
  • That should be the correct way, however I feel like you should put a do-while loop in the if-loop. Check my edits Commented Mar 2, 2015 at 4:49
  • Thank you again!
    – ZuzkaP
    Commented Mar 18, 2015 at 23:54
  • 1
    If this works, please mark this question as resolved. Thank you! Commented Mar 19, 2015 at 3:44

You must log in to answer this question.

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