Results 1 to 8 of 8

Thread: How do you use modulus operator for Greedy Algorithm

  1. #1
    Join Date
    Nov 2012
    Beans
    30

    How do you use modulus operator for Greedy Algorithm

    suppose user has given us floating point number(like 0.37$) and we want it to covert into decimal by roundingoff (mayb such that it doesn't affect on answer) and then use modulus operator on them.
    P.S
    i have been working on project that says take amount of money from user and return back such that first use biggest possible coin(quarter,dime,nickel and penny ) and give output how much coin used up in returning back.

    I wrote program it worked correctly but each time it gives me one coin less,when i debug with cout at every level problem appears on the last stage when it compensate with penny(0.01)[when a penny subtract with 0.02 it gives output of 0.009999 instead 0.01..that's creating problem]

    So i decided how about if i convert float into decimal and use modulus operator and use its remainder to give it next possible coin.
    while(coin can be used)
    {
    counter++ //coin counter
    remainder = remainder%quarter(could be penny,nickel etc) //currently working as user_input = user_input-quarter(etc)
    }
    each time coin increase
    input amount decreases

    Thanks,

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How do you use modulus operator for Greedy Algorithm

    Don't deal with your cash as fractions of your of a dollar/pound - work in integers (i.e. pennies) - and then you wont get rounding errors.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: How do you use modulus operator for Greedy Algorithm

    Quote Originally Posted by Tony Flury View Post
    Don't deal with your cash as fractions of your of a dollar/pound - work in integers (i.e. pennies) - and then you wont get rounding errors.
    True. When you're dealing with money, you want all the accuracy that you can muster, and using integers is often the easiest way of achieving this.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.

  4. #4
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    171

    Re: How do you use modulus operator for Greedy Algorithm

    why do you use in your program
    Code:
    remainder = remainder%quarter
    instead of
    Code:
    remainder = fmod(quarter,remainder)

  5. #5

    Re: How do you use modulus operator for Greedy Algorithm

    hatsoff didn't name a language, there may not even be an fmod function...

  6. #6
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    171

    Re: How do you use modulus operator for Greedy Algorithm

    Quote Originally Posted by trent.josephsen View Post
    hatsoff didn't name a language, there may not even be an fmod function...
    since hatsoff wrote
    Quote Originally Posted by hatsoff View Post
    I wrote program it worked correctly but each time it gives me one coin less,when i debug with cout at every level problem appears on the last stage
    I supposed that he was programming in C++.

  7. #7

    Re: How do you use modulus operator for Greedy Algorithm

    You are probably right.

  8. #8
    Join Date
    Nov 2012
    Beans
    30

    Re: How do you use modulus operator for Greedy Algorithm

    Got it Thank you guys!

Bookmarks

Posting Permissions

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