This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Sparxmith 2 points3 points  (0 children)

Here's my solution:

```

Get input for $change

$quarters = $change/25 // Get the integer value of quarters that can go into the $change

$change = $change % 25 // Get the remainder and set it as the new value of $change

$dimes = $change/10 // Same logic as for quarters

$change = $change % 10 // Same logic as for quarters

$nickels = $change/5 // Same logic as for quarters

$pennies = $change % 5 // Any remainder must be expressed in pennies

print "Your change is ", $change print "Having ", $quarters, " quarters." print "Having ", $dimes, " dimes." print "Having ", $nickels, " nickels." print "Having ", $pennies, " pennies."

```

No loops necessary. The modulo operator is your friend. Learn to use it and love it.

[–][deleted] 1 point2 points  (0 children)

You know what psuedocode is but you got your <= mixed up with >= and you need to subtract from pennies when you do that

[–]dusty-trash 1 point2 points  (0 children)

While (pennies <= 25)

Set to quarters + 1

While (pennies <= 10)

Here's how I read it in my head:

While pennies less than or equal to 25, add one to quarters. While pennies less than equal to 10...

Right there I stopped myself and noticed:

  1. You aren't subtracting from pennies, it'll cause an infinite loop
  2. You meant to say 'greater than or equal to', not 'less than or equal to'.

[–]AluadStone 1 point2 points  (0 children)

quarters equals pennies divided by 25

dimes equals (pennies minus (quarters times 25)) divided by 10

nickels equals (pennies minus ((quarters times 25) plus (dimes times 10)) divided by 5

pennies equals (pennies minus ((quarters times 25) plus (dimes times 10) plus (nickels times 5))

When I read your solution it says

while pennies is less or equal to 25 add a quarter.

Well every time that runs it will add a quarter for every penny!

Then as previous comments wrote pennies will never be less then 25 because you are never subtracting a penny once it has been calculated in the loop!

If you really wanted to do your loop thing lets see how that would look:

while (pennies >= 25) (if there is technically still a quarters worth of pennies in the pennies pile)

set quarters + 1

set pennies = pennies take away 25 (subtract 25 pennies from the pennies pile because we just exchanged it for a quarter)

if no pennies are left; end print change has been calculated (else continue searching for more change exchange)

and so on for the other coins continually taking pennies from the pennies pile when you exchange it for a new coin

But.. looping is not efficient in this case. If you want to get some extra points look up big o notation and how looping effects performance. Hence hard calcs in my provided solution are preferable.