Forum: Ruby helpppp please.my head hurt and i m about to beat this sit up

614aa6cb0cc315b8ef993dea32679642?d=identicon&s=25 John Yd (abettertomorrow80)
on 2014-06-19 22:30
data=[0,1,2,-1,-3,-4,-6,-3]
newarray =[]

sum  = 0
i = 0

while i < data.length
  val1 = data[i]
  j = i+1
  while j < data.length
    val2 = data[j]
    sum = val2 + val1

    newarray << val2 if sum == 0
    newarray << val1 if sum == 0

    j = j+1
  end
  i = i+1

end

p newarray



#comment. the above give me the right answer which is -1 and 1...but it
doesnt give me 3 and -3...i need 3 and -3 also...how i do that
#comment. so it stop after finding the first two numbers that add to
zero..how do i make it keep going till the end of array...it should
include 3 and -3.
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2014-06-19 23:14
I think you meant to have one of the 3s be positive in your data array

data=[0,1,2,-1,3,-4,-6,-3]

same code gave me this result     [-1, 1, -3, 3]
614aa6cb0cc315b8ef993dea32679642?d=identicon&s=25 John Yd (abettertomorrow80)
on 2014-06-19 23:19
thanks man lol...i slept at 3am last nite....
5b972395a92333843018b4add8af0437?d=identicon&s=25 Damián M. González (igorjorobus)
on 2014-06-22 02:56
That's nothing dude, must of us don't sleep, just coffee coffee
coffee...
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.