Solution: Calculate Average
Go over the implementation of calculating average value by using different methods in the Array class.
We'll cover the following...
We'll cover the following...
Courtney's version
array = []
count = 0
puts "Enter scores: "
while true
input = gets.chomp.to_i
if input == -1
break
else
array << input
count += 1
end
end
sum = 0
array.each do |number|
sum += number
end
average = sum / count
puts "Average score: #{average}"
Calculating the average using a loop
Explanation
Line 10: The count of values is maintained by incrementing count ...