Q:

Write a Ruby program to find the difference between the largest and smallest values of a given array of integers and length 1 or more

0

 Write a Ruby program to find the difference between the largest and smallest values of a given array of integers and length 1 or more.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def check_array(nums)
  max = nums[0];
  min = nums[0];
  nums.each do |item|
    if(item > max)
			max = item;
		elsif(item < min)
			min = item
    end 
   end    
  return (max-min)
end

print check_array([3, 4, 5, 6]),"\n"
print check_array([3, 4, 5]),"\n"
print check_array([3, 4])

Output:

3
2
1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now