Explore BrainMass

Second smallest number in the list

Write a Ruby program with the following specification:

Input: A list of numbers from the keyboard.
Output: The second smallest number in the list, along with its position in the list, with 1 being the position of the first number.

© BrainMass Inc. brainmass.com July 17, 2018, 5:35 pm ad1c9bdddf

Solution Preview

Please save the following program in a file, say, number.rb . If you give less than 2 numbers as input, it won't output anything.

------------- Program text ------------

numberList = Hash.new

# Read a list of numbers from the keyboard.
puts "Enter numbers one per line."

count = 1
$stdin.each_line ...

Solution Summary

Ruby program in the solution assumes that input contains at least two numbers. If you give less than two numbers as input, it will not output anything.