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.
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.
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.
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.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
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
On Wed, Dec 18, 2013 at 3:37 AM, Johanna Bettina <lists@ruby-forum.com>wrote:
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.Like this:
return mean
end
puts "#{mean(a)}"
But where sould I add it?Please could you explain :), would be nice!