Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Modular Programming - What's wrong w/my code?

    I need to write a program for an automatic teller machine that dispenses money. The user should enter the desired amount (multiples of 10 dollars) and teh machine dispenses that amount using the least number of bills. The bills dispensed are 50s, 20s, and 10s. I also need to specify how many of each kind of bill to dispense. My mess of a code is attached. Help

    #include <stdio.h>
    #include <math.h>

    int GetBills(int dollars, int* fifties, int* twenties, int* tens)

    {
    *fifties = dollars / 50;
    *twenties = (dollars %= 50) / 20;
    *tens = (dollars %= 20) / 10;

    return *fifties || *twenties || *tens;
    }

    void PrintBills(int dollars, int fifties, int twenties, int tens)
    {
    printf("\nDispensing %d dollars ...", dollars)
    if (fifties)
    printf("\n%d $50 bills", fifties);
    if (twenties)
    printf("\n%d $20 bills", twenties);
    if (tens)
    printf("\n%d $10 bills", tens);
    }

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    i am not sure how you would program this in C or C++ but i think the logic needs to be set out like so...

    I have checked the logic using PHP and it works fine so u will just need to convert into wot eva u need it to be
    Code:
    void get_bills(int amount)
    {
    
     check50 = amount / 50;
    
     if(check50 > 1) //more than 50 requested
     {
    
       total50s = round_down(check50);
       remaining = amount - (total50s * 50);
    
     }
     else
     {
    
        total50s = 0;
        remaining = amount;  
     
     }
    
     check20 = remaining / 20;
    
     if(check20 > 1) //more than 20 remaining
     {
    
       total20s = round_down(check20);
       remaining = remaining - (total20s * 20);
    
     }
     else
     {
    
        total20s = 0;
        remaining = remaining;  
     
     }
     
     check10 = remaining / 10;
    
     if(check10 >= 1) //more than 10 remaining
     {
    
       total10s = round_down(check10);
    
     }
     else
     {
    
        total10s = 0;
     
     }
     
     output['50s'] = total50s;
     output['20s'] = total20s;
     output['10s'] = total10s;
    
     return output
    }
    Hope this helps

  • Users who have thanked mic2100 for this post:

    Jus S (11-22-2008)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Lost in Localhost...
    Posts
    702
    Thanks
    3
    Thanked 43 Times in 42 Posts
    Please in future wrap your code in [code] tags.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •