Would this be possible?

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}”

···

--
Posted via http://www.ruby-forum.com/.

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}"

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"
----

On 3/27/06, Craig Schweitzer <cschweitzer@cfl.rr.com> wrote:

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}"

--
Posted via http://www.ruby-forum.com/\.

Hi --

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}”

See other responses; here's another possibility:

   require 'scanf'

   all =

   while float = scanf("%f")[0]
     all << float
   end

   average = all.inject(0) {|x,y| x + y } / all.size
   over = all.find_all {|f| f > average}.size

   puts "Average is #{average}"
   puts "Over average: #{over}"

David

···

On Tue, 28 Mar 2006, Craig Schweitzer wrote:

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! Ruby for Rails

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"
----