Q:

Write a Ruby program to compute the average values of a given array of except the largest and smallest values

0

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

All Answers

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

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

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