This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CreeperGoBoom's profile picture

[solved]Maths problem? my function ATMCashOutCount buggy as it currently doesn't take away any calculations from total, do not know how to proceed.

Started by CreeperGoBoom, 12 December 2018 - 05:36 PM
CreeperGoBoom #1
Posted 12 December 2018 - 06:36 PM
ok so i have this code:


function ATMCashOutCount(amount)
cash = {}
cash.hundred = math.floor(amount/100) --[[returns number of items needed to
give equivalant to x number of $100 bills]]
cash.fifty = math.floor((amount%100)/50)
cash.twenty = math.floor((amount%50)/20)
cash.ten = math.floor((amount%20)/10)
cash.five = math.floor((amount%10)/5)
cash.dollar = math.floor(amount%5)
if cash.hundred > 0 then
  io.write(cash.hundred.."x$100, ")
end
if cash.fifty > 0 then
  io.write(cash.fifty.."x$50, ")
end
if cash.twenty > 0 then
  io.write(cash.twenty.."x$20, ")
end
if cash.ten > 0 then
  io.write(cash.ten.."x$10, ")
end
if cash.five > 0 then
  io.write(cash.five.."x$5, ")
end
if cash.dollar > 0 then
  io.write(cash.dollar.."x$1")
end

end
input = read()
ATMCashOutCount(input)

When I run it and input 70, i get 1x50, 1x20, 1x10 (=80)
How do i remove the first result from the second?

in other words

770 =
7x100 then
70 is left, subsequently 70/50 floored is 1, remaining 20 which means 1x20 completes the calculation with no 10s remaining.

Each result will then be passed to a command block to give the correct custom items chosen for my economy which is why I cant have any rogue 10s etc.

on another note if i input 80, i get 1x50, 1x20 (=70) so there seems to be a small bug to "flush" out

Do i need to code it differently? Cheers in advance.
Edited on 13 December 2018 - 05:20 AM
Dog #2
Posted 12 December 2018 - 10:49 PM
I would have a variable to track how much cash you're actually dealing with at any point in the process and work with it. For example…

cash = { }
workingCash = amount --# use this to track how much is left of the amount that you need to work with
cash.hundred = math.floor(workingCash / 100)
workingCash = workingCash - (cash.hundred * 100) --# decrement the working cash variable by the amount of cash you've already processed (all hundreds)
cash.fifty = math.floor(workingCash / 50)
workingCash = workingCash - (cash.fifty * 50) --# decrement the working cash variable by the amount of cash you've already processed (all fifties)

...

cash.dollar = math.floor(workingCash) --# whatever is left is spare dollars (rounded down in case cents were added)

This code doesn't use modulo, but it should avoid the problem you're running into. I'm not very good with math in general so I'm not sure why you're running into the problem you're seeing with your code the way it's structured.
Edited on 12 December 2018 - 09:57 PM
Bomb Bloke #3
Posted 13 December 2018 - 03:25 AM
I'd use a table filled with currency values, and then loop through them:

local bills = {100, 50, 20, 10, 5, 1}

local function ATMCashOutCount(amount)
	local cash = {}
	
	for i = 1, #bills do
		cash[ bills[i] ] = math.floor(amount / bills[i])

		if cash[ bills[i] ] > 0 then
			write(cash[ bills[i] ] .. "x$" .. bills[i] ..", ")
			amount = amount % bills[i]
		end
	end
	
	print()
	
	return cash
end

local cash = ATMCashOutCount( tonumber( read() ) )

print( textutils.serialise( cash ) )
CreeperGoBoom #4
Posted 13 December 2018 - 06:18 AM
I would have a variable to track how much cash you're actually dealing with at any point in the process and work with it. For example…
 cash = { } workingCash = amount --# use this to track how much is left of the amount that you need to work with cash.hundred = math.floor(workingCash / 100) workingCash = workingCash - (cash.hundred * 100) --# decrement the working cash variable by the amount of cash you've already processed (all hundreds) cash.fifty = math.floor(workingCash / 50) workingCash = workingCash - (cash.fifty * 50) --# decrement the working cash variable by the amount of cash you've already processed (all fifties) ... cash.dollar = math.floor(workingCash) --# whatever is left is spare dollars (rounded down in case cents were added) 
This code doesn't use modulo, but it should avoid the problem you're running into. I'm not very good with math in general so I'm not sure why you're running into the problem you're seeing with your code the way it's structured.


[SOLVED]
#Hashtag#MIssingPeiceOfThePuzzle

Thankyou, you answered my question of how do i keep track of the remaining cash, the missing piece? the variable that keeps track of it. cheers. New working code:

function ATMCashOutCount(amount)
cash = {}
workingCash = amount
cash.hundred = math.floor(amount/100) --[[returns number of items needed to
give equivalant to x number of $100 bills]]
workingCash = workingCash - (cash.hundred * 100) --keeps track of remaining cash
cash.fifty = math.floor((workingCash%100)/50)
workingCash = workingCash - (cash.fifty * 50)
cash.twenty = math.floor((workingCash%50)/20)
workingCash = workingCash - (cash.twenty * 20)
cash.ten = math.floor((workingCash%20)/10)
workingCash = workingCash - (cash.ten * 10)
cash.five = math.floor((workingCash%10)/5)
workingCash = workingCash - (cash.five * 5)
cash.dollar = math.floor(workingCash%5)
if cash.hundred > 0 then
  io.write(cash.hundred.."x$100, ")
end
if cash.fifty > 0 then
  io.write(cash.fifty.."x$50, ")
end
if cash.twenty > 0 then
  io.write(cash.twenty.."x$20, ")
end
if cash.ten > 0 then
  io.write(cash.ten.."x$10, ")
end
if cash.five > 0 then
  io.write(cash.five.."x$5, ")
end
if cash.dollar > 0 then
  io.write(cash.dollar.."x$1")
end

end
input = read()
ATMCashOutCount(input)

Now I can pass these results to a command block :D/>

Edit: Thanks Bomb Bloke that will help keep the code smaller.
Edited on 13 December 2018 - 05:19 AM