Write a program to find the second smallest number in an array in ruby
class SecondSmallest
def findSecondSmallest
print "Enter the size of array"
size = gets.chomp.to_i
input_array = []
print "Enter the elements of array"
while size > 0 do
input_array.push(gets.chomp.to_i)
size = size - 1
end
second_smallest = input_array.sort[1]
print "Entered array is: #{input_array}"
print "\nSecond smallest element is: #{second_smallest}"
end
end
p = SecondSmallest.new
p.findSecondSmallest
Output:
class SecondSmallest
def findSecondSmallest
print "Enter the size of array"
size = gets.chomp.to_i
input_array = []
print "Enter the elements of array"
while size > 0 do
input_array.push(gets.chomp.to_i)
size = size - 1
end
second_smallest = input_array.sort[1]
print "Entered array is: #{input_array}"
print "\nSecond smallest element is: #{second_smallest}"
end
end
p = SecondSmallest.new
p.findSecondSmallest
Output:
Enter the size of array 5
Enter the elements of array 88
44
22
11
99
Entered array is: [88, 44, 22, 11, 99]
Second smallest element is: 22
|
No comments:
Post a Comment