--8323328-1443071762-11435093038832
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1443071762-1143509303=:28832"

  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1443071762-11435093038832
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

On Tue, 28 Mar 2006, Craig Schweitzer 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 umber 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

-- 
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! http://www.manning.com/books/black
--8323328-1443071762-11435093038832--
--8323328-1443071762-11435093038832--