mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
average mean ruby program implementation
This commit is contained in:
parent
726cce3875
commit
b1b6f80a0b
1 changed files with 28 additions and 0 deletions
28
maths/average_mean.rb
Normal file
28
maths/average_mean.rb
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# A ruby program for finding average mean
|
||||||
|
|
||||||
|
module AverageMean
|
||||||
|
# Average mean = sum of elements / total number of elements
|
||||||
|
def self.average_mean(n, *array)
|
||||||
|
if n.instance_of? Integer
|
||||||
|
if n == array.size
|
||||||
|
puts "The average mean of the following elements #{array} is #{array.sum/array.size}."
|
||||||
|
else
|
||||||
|
puts "Provide the required #{n} elements properly!"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
puts "Error: Please provide number only!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Valid inputs
|
||||||
|
AverageMean.average_mean(2, 3, 1)
|
||||||
|
AverageMean.average_mean(5, 1, 2, 3, 4, 5)
|
||||||
|
AverageMean.average_mean(3, 2, 2, 2)
|
||||||
|
|
||||||
|
# Invalid inputs
|
||||||
|
AverageMean.average_mean(2, 3, 1, 5)
|
||||||
|
AverageMean.average_mean(2, 3, "a")
|
||||||
|
AverageMean.average_mean("a", 1, 2)
|
Loading…
Reference in a new issue