summaryrefslogtreecommitdiff
path: root/avg
blob: ed31465d06c12f34f6a9c6fc177c59337fd20cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env ruby

require 'trollop'


cfg = Trollop::options do
  banner "avg < <one number per line>"
  opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
end

sum = 0.0
i = 0
while line = STDIN.gets
  sum += line.to_f
  i +=1
end

avg = sum/i.to_f

if cfg[:round] >= 0
  puts avg.round cfg[:round]
else
  puts avg
end