Computer Science Canada Issue with div and mod for coin calculating mini program

 Author: Daniel23 [ Sun Mar 02, 2014 4:36 pm ] Post subject: Issue with div and mod for coin calculating mini program What is it you are trying to achieve? with your answers/code and remove the <>> What is the problem you are having? The problem that I am having is I am unsure of how to calculate the minimum amount of coins for an amount of money under \$1.00 (\$0.99-\$0.01) using div and mod. Describe what you have tried to solve this problem I have tried doing put amt div PENNIE - for all coins (pennies, nickels, dimes and quarters) as well as fiddling around with mod. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) Current code: var amt, : real const PENNIE := 0.01 const NICKEL := 0.05 const DIME := 0.10 const QUARTER := 0.25 put "" put "Coins Calculator" put "================" put "" put "Please enter an amount of money under \$1.00: ".. get amt put "" Please specify what version of Turing you are using 4.11

 Author: Panphobia [ Sun Mar 02, 2014 5:27 pm ] Post subject: Re: Issue with div and mod for coin calculating mini program Ok so according to your problem you're supposed to find the minimum amount of coins to add up to under \$1.00. So imagine you start with \$0.24, how would you yourself go about figuring out the minimum number of coins? Ok so, lets start off with quarters, can a quarter fit into 24 cents at least once? no, ok onto dimes, can dimes fit into 24 cents at least once? yes, how many times does a dime fit into 24 cents? 2 times, ok now what is the remainder after you have taken out 2 dimes, 4 cents right? Now onto nickels, can a nickel fit into 4 cents at least once? no, so onto pennies. Can pennies fit into 4 cents at least once? Yes, how many times can a penny fit into 4 cents, 4 times. What is the remainder after you take away 4 pennies from 4 cents, 0 right? So now we are at a point where we have the minimum number of coins to get to 24 cents, which is 6 coins, 4 pennies and 2 dimes. Does that make sense?

 Author: Daniel23 [ Sun Mar 02, 2014 5:57 pm ] Post subject: RE:Issue with div and mod for coin calculating mini program Yes that makes sense, but I'm not sure how to use the div and mod function to get that to show up on the ouput...

 Author: keyboardwalker [ Sun Mar 02, 2014 7:08 pm ] Post subject: RE:Issue with div and mod for coin calculating mini program div will give you the integer result after of a divison. AKA the dividend. EX 5 div 2 = 2 where as 5/2 = 2.5. mod returns the remainder of a division. 5 mod 2 = 1. Applying it to the question at hand money(s) div coin (v) = (n) the number of coins of that type. The left over money would be s mod v.

Author:  Zren [ Sun Mar 02, 2014 8:06 pm ]
Post subject:  RE:Issue with div and mod for coin calculating mini program

Instead of having variables representing an amount in dollars, which force you to use real numbers, have them represent cents. That way you can use integers. You can always convert it to dollars for the output.

 Turing: QUARTER :real := 0.25 % Dollars QUARTER :int := 25 % Cents

 Author: Raknarg [ Sun Mar 02, 2014 10:56 pm ] Post subject: RE:Issue with div and mod for coin calculating mini program What's the matter with floats? Div and mod work with floats just as well.

 Author: Insectoid [ Sun Mar 02, 2014 11:16 pm ] Post subject: Re: RE:Issue with div and mod for coin calculating mini program Raknarg @ Sun Mar 02, 2014 10:56 pm wrote:What's the matter with floats? Div and mod work with floats just as well. Floating point error. Well do I remember tearing out my hair to figure out why sometimes there would be 1 cent left over. Switching to ints solved the problem.

 Author: Zren [ Mon Mar 03, 2014 12:23 am ] Post subject: Re: RE:Issue with div and mod for coin calculating mini program Raknarg @ Sun Mar 02, 2014 10:56 pm wrote:What's the matter with floats? Div and mod work with floats just as well. I actually didn't know that (or forgot about it). Though like Insectoid said, avoiding the use of floating points (especially when dealing with money) is always best.

 Author: Bakir [ Thu Nov 18, 2021 2:33 pm ] Post subject: RE:Issue with div and mod for coin calculating mini program To anyone who needs this in the future use this put "Hey, I can calculate your coins in the most optimal way possible, just give me a value (In cents) under 1 dollar and I will calculate it!" var coins : int get coins if coins > 100 then put "Please input a value under \$1.00" get coins end if if coins < 100 then put coins div 25 var dime : int := coins - coins div 25 * 25 var dimeNum : int := dime div 10 put dimeNum var nickle : int := dime - dime div 10 * 10 var nickleNum : int := nickle div 5 put nickleNum var pennies : int := nickle div 5 put pennies end if Took me way to long to get and I noticed that people have had to do this for years. Cheers!

 :