In: Computer Science
Ruby programming
Create a Ruby program which reads numbers, deposits them into an array and then calculates the arithmetic mean (otherwise known as the average), the median (that is, the number that splits the set of value in half with half being above and half being below the number), and the standard deviation (that is, the average of distance each number is from the mean then squared). Further information about these calculations can be found here:
average: http://en.wikipedia.org/wiki/Average
(add up all the numbers in the set and divide that total by the
quantity of values in the set)
median: http://en.wikipedia.org/wiki/Median
(for a set that holds an odd number of values, sort the numbers in
the set and then take the value in the middle of the set. for a set
that holds an even number of values, sort the numbers in the set
and find the average of the two values in the middle of the
set).
standard deviation:
http://en.wikipedia.org/wiki/Standard_deviation
(find the average of the set. for each value in the set, calculate
the difference between each value and the average. square each of
these difference. find the average of these squared differences.
take the square root of this average).
IN ORDER TO RECEIVE FULL CREDIT, YOUR CODE SHOULD PROPERLY READ VALUES, STORE THEM IN AN ARRAY AND MANIPULATE THE ARRAY TO ARRIVE AT THE DESIRED CALCULATIONS.
Hint : In order to have Ruby calculate the square root of a numeric value named i, please say squareRootOfI = Math::sqrt( i )
A number of different program dialogues describe the program I am looking for.
Math Calculator
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
N
2 4 4 4 5 5 7 9
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
A
Average = 5.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
M
Median = 4.5
N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
S
Standard Deviation = 2.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
C
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
N
4 4 4 6
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
N
6 3 1
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
A
Average = 4.0
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
M
Median = 4
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
S
Standard Deviation = 1.603
[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear [Q]uit:
Q
Working code implemented in Ruby and appropriate comments provided for better understanding.
Source Code for Math_Calculator.rb:
STDOUT.sync = true
#squareRootOfI = Math::sqrt( i )
class Math_Calculator
def prompt
puts "[N]umbers [A]verage [M]edian [S]tandard Deviation [C]lear
[Q]uit:"
@inputs1=gets.chomp
testing(@inputs1)
end
def testing(inputs)
case inputs
when "N" then inn
when "M" then median
when "A" then average
when "S" then deviation
when "C" then cleared
when "Q" then quit
else
prompt
end
end
def inn
@arr_inputs = gets.chomp
input = @arr_inputs.split(" ").map(&:to_i)
@avrg = (input.inject(:+)/(input.size).to_f)
input += input.map {@arr_inputs}
testing @arr_inputs
end
def median
medi = @arr_inputs.split(" ").map(&:to_i)
medium = medi.sort
medisize = medium.size
if medisize.even?
eve = (medisize) / 2
puts eve
@median = ((medium[eve-1] + medium[eve]) / 2.0)
else
odd = ((medisize/2).to_i)
puts odd
@median = medium[odd]
end
puts "The median is: #{@median}"
prompt
end
def average
puts "The average is: #{@avrg}"
prompt
end
def deviation
dev = @arr_inputs.split(" ").map(&:to_i);
sum = 0
i = 0
while i < dev.size
sum += (dev[i] - @avrg)**2
i += 1
end
squareRootOfI = Math::sqrt(sum/(dev.size))
puts "Standard Deviation: #{squareRootOfI}"
prompt
end
def cleared
@arr_inputs = []
prompt
end
def quit
@arr_inputs = []
prompt
end
def quit
exit
end
end
calcu = Math_Calculator.new
calcu.prompt
Code Screenshots:
Sample Output Screenshots: