Hi everyone,
I am building an array recursively and am not getting the correct value when I try to recall the previous value. I have two arrays in my code. fx is up to four 1's followed by 0's and gx is a recursive calculation based on fx and the previous gx values. When I type in gx(3) I get 78, but when I type gx(4) which in my function, I have defined to be gx(3), I get 72 instead of 76 and I don't know why.
My code is below.
I am building an array recursively and am not getting the correct value when I try to recall the previous value. I have two arrays in my code. fx is up to four 1's followed by 0's and gx is a recursive calculation based on fx and the previous gx values. When I type in gx(3) I get 78, but when I type gx(4) which in my function, I have defined to be gx(3), I get 72 instead of 76 and I don't know why.
My code is below.
Code:
Function function(x As Integer) As Double
Dim fx(), gx() As Double
ReDim fx(x + 1), gx(x + 1) As Double
gx(1) = 1
Dim i, j As Integer
Dim S As Double
For i = 1 To x + 1
fx(i) = 1
Next i
If x > 3 Then
For i = 4 To x + 1
fx(i) = 0
Next i
End If
For i = 1 To x
If i <= 3 Then
S = 0
For j = 1 To i
S = j * fx(j + 1) * gx(i - j + 1) + S
Next j
gx(i + 1) = 6 / i * S
ElseIf i > 3 Then
gx(i + 1) = gx(i)
End If
Next i
function = gx(x + 1)
End Function