Would this be possible?

C

Craig Schweitzer

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}â€
 
M

Matthew Moss

I'm not sure how that would work.... you're re-reading the input stream tw=
ice.

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


figures =3D STDIN.read.split("\n").map { |x| x.to_f }

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

puts "Average: #{average}"
puts "Count greater than average: #{high.size}"
 
C

Chris Alfeld

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 =3D []
while a =3D STDIN.gets
input << a.to_f
end

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

print "Average =3D #{average}\n"
print "Number of elements greater than Avg =3D #{count}\n"
 
C

Chris Alfeld

Or even better:

----
input =3D STDIN.readlines.collect {|x| x.to_f}

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

print "Average =3D #{average}\n"
print "Number of elements greater than Avg =3D #{count}\n"
----
 
D

dblack

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

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 ([email protected])
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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,061
Members
47,668
Latest member
SamiraShac

Latest Threads

Top