Explore BrainMass

Explore BrainMass

    Second smallest number in the list

    This content was COPIED from BrainMass.com - View the original, and get the already-completed 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 October 10, 2019, 4:24 am 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.