Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,928 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,820 people online right now. Registration is fast and FREE... Join Now!




Dreaded Fibonacci Solution

 
Reply to this topicStart new topic

Dreaded Fibonacci Solution, How to Display: 1,1,2,3,5,8,13,21, and 55 in label

raeNet
10 Dec, 2007 - 08:37 PM
Post #1

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
This is an assignment that I've been working on for quite some time. Possibly, too long. My code displays: "144," in the label; however, the label should display: "1,1,2,3,5,8,13,21,34, and 55"

Can you help me out? Thanks in advance.



CODE


Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click

        Dim fib1 As Double = 0
        Dim fib2 As Double = 1
        Dim counter As Double
        Dim result As Double
        result = fib1 + fib2


        ' repeat to produce first 10 numbers in fibonacci sequence
        Do While counter < 11

            fib1 = fib2 + result
            result = fib2
            fib2 = fib1
            counter = counter + 1

        Loop

        ' display sequence list in label
        Me.xNumbersLabel.Text = Convert.ToString(result) + ","
    
    End Sub
End Class


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Dreaded Fibonacci Solution
10 Dec, 2007 - 11:03 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Moved to VB.NET.
User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Dreaded Fibonacci Solution
11 Dec, 2007 - 08:00 AM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
You've actually pretty much got it. You're just changing your label's text too late; by the time it gets there, the result is all added up. You should put the line of code that changes the label in the loop like so:

CODE


Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click

        Dim fib1 As Double = 0
        Dim fib2 As Double = 1
        Dim counter As Double
        Dim result As Double
        result = fib1 + fib2
        ' display the first result in the label
        Me.xNumbersLabel.Text = Convert.ToString(result) & ", "


        ' repeat to produce first 10 numbers in fibonacci sequence
        Do While counter < 11

            fib1 = fib2 + result
            result = fib2
            fib2 = fib1
            counter = counter + 1
            ' add sequence list to the label
            Me.xNumbersLabel.Text = Me.xNumbersLabel.Text & Convert.ToString(result) & ", "

        Loop
        
    End Sub
End Class


Hope that helps!
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Dreaded Fibonacci Solution
11 Dec, 2007 - 10:31 AM
Post #4

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
Yes it does. Thank you so much. I wish that I had a better understanding of sequencing my code.

Again, thank you!
User is offlineProfile CardPM
+Quote Post

Pwn
RE: Dreaded Fibonacci Solution
12 Dec, 2007 - 02:59 AM
Post #5

D.I.C Head
**

Joined: 25 Nov, 2007
Posts: 65



Thanked: 2 times
My Contributions
QUOTE(raeNet @ 11 Dec, 2007 - 11:31 AM) *

Yes it does. Thank you so much. I wish that I had a better understanding of sequencing my code.

Again, thank you!


That's an easy one. Start debugging and step into your code line by line. That will give you an exact understanding of what's happening and when and where.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 03:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month