I have created a two-dimensional (4x4) array, where each element is a random number from -10 to 10.
I would like to now print all negative elements of this array in the terminal. How can one do this?
Here is my code so far which initialises the array, along with my current attempt to print all negative values:
a = Array.new(4) { rand(-10...10) }
a[0] = Array.new(4) { rand(-10...10) }
a[1] = Array.new(4) { rand(-10...10) }
a[2] = Array.new(4) { rand(-10...10) }
a[3] = Array.new(4) { rand(-10...10) }
a.each {|i|
a.each {|j|
puts j
}
}
puts ...) if it is negative? If so, do you want to display the element's indices as well (e.g.,"a[2,3] = -4")?randisn't very random. Use something like:SecureRandom.random_number(-10..10)instead.require 'securerandom'to enable this.