Explore BrainMass

Second smallest number in the list

This content was STOLEN from BrainMass.com - View the original, and get the solution, here!

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 September 20, 2018, 12:04 am ad1c9bdddf - https://brainmass.com/computer-science/html-html5/second-smallest-number-in-the-list-459356

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.