class MeanMedianMode::Median

Public Class Methods

new(num_array) click to toggle source
# File lib/mean_median_mode/median.rb, line 3
def initialize(num_array)
  puts '*'*80
  puts 'Median called'
  puts '*'*80
  num_arr = num_array
  total = num_arr.count
  if total
    self << answer_median(total, num_arr)
  end

end

Protected Instance Methods

answer_median(total, num_arr) click to toggle source
# File lib/mean_median_mode/median.rb, line 16
def answer_median(total, num_arr)
  if total
    if total%2==0
      pos1 = num_arr[((total/2)-1)]
      pos2 = num_arr[(total/2)]
      return (pos1+pos2).to_f/2
    else
      puts 'else'
      pos = ((total+1).to_f)/2
      return num_arr[pos]
    end
  else
    return 0
  end
end