From: Christian Neukirchen Date: 2005-11-12T23:07:09+09:00 Subject: Re: I rea......lly need your help ( C++ ) nobu.nokada@softhome.net writes: > Hi, > > At Sat, 12 Nov 2005 13:52:15 +0900, > ashmangat@yahoo.com wrote in [ruby-talk:165395]: >> Calculate and display the average, median, and mode of the values >> entered. > > students = [] > ARGF.each do |line| > /^\d+$/ =~ line or break > students << line.to_i > end > mode, = students.inject(Hash.new(0)) {|h, s| h[s] += 1; h}.max_by {|s, n| n} > avrg = students.inject(0) {|a, s| a + s}.to_f / students.size > require 'pp' > print "The array is: " > pp students > puts "The Mode is #{mode}." > puts "The average is #{avrg}." This was exactly the kind of response I wanted to write, but you were faster. :-P Let's add the median (untested, beware): students.sort[students.size/2] > Nobu Nakada -- Christian Neukirchen http://chneukirchen.org