Skip to content

Instantly share code, notes, and snippets.

@tucano
Created March 20, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tucano/c836b4f47036f43e1553 to your computer and use it in GitHub Desktop.
Save tucano/c836b4f47036f43e1553 to your computer and use it in GitHub Desktop.
def get_average(array)
sum = 0.0
result = 0.0
if array.length > 0 then
array.each do |item|
sum += item
end
result = sum / array.length
end
return result
end
data = [3.5, 4.7, 8.6, 2.9]
average = get_average(data)
puts "The average is #{average}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment