Thread: Count problem

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    Count problem

    Hi,

    I am required to write a program that counts the minimum amount of coins required to return a change, i.e. if user inputs .57 for example, the program has to output number 5 (two quarters, one dime, one nickel).

    What I have so far is this (

    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <math.h>
    
    
    int main(void)
    {    
        //creates a infinite loop that checks if the value is valid
        while(1)
        {
            //get a float value from the user and convert it to cents 
            printf("O hai! How much change is owed?\n");
            float v = GetFloat();
            v = v*100;
            int c = (int) v;
        
            //quarters
            if(c >= 25)    
            {
                int count = 0;
                count = (c / 25);
                c = c % 25;
                printf("%d\n", count);
                    
            }
    
    
            //dimes
            if(c >= 10)
            {
                int countd = 0;
                countd = count + (c / 10);
                c = c % 10;        
                printf("%d\n", countd);    
                            
            }
            break;
    
    
                
    
    
        }
        return 0;
    }
    Quarters part works as it should (If I remove the dimes part, compile it, run it and input .50 for example, it spits out 2), however what I do not understand is why I cannot use the "count" from quarters and then add dimes to it (c / 10).

    If I try to compile it like this I get an error message saying that

    Code:
    greedy.c:41:13: error: use of undeclared identifier 'count'; did you mean 'countd'?
    Any help greatly appreciated. Thank you

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Variables declared within braces are only valid within those braces. What you have is basically like this:

    Code:
    {
       int a = 5;
    }
    
    {
       int b = a + 1;  // not correct because a is out of scope now
    }

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    And how should I solve this problem then?

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Actually nevermind, I found the solution

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Hi, so it's me again!

    So I managed to finish whole code, it works as it should for 99% of values, however, for some values it just keeps going on and on in an infinite loop and I have no idea why


    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <math.h>
    
    
        int main(void)
        {    
            //creates a infinite loop that checks if the value is valid
            while(1)
            {
                //get a float value from the user and convert it to cents 
                printf("O hai! How much change is owed?\n");
                float v = GetFloat();
                v = v*100;
                v = round(v);
                int c = (int) v;
                int count = 0;
        
                if(c > 0)
                {
                    //quarters
                    while(c >= 25)    
                    {
                        count = (c / 25);
                        c = c % 25;
                    
                    }
        
                    //dimes
                    while(c >= 10)
                    {
                        count = count + (c / 10);
                        c = c % 10;
                    }
    
    
                    //nickels
                    while(c >= 5)
                    {
                        count = count + (c / 5);
                        c = c % 5;    
                    }
    
    
                    //pennies
                    while(c >= 1)
                    {
                        count = count + (c / 1);
                    }
            
                    //prints out the final count
                    printf("%d\n", count);    
        
                    break;
                }
            }
            return 0;
    }
    Any ideas?

    //Actually I see it now, if the value is less then 25, then program doesn't understand "count + (c / 10)". Any ideas how could I solve this?
    Last edited by Kecupochren; 12-23-2012 at 06:22 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Code:
    while(c >= 1)
    {
           count = count + (c / 1);
    }
    never changes c so, if the loop body is executed once, it will be executed an infinite number of times.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Yup, you're right! Thank you very much
    Last edited by Kecupochren; 12-23-2012 at 06:40 PM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Line 24 isn't right either. The second and subsequent time around the loop the value of count that was previously calculated is overwritten.
    Edit: Scratch that. The last four while loops are completely redundant. They never loop more than once, and may as well just be if-statements. But even then, there's no harm in just executing those statements inside the block anyway.
    You would use a while loop only if you were going to subtract the amount of one coin each time.

    Also note that a = a + b; can be written as a += b;
    Last edited by iMalc; 12-23-2012 at 07:11 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    .57 for example, the program has to output number 5 (two quarters, one dime, one nickel).
    Shouldn't it be 2 quarters, 2 pennies and 1 nickel? Granted it's still 5 coins, but don't lose your job at the register!

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Quote Originally Posted by whiteflags View Post
    Shouldn't it be 2 quarters, 2 pennies and 1 nickel? Granted it's still 5 coins, but don't lose your job at the register!
    Yeah well, it was one of the mistakes I made previously, I thought that nickel is 2 cents, not 5. I am an European so I dunno this stuff

    @iMalc: Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem count \n, \t and \b
    By jmvbxx in forum C Programming
    Replies: 2
    Last Post: 12-20-2009, 10:12 AM
  2. c count problem
    By peter121 in forum C Programming
    Replies: 2
    Last Post: 03-27-2005, 06:14 AM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM