I've written a weighted average function from memory...
numbers is an object array, which will contain something like {1 {1}, 2 {1.5}, 3 {1}, 4 {3.5}, 5 {1}}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
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?




Reply With Quote