Topic: Adding elements in a array [IMG]

http://dl.dropbox.com/u/4156175/ruby-addition.jpg

Somebody gave me this problem to solve some time ago:
1.Generate an array of 6 numbers(X) (max. 2 digits each)
2.Add each following numbers(X) to each other and store the individual scores(Y) in a separate array
3.Input a number via keyboard, if matches one of the numbers from array Y display msg, if not restart program.

Since I`m a total noob, trying my hardest to learn programming, this is what I know so far:
1. initilize empty array [x] & [y]
2. 6.times.map{ Random.rand(1..99) }
3. add numbers using each do?
4. store in array [y]
5. compare via input to == array [y]
6. if any instance matches display msg else restart

I would be really grateful for any guidance or help with this problem.

Last edited by Big_Bird (2011-06-24 15:23:33)

Re: Adding elements in a array [IMG]

If I properly understand this task, one of the possible solutions looks like this

x = [12, 23, 7, 48, 71, 2]
y = []

x.inject do |s, i|
  y << s + i
  y.last
end

while true
  numb = gets.chomp.to_i
  if y.detect { |i| i == numb }
    puts "#{numb} has been found!"
    break
  end
end

Re: Adding elements in a array [IMG]

I`ve asked this questions around couple of sites and your answer is the most easy to read and to use I got.

Thanks for taking the time out of your day and writing a reply!

This helped alot.