# Would this be possible?

**URL:** <https://www.ruby-forum.com/t/would-this-be-possible/56087>\
**Category:** Ruby\
**Created:** [March 28, 2006, 2:21am UTC](https://www.ruby-forum.com/t/would-this-be-possible/56087 "2006-03-28T02:21:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@craigb](https://www.ruby-forum.com/u/craigb)\
**Post date:** [March 28, 2006, 2:21am UTC](https://www.ruby-forum.com/t/would-this-be-possible/56087/1 "2006-03-28T02:21:44Z")

</div>

Would this work to find the mean average and the number of elements  
greater than the average?

# Calculating Avg and # of elements greater than Avg. in Ruby

sum = 0.0  
n = 0  
count = 0

while figure = STDIN.gets.to\_f  
sum += figure  
n += 1  
end

average = sum / n  
puts “Average = #{average}”

while figure = STDIN.gets.to\_f  
if figure \> average  
count +=1  
end  
end

puts â??Number of elements greater than Avg = #{count}â?

---

<div class="post-metadata">

**Author:** ![craigb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@craigb](https://www.ruby-forum.com/u/craigb)\
**Post date:** [March 28, 2006, 2:38am UTC](https://www.ruby-forum.com/t/would-this-be-possible/56087/2 "2006-03-28T02:38:42Z")

</div>

I’m not sure how that would work… you’re re-reading the input stream  
twice.

Anyway, here’s my version. I could compact it more, but I won’t try  
confusing you too much all at once. =)

figures = STDIN.read.split("\n").map { |x| x.to\_f }

sum = figures.inject { |s, x| s + x }  
average = sum / figures.size  
high = figures.select { |x| x \> average }

puts “Average: #{average}”  
puts “Count greater than average: #{high.size}”

---

<div class="post-metadata">

**Author:** ![craigb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@craigb](https://www.ruby-forum.com/u/craigb)\
**Post date:** [March 28, 2006, 2:43am UTC](https://www.ruby-forum.com/t/would-this-be-possible/56087/3 "2006-03-28T02:43:35Z")

</div>

There are a few problems.

- Your input loop will never exit. On EOF STDIN.gets will return nil  
and then you call nil.to\_f which is 0.0, which is a true value.

- Your output needs new lines.

Consider

* * *

input = []  
while a = STDIN.gets  
input \<\< a.to\_f  
end

average = input.inject(0) {|m,x| m = m+x} / input.size  
count = input.count {|x| x \> average}

## print “Average = #{average}\n” print “Number of elements greater than Avg = #{count}\n”

---

<div class="post-metadata">

**Author:** ![craigb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@craigb](https://www.ruby-forum.com/u/craigb)\
**Post date:** [March 28, 2006, 2:46am UTC](https://www.ruby-forum.com/t/would-this-be-possible/56087/4 "2006-03-28T02:46:48Z")

</div>

Or even better:

* * *

input = STDIN.readlines.collect {|x| x.to\_f}

average = input.inject(0) {|m,x| m = m+x} / input.size  
count = input.count {|x| x \> average}

## print “Average = #{average}\n” print “Number of elements greater than Avg = #{count}\n”
