Answers for "lua calculate average number"

Lua
0

lua calculate average number

function math.average(t)
  local sum = 0
  for _,v in pairs(t) do -- Get the sum of all numbers in t
    sum = sum + v
  end
  return sum / #t
end

print(math.average({9, 10, 12, 13, 13, 13, 15, 15, 16, 16, 18, 22, 23, 24, 24, 25})) -- Result: 16.75
Posted by: Guest on November-14-2020

Code answers related to "lua calculate average number"

Browse Popular Code Answers by Language