ashblynn02
asked on
VB Fibonacci Code.. HELP!
I am trying to write a fibonacci sequence for Visual Basic. For some reason my code it not working.
I attached the code I have in eestuff ....
Public Class Form1
Private Sub BtnProcessData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcessData.Click
Dim txtprevious1 As Long
Dim txtprevious2 As Long
Dim txtprevious3 As Long
Dim i As int
Dim txtnumber As Int
txtprevious2 = 1
txtcurrent = txtNumber
for (i = 1 to txtcurrent)
txtprevious3 = txtprevious3
txtprevious1 = txtprevious2
txtprevious2 = txtprevious1 + txtprevious3
Next i
txtnumber = txtprevious1
End Sub
End Class
I attached the code I have in eestuff ....
Public Class Form1
Private Sub BtnProcessData_Click(ByVal
Dim txtprevious1 As Long
Dim txtprevious2 As Long
Dim txtprevious3 As Long
Dim i As int
Dim txtnumber As Int
txtprevious2 = 1
txtcurrent = txtNumber
for (i = 1 to txtcurrent)
txtprevious3 = txtprevious3
txtprevious1 = txtprevious2
txtprevious2 = txtprevious1 + txtprevious3
Next i
txtnumber = txtprevious1
End Sub
End Class
Please desk check your code. I'd suggest doing a walkthru with a peer.
ASKER
what do you mean desk check
This should do it
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = Val(TextBox1.Text)
If count < 3 OrElse count > 1000 Then
MsgBox("Try something between 3 and 1000")
Exit Sub
End If
Dim Fibo(count - 1) As Integer
Fibo(0) = 1
Fibo(1) = 1
For x As Integer = 2 To count - 1
Fibo(x) = Fibo(x - 1) + Fibo(x - 2)
Next
WholeSeries.Text = ""
For x As Integer = 0 To count - 2
WholeSeries.Text &= Fibo(x) & ", "
Next
WholeSeries.Text &= Fibo(count - 1)
Result.Text = Fibo(count - 1)
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = Val(TextBox1.Text)
If count < 3 OrElse count > 1000 Then
MsgBox("Try something between 3 and 1000")
Exit Sub
End If
Dim Fibo(count - 1) As Integer
Fibo(0) = 1
Fibo(1) = 1
For x As Integer = 2 To count - 1
Fibo(x) = Fibo(x - 1) + Fibo(x - 2)
Next
WholeSeries.Text = ""
For x As Integer = 0 To count - 2
WholeSeries.Text &= Fibo(x) & ", "
Next
WholeSeries.Text &= Fibo(count - 1)
Result.Text = Fibo(count - 1)
End Sub
End Class
ASKER
I uploaded my code with my object box
Hi ashblynn02,
The zip file you uploaded to ee-stuff only contained the solution file. You need to also include the forms that go with it...
So none of the solutions in your previous question helped you?
https://www.experts-exchange.com/questions/22852012/Fibonacci-Visual-Basic.html
The zip file you uploaded to ee-stuff only contained the solution file. You need to also include the forms that go with it...
So none of the solutions in your previous question helped you?
https://www.experts-exchange.com/questions/22852012/Fibonacci-Visual-Basic.html
>what do you mean desk check
I mean, read your code and look for stupid mistakes, like this:
txtprevious3 = txtprevious3
or like this:
Dim i As int
Dim txtnumber As Int
txtcurrent = txtNumber
for (i = 1 to txtcurrent)
I mean, read your code and look for stupid mistakes, like this:
txtprevious3 = txtprevious3
or like this:
Dim i As int
Dim txtnumber As Int
txtcurrent = txtNumber
for (i = 1 to txtcurrent)
ASKER
okay im sorry i didn't know what you meant
ASKER
I have it almost figured out I just can't figure out why it is not returning a result
Public Class Form1
Private Sub BtnProcessData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcessData.Click
Dim previous1 As Long
Dim previous2 As Long
Dim previous3 As Long
Dim number As Long
Dim fibonacci As Integer
Dim i As Integer
previous2 = 1
fibonacci = CInt(number)
For i = 1 To fibonacci
previous3 = previous3
previous1 = previous2
previous2 = previous1 + previous3
Next i
number = CInt(previous1)
End Sub
End Class
Public Class Form1
Private Sub BtnProcessData_Click(ByVal
Dim previous1 As Long
Dim previous2 As Long
Dim previous3 As Long
Dim number As Long
Dim fibonacci As Integer
Dim i As Integer
previous2 = 1
fibonacci = CInt(number)
For i = 1 To fibonacci
previous3 = previous3
previous1 = previous2
previous2 = previous1 + previous3
Next i
number = CInt(previous1)
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why do you have both fibonacci and number when they are the same?
Also, previous1-3 are numbered strangely, which makes reading your code difficult. You could make your code more understandable with something like this
current = prev1+prev2
prev2=prev1
prev1=current
Also, previous1-3 are numbered strangely, which makes reading your code difficult. You could make your code more understandable with something like this
current = prev1+prev2
prev2=prev1
prev1=current