-
Mar 22nd, 2021, 10:56 PM
#1
[RESOLVED] Weighted Average formula
I've written a weighted average function from memory...
Code:
Dim numbersAsInteger(numbers.Length) As Integer
Dim weightings(numbers.Length) As Decimal
Dim sum As Decimal
For x As Integer = 0 To numbers.GetUpperBound(0)
Dim parts() As String = numbers(x).ToString.Split(New String() {" {", "}"}, StringSplitOptions.None)
numbersAsInteger(x) = CInt(parts(0))
weightings(x) = CDec(parts(1))
sum += numbersAsInteger(x) * weightings(x)
Next
Return sum / weightings.Sum
numbers is an object array, which will contain something like {1 {1}, 2 {1.5}, 3 {1}, 4 {3.5}, 5 {1}}
so numbersAsInteger will be {1, 2, 3, 4, 5}
and the parallel weightings array will be {1, 1.5, 1, 3.5, 1}
That's my basic understanding of how a weighted average works. Is that correct?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2021, 05:06 AM
#2
Re: [RESOLVED] Weighted Average formula
That's seems correct:
Weighted Average = sum (Xi*Wi)/Sum(Wi)
Xi the values
Wi the weight
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Mar 23rd, 2021, 05:45 PM
#3
Re: [RESOLVED] Weighted Average formula
Originally Posted by Delaney
That's seems correct:
Weighted Average = sum (Xi*Wi)/Sum(Wi)
Xi the values
Wi the weight
Ok thanks...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|