ruby - Way is my code showing the output before getting all inputs? -
i'm creating program asks number of inputs needed, , bubbles sort these inputs. code:
def bubble_sort(list) sl = list.clone sl.each_index |i| ( sl.length - - 1 ).times |j| if ( sl[j+1] < sl[j] ) sl[j], sl[j+1] = sl[j+1], sl[j] end end end end puts "enter number of elements" n = gets.chomp puts "enter #{n} elements" n.to_i.times (list ||= []) << gets.chomp bubble_sort(list) p ('sorted elements:') p (list) end
i have tested bubbling loop , it's working fine. problem have output, program asks each input in new line, whenever entered input, shows output , doesn't wait inputting other elements. can me out of how fix make program hold output until finishes inputs?
you put p(list)
(as bubble_sort(list)
, ton of other stuff) in element-gathering loop. if don't want them done part of loop, should put them outside.
Comments
Post a Comment