Need help - Array and function

Hello,
I´m a girl form Germany and I need your help.
I have to do a task for university and I´m trying for hours, but it
never works :/. Maybe you can help me. I would be so happy!

The task is to make a function called “mean(a)”, for this function we
take a array we can build by ourselves, for example a = [1,4,5,7]. So
this function should summ up the elements of the array (for example 1 +
4 + 5 + 7] and then divide this sum with the count of the members (in
this example 4) --> sum/count --> also called mean. The function should
return the mean. So far i got it. But then we also should be prepared
for the case that there are no elements in the array then the function
should return “0”. But that´s what I don´t understand. We must do it all
with a loop. And I don´t understand this. The result should also be a
floating-point number.

This is what I got so far:

a = [1,2,3,4,]

def mean(a)
count = 0
summe = 0

for x in a
count += 1
summe += x
end

mean=(summe/count)

return mean

end

puts “#{mean(a)}”

Would be so nice to help me.

Thank you :slight_smile:

Hello,

The use of “for loops” isn’t very common in Ruby. It’s preferable to use
iterators like “each”.

And you don’t need to count how many elements are in the array, you can
simply use a.size to get it (just be sure to verify it’s greater than 0
before calculating the mean).

Another thing: the “return” keyword isn’t very used in Ruby because the
last expression’s value is automatically returned from each function.
See
below.

You could rewrite your solution as:

def mean(a)
sum = 0
a.each do |x|
sum += x
end

if a.size > 0
sum / a.size
else
0
end

There’s also a solution using reduce() in one line, but it’s too early
for
this.


Carlos A.
Software Engineer @ Geekie (geekie.com.br)
+55 11 97320-3878
@carlos_agarie

2013/12/17 Johanna B. [email protected]

Thank you :). But where is the return?I need to return the mean. Our
Prof said we need a return so we can later test the function. Now when I
test it in the “terminal” I can see nothing.I need something like this:

return mean

end

puts “#{mean(a)}”

But where sould I add it?Please could you explain :), would be nice!

What returns is the last expression evaluated in the method writed by
Carlos, you don’t need to call return, because the method itself will
return 0 or more than 0. Do you understand?

a = [1,2,3,4,]

def mean(a)
sum = 0
a.each do |x|
sum += x
end

if a.size > 0
sum / a.size
else
0
end
end

puts "mean(a) => #{mean(a)}

You can also still blowing your mid, this method can have less lines,
take a look:

a = [1,2,3,4,]

def mean(a)
sum = (a.inject(:+) || 0) #: Fixnum
a.empty? ? 0 : (sum./(a.size)) #: Fixnum
end

puts "mean(a) => #{mean(a)}

You can explicitly add a ‘return’, if you like to the following code
provided by Carlos:

def mean(a)
sum = 0
a.each do |x|
sum += x
end

if a.size > 0
return(sum / a.size)
else
return(0)
end
end

saji

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://enformtk.u-aizu.ac.jp
bib: http://www.researcherid.com/rid/B-9188-2009
code: https://github.com/sajinh

def main(aArray)
sum = 0
if a.count > 0
a.each {|i| sum += i }
return sum / a.count.to_f
end
0
end

a = [1,2,3,4]

puts main(a)

Well, if we’re all just going to post our own ideas…

irb(main):002:0* a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):004:0> def a.mean
irb(main):005:1> reduce(&:+) / length
irb(main):006:1> end
=> nil
irb(main):007:0> a.mean
=> 2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs