diff --git a/app/controllers/calculations_controller.rb b/app/controllers/calculations_controller.rb index f143fcc..dc4aa97 100644 --- a/app/controllers/calculations_controller.rb +++ b/app/controllers/calculations_controller.rb @@ -83,27 +83,72 @@ def descriptive_statistics # The numbers the user input are in the array @numbers. # ================================================================================ - @sorted_numbers = "Replace this string with your answer." + # @squared_numbers = [] - @count = "Replace this string with your answer." + # @numbers.each do |num| + # square = num * num + # @squared_numbers.push(square) + # end - @minimum = "Replace this string with your answer." + # @sorted_numbers = @squared_numbers - @maximum = "Replace this string with your answer." + ####################SORTED NUMBERS#################### + @sorted_numbers = @numbers.sort - @range = "Replace this string with your answer." + ####################COUNT#################### + @count = @numbers.size - @median = "Replace this string with your answer." + ####################MINIMUM#################### + @minimum = @numbers.min - @sum = "Replace this string with your answer." + ####################MAXIMUM#################### + @maximum = @numbers.max - @mean = "Replace this string with your answer." + ####################RANGE#################### + @range = @numbers.max - @numbers.min - @variance = "Replace this string with your answer." + ###################MEDIAN#################### + if(@count % 2 == 0) + @median = (@sorted_numbers[(@count/2).to_i - 1] + @sorted_numbers[(@count/2).to_i])/2.0 + else + @median = @sorted_numbers[(@count/2).to_i] + end - @standard_deviation = "Replace this string with your answer." + ###################SUM#################### + @sum = @numbers.sum + + ###################MEAN#################### + @mean = @sum/@count + + ###################VARIANCE#################### + @calculation = [] + + @numbers.each do |num| + difference = num - @mean + @calculation.push(difference * difference) + end + + @variance = (@calculation.sum) / (@count) + + ###################STANDARD DEVIATION#################### + @standard_deviation = @variance ** (1.0/2.0) + + ###################MODE#################### + counter = Hash.new(0) + + @sorted_numbers.each do |num| + counter[num] += 1 + # p counter + end + + highest_mode = counter.max_by{|key,value| value}[1] #counter.sort_by{|key,value| value}[-1] + values = counter.select{|key,value| value==highest_mode} + + @mode = values + + #values.select{|k,v| v == hash.values.max} + #counter.invert["key"=>highest_mode] - @mode = "Replace this string with your answer." # ================================================================================ # Your code goes above.