Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB Fibonacci Code.. HELP!

Posted on 2007-10-02
10
Medium Priority
?
2,392 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:ashblynn02
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 20001229
Please desk check your code.  I'd suggest doing a walkthru with a peer.  
0
 

Author Comment

by:ashblynn02
ID: 20001269
what do you mean desk check
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 20001315
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ashblynn02
ID: 20001339
I uploaded my code with my object box
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20001513
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?
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22852012.html
0
 
LVL 42

Expert Comment

by:dqmq
ID: 20001534
>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)
0
 

Author Comment

by:ashblynn02
ID: 20001576
okay im sorry i didn't know what you meant
0
 

Author Comment

by:ashblynn02
ID: 20001584
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
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 2000 total points
ID: 20002103
On your form, add a wide TextBox, named txtSeries, and use this code:
Public Class Form1

    Private Sub BtnProcessData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProcessData.Click
       Dim previous1 As Long = 1
        Dim previous2 As Long
        Dim previous3 As Long
        Dim number As Long = 15
        Dim fibonacci As Long
        Dim i As Long
        previous2 = 1
        fibonacci = number

        For i = 1 To fibonacci

            previous3 = previous1
            previous1 = previous2
            previous2 = previous1 + previous3
            txtSeries.Text += previous1.ToString() + ";"
        Next
  End Sub
End Class

AW
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 20002212
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question