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

Join 86,269 Programmers. There are 1,882 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

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



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
+Quote Post


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


D.I.C Regular

Group Icon
Joined: 22 Mar, 2007
Posts: 330

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
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:29AM

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