Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,763 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,696 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Seeking help with Ruby loops and arrays

 
Reply to this topicStart new topic

Seeking help with Ruby loops and arrays

jjones08
post 4 Dec, 2007 - 12:08 PM
Post #1


New D.I.C Head

*
Joined: 4 Dec, 2007
Posts: 1


My Contributions


I need to write a complete Ruby program to prompt for and read a sequence of non-zero integers, and keep on reading them until the user types 0. Then print out the positive numbers in increasing order and the negatives in increasing order. Use an array to hold the positives and another array to hold the negatives. Use loops to print out the two arrays.

I am using text wrangler for this and am a beginner this is the code I have so far and am not sure at all if it is correct despite my research

puts "What's your name?"
name = gets.chomp
count = 0
while count =+ 1
puts "Give an integer, 0 to quit"
count = gets.chomp.to_i
end

I want it to continue prompting "Give an integer, 0 to quit" until the user types 0, then I want the program to list all of the non-zero integers the user typed
User is offlineProfile CardPM

Go to the top of the page


spullen
post 6 Dec, 2007 - 04:28 PM
Post #2


D.I.C Regular

Group Icon
Joined: 22 Mar, 2007
Posts: 330



Dream Kudos: 50
My Contributions


Here is sort of what you want, I'll let you figure out how to print out the numbers in the order that you want them to be.
CODE

nums = Array.new
puts "Enter a name"
name = gets
puts "Enter numbers, enter 0 to stop"
exitNow = false
count = 0
while !exitNow
  num = gets
  if num.to_i == 0
    exitNow = true
  else
    nums[count] = num
  end
    count = count + 1
end

puts name + "Entered: "

nums.each do |n|
  puts n.to_s
end
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 01:41PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month