Thread: Calculate the minimum number of coins required to give a user change

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    Calculate the minimum number of coins required to give a user change

    Hi there! I'm trying to work out this problem where I have to calculate the minimum number of coins required to give a user change. I compiled my program with no errors. The thing is whenever I input a value, it keeps giving me the value of 4 and I don't know why. Can someone help me with this?


    Code:
    
    #include <stdio.h>
    #include <cs50.h>
    
    
    int main(void)
    {
        float balAmt;
        
        do
        {
            printf("Amount? $");
            balAmt = get_float() * 100;
        }
        while (balAmt < 0);
        
        
        int coinCounter = 0;
        
        while (balAmt > 0)
        {
        
        while (balAmt >= 25)
            balAmt = balAmt - 25;
            coinCounter++;
        
        while (balAmt >= 10)
            balAmt = balAmt - 10;
            coinCounter++;
            
        while (balAmt >= 5)
            balAmt = balAmt - 5;
            coinCounter++;
            
        while (balAmt >= 1)
            balAmt = balAmt - 1;
            coinCounter++;
            
        printf("%i/n", coinCounter);
        }
        
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    C doesn't care about indentation, it cares about code blocks. Without curly braces, only the next statement is considered part of an if/for/while expression.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Thanks a bunch! That actually fixed my problem!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Jason Dang View Post
    Thanks a bunch! That actually fixed my problem!
    Are you sure? Have you tested it enough? What if you enter 0.15 ?

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Hey algorism! Thanks for the heads up. Yea it's behaving odd when I enter 0.15. It keeps spamming 2 in the terminal. Why is that?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You don't need the while (balAmt > 0) loop at all. It's the combination of that loop and balAmt being a float that's causing the infinite loop. Basically, floating point values are often not exact. So 0.15, for instance, is not stored exactly and therefore never becomes exactly zero with your subtractions (although it gets very close to 0).

    It would actually be best to use an int instead of a float, so for instance you could transfer balAmt to an int variable and use that in your further calculations.
    Code:
    int bal = balAmt * 100 + 0.5;  // extra 0.5 to round up the value.
    You would do that after your input loop (and remove the * 100 from the input loop) then use bal in the rest of the code.

    Still, just getting rid of the while (balAmt > 0) loop will actually "fix" the problem.

    FYI, you don't need all the little counter loops, either. Instead you could do (assuming bal is an int as above) :
    Code:
        coinCounter += bal / 25;  // integer division
        bal %= 25;   // the modulus operator yields the remainder of an integer division
    (Recall that x += y is shorthand for x = x + y.)

    Same for the dimes and nickels, and then for the pennies just
    Code:
    coinCounter += bal;
    Last edited by algorism; 02-25-2017 at 03:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-30-2015, 09:15 AM
  2. Replies: 3
    Last Post: 03-09-2014, 01:44 AM
  3. Replies: 2
    Last Post: 10-26-2011, 11:18 AM
  4. C program to Calculate Change
    By CaptMorgan in forum C Programming
    Replies: 8
    Last Post: 04-07-2010, 12:31 AM
  5. Replies: 5
    Last Post: 10-08-2007, 09:44 AM

Tags for this Thread