# Thread: How do you use modulus operator for Greedy Algorithm

1. 5 Cups of Ubuntu
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. ## 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.

3. ## Re: How do you use modulus operator for Greedy Algorithm

Originally Posted by Tony Flury
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.

4. 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`
Code:
`remainder = fmod(quarter,remainder)`

5. Tall Cafè Ubuntu
Join Date
Feb 2009
Beans
1,465

## 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. Gee! These Aren't Roasted!
Join Date
Dec 2012
Beans
171

## Re: How do you use modulus operator for Greedy Algorithm

Originally Posted by trent.josephsen
hatsoff didn't name a language, there may not even be an fmod function...
since hatsoff wrote
Originally Posted by hatsoff
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. Tall Cafè Ubuntu
Join Date
Feb 2009
Beans
1,465

## Re: How do you use modulus operator for Greedy Algorithm

You are probably right.

8. 5 Cups of Ubuntu
Join Date
Nov 2012
Beans
30

## Re: How do you use modulus operator for Greedy Algorithm

Got it Thank you guys!

#### Posting Permissions

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