Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Finding the mean of a set of numbers

Asked by
RSoMKC 45
10 years ago

Can someone explain why this script results in this:

# = Random number

#
#
#
#
#
-
0
-
0
function Add()  
    x =  First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
end

First = script.findMean.First--.Value
Second = script.findMean.Second--.Value
Third = script.findMean.Third--.Value
Fourth = script.findMean.Fourth--.Value
Fifth = script.findMean.Fifth--.Value
Plus = script.findMean.Plus--.Value

--

First.Value = (math.random(1,10))
Second.Value = (math.random(1,10))
Third.Value = (math.random(1,10))
Fourth.Value = (math.random(1,10))
Fifth.Value = (math.random(1,10))

--

print(First.Value)
print(Second.Value)
print(Third.Value)
print(Fourth.Value)
print(Fifth.Value)

--

print("-")

Plus.Value = Add()
print(Plus.Value)

print("-")

print(Plus.Value / 5)

How I've got it set up: http://i.imgur.com/unSZNlk.png

2 answers

Log in to vote
2
Answered by
Bubby4j 231 Moderation Voter
10 years ago

You never return a value in Add(), change x = ... to return ...

function Add()
    return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
end

Also, be careful, if you call Add() before First, Second, etc, are defined, it'll error. It's better to define variables before functions.

Here's a rewritten version:

First = script.findMean.First
Second = script.findMean.Second
Third = script.findMean.Third
Fourth = script.findMean.Fourth
Fifth = script.findMean.Fifth
Plus = script.findMean.Plus

function Add()  
    return First.Value + Second.Value + Third.Value + Fourth.Value + Fifth.Value
end

--

First.Value = (math.random(1,10))
Second.Value = (math.random(1,10))
Third.Value = (math.random(1,10))
Fourth.Value = (math.random(1,10))
Fifth.Value = (math.random(1,10))

--

print(First.Value)
print(Second.Value)
print(Third.Value)
print(Fourth.Value)
print(Fifth.Value)

--

print("-")

Plus.Value = Add()
print(Plus.Value)

print("-")

print(Plus.Value / 5)

By the way, this really isn't a good way to do it. It's better to use a table rather than use a bunch of NumberValues.

Here's a simple way to get the average of numbers in a table:

local numbers = {23,5,2,12,10}

function average(table)
    local sum = 0
    for i = 1, #table do
        sum = sum + table[i]
    end
    return sum/#table
end

average(numbers) --> 10.4

0
Thank you! I wasn't aware that tables were a thing! RSoMKC 45 — 10y
2
Tables are amazing! You can do anything with tables. See http://wiki.roblox.com/index.php/Table Bubby4j 231 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

The only main problem I can see, is that the function is above your variables, and when you are trying to access your variables, it can't, because that part of the script hasn't been loaded yet.

Try putting the function below your variables.

0
That's wrong, the function Add() is called AFTER the variables are defined. Lua isn't compiled :) Bubby4j 231 — 10y
0
Really? If that is wrong, then why did you suggest the same exact thing? Don't be a hypocrite . AmericanStripes 610 — 10y
0
I didn't suggest the same thing. You were implying that there would be an error because the variables were defined after the function. The fact is that it doesn't matter, as long as the variables are defined before the function is actually called. Bubby4j 231 — 10y
0
Test this script, it works: http://pastebin.com/BCiU8GGR Bubby4j 231 — 10y
View all comments (2 more)
0
Lua actually is compiled. It's compiled at run time. Functions and variables must be defined before you use them. (But you don't need to actually set them to anything) User#11893 186 — 10y
0
By compiled I meant compiled to machine code. Bubby4j 231 — 10y

Answer this question