Write a Ruby program to compute the average values of a given array of except the largest and smallest values. The array length must be 3 or more
def check_array(nums) min = nums[0] max = nums[0] sum = 0 nums.each do |item| sum = sum + item if(item > max) max = item elsif(item < min) min = item end end return (sum-max-min).to_f/(nums.length - 2) end print check_array([3, 4, 5, 6]),"\n" print check_array([12, 3, 7, 6]),"\n" print check_array([2, 15, 7, 2]),"\n" print check_array([2, 15, 7])
Output:
4.5 6.5 4.5 7.0
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer