summaryrefslogtreecommitdiff
path: root/avg
diff options
context:
space:
mode:
Diffstat (limited to 'avg')
-rwxr-xr-xavg31
1 files changed, 31 insertions, 0 deletions
diff --git a/avg b/avg
new file mode 100755
index 0000000..cc4c0e6
--- /dev/null
+++ b/avg
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby
+
+require 'trollop'
+
+
+def usage
+ STDERR.write "./avg [-r <d>] < <one number per line>\n"
+ exit 1
+end
+usage if not [0,2].include? ARGV.size
+
+opts = Trollop::options do
+ opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
+end
+
+
+sum = 0.0
+i = 0
+while line=STDIN.gets
+ sum += line.strip.to_f
+ i +=1
+end
+
+avg = sum/i.to_f
+
+if opts[:round] >= 0
+ puts avg.round opts[:round]
+else
+ puts avg
+end
+