From: dblack@... Date: 2006-03-28T10:28:40+09:00 Subject: Re: Would this be possible? --8323328-1443071762-1143509303=:28832 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-1143509303=:28832 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 =3D 0.0 > n =3D 0 > count =3D 0 > > while figure =3D STDIN.gets.to_f > sum +=3D figure > n +=3D 1 > end > > average =3D sum / n > puts "Average =3D #{average}" > > while figure =3D STDIN.gets.to_f > if figure > average > count +=3D1 > end > end > > puts =E2=80=9CNumber of elements greater than Avg =3D #{count}=E2=80=9D See other responses; here's another possibility: require 'scanf' all =3D [] while float =3D scanf("%f")[0] all << float end average =3D all.inject(0) {|x,y| x + y } / all.size over =3D all.find_all {|f| f > average}.size puts "Average is #{average}" puts "Over average: #{over}" David --=20 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-1143509303=:28832-- --8323328-1443071762-1143509303=:28832--