How Does the Modulus Operator Work in Coin Change Algorithms?

Click For Summary

Discussion Overview

The discussion revolves around the modulus operator's role in a coin change algorithm, specifically how to break down an amount of money into quarters, dimes, nickels, and pennies. Participants are tracing the algorithm for a specific amount, A = 27, and exploring the implications of the modulus operation in this context.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant outlines the algorithm for breaking down an amount of money into coins, specifying the operations involved with the modulus operator.
  • Another participant attempts to trace the algorithm for A = 27, noting the importance of using integers with the mod operator.
  • A subsequent post corrects an earlier calculation, indicating that the value of A after applying the modulus operator should be clarified.
  • Another participant proposes an alternative breakdown of A = 27, suggesting different values for the number of dimes, nickels, and pennies.
  • Questions arise regarding the assignment of values to A after each operation, particularly how the mod operator affects the subsequent calculations.
  • A participant provides examples of the modulus operation to clarify its function, illustrating how it yields the remainder from division.

Areas of Agreement / Disagreement

Participants express differing views on the correct values for the breakdown of A = 27, with some corrections and clarifications being made. The discussion remains unresolved regarding the exact values and the implications of the modulus operation in this algorithm.

Contextual Notes

There are indications of potential errors in the calculations, particularly concerning the values assigned to dimes, nickels, and pennies. The discussion also highlights the need for clarity on how the modulus operator is applied in the context of the algorithm.

jonroberts74
Messages
189
Reaction score
0
only had a brief intro to algorithms like this

Given an amount of money A between.01 and .99, this determines the breakdown of A into quarters (q) dimes (d) nickels (n) pennies (p)


q:=A div 25
A:=A mod 25
d:= A div 10
A:= A mod 10
n:= A div 5
p:= A mod 5

I have to trace the algorithm for A=27
 
Physics news on Phys.org
jonroberts74 said:
only had a brief intro to algorithms like this

Given an amount of money A between.01 and .99, this determines the breakdown of A into quarters (q) dimes (d) nickels (n) pennies (p)


q:=A div 25
A:=A mod 25
d:= A div 10
A:= A mod 10
n:= A div 5
p:= A mod 5

I have to trace the algorithm for A=27

So simply plug ##A = 27## in. Take note that the mod operator only works with integers.

##q := A/25 = 2##
##A := 27 \space mod \space 25 = 2##

Can you continue?
 
q:=A div 25 =1
A:=A mod 25 = 2
d:= A div 10 = 2
A:= A mod 10 =5
n:= A div 5 = 5
p:= A mod 5 = 2

??
 
jonroberts74 said:
q:=A div 25 =1
A:=A mod 25 = 2
d:= A div 10 = 2
A:= A mod 10 =5
n:= A div 5 = 5
p:= A mod 5 = 2

??

Yes sorry for the typo in my prior post. I meant ##q := 1##.

I have bolded where you have made a slight error. It should read ##A := 2 \mod 10 = 2##.

EDIT: Also, while it hasn't been mentioned yet, the line for ##p## should actually read something different. Is this for a programming class? If so the line for ##d## might also be wrong.
 
Last edited:
The proper algorithm should be this regardless though:

A = 27

q:= A div 25 = 1
A:= A mod 25 = 2
d:= A div 10 = 0
A:= A mod 10 = 2
n:= A div 5 = 0
p:= A mod 1 = 2
 
  • Like
Likes   Reactions: 1 person
Its for a discrete math class

why is it A:= 2 mod 10 = 2 ?

A becomes the value from the previous line??
 
jonroberts74 said:
Its for a discrete math class

why is it A:= 2 mod 10 = 2 ?

A becomes the value from the previous line??

The mod operator gives you the remainder from doing a division.
so for example (% being the mod operator)
1%5 = 1
2%5 = 2
3%5 = 3
4%5 = 4
5%5 = 0
6%5 = 1 etc
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
10
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K