Welcome to Dream.In.Code
Become an Expert!

Join 136,936 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,849 people online right now. 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
4 Dec, 2007 - 11:08 AM
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
+Quote Post

spullen
RE: Seeking Help With Ruby Loops And Arrays
6 Dec, 2007 - 03:28 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
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
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:46PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month