Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Issue with div and mod for coin calculating mini program
Author Message
Daniel23

Posted: 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?

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

Panphobia

Posted: 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?
Daniel23

Posted: 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...
keyboardwalker

Posted: 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.
Zren

Posted: 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
Raknarg

Posted: 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.
Insectoid

Posted: 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.
Zren

Posted: 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.

Bakir

Posted: 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
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!
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 9 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: