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

Sum of z

Started by Dr_Zarreh, 11 May 2012 - 06:43 PM
Dr_Zarreh #1
Posted 11 May 2012 - 08:43 PM

cats = {10,11,12,8,100}
for i,v in ipairs(cats) do
t = #cats
v/t = z
print(?) - -sum of z
end

Basically i want it to print the sum of z, how would i go about getting the sum?
MysticT #2
Posted 11 May 2012 - 09:00 PM
You need to initialize the variable with 0 and then add the result to it on each iteration:

local cats = {10,11,12,8,100}
local z = 0
local t = #cats
for i, v in ipairs(cats) do
  z = z + (v / t)
  print(z)
end