This is clearly a Ruby beginner question, so please be nice
This program is supposed to take user input of numbers and average the
numbers. I figured I’d create a loop, an array, and then average the
numbers by dividing by the total length of the array.
total = [ ]
until $_ == “n”
puts “Enter Number:\n”
number = STDIN.gets
total.push(number) #puts all the numbers into the array
print “add more numbers to average? y or n\n”
continue = STDIN.gets
all = total.slice(0…total.length) #slices the array no matter how large
averageall = [all.slice(0…total.length)] / total.length
puts “Average is:”, averageall,"\n"
Needless to say, I’m getting an error with the averageall, since (I
assume) it’s trying to divide by strings. I figure I need to convert the
array of strings to an array of integers. Alas, I can’t seem to track
down the correct solution. Any help would be much appreciated for this
newbie to Ruby.