Results 1 to 6 of 6

Thread: Need help ASAP with a program on vb 6.0

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    3

    Question Need help ASAP with a program on vb 6.0

    The sequence of Fibonacci numbers begins with 1,1,2,3,5,8,13,21,34,..... and then continues with each sucessive number being the sum of the previous two.
    I am to write a program that gets a number through a textbox and then prints the first Fibonacci numbers in a picture box.
    Example user enters 7 then in pictue box should show 1,1,2,3,5,8,13. The first 7 fibonacci numbers.
    Here is what I have and it doesn't work. I just need it basic.

    Dim num1 As Integer
    Dim num2 As Integer
    Dim temp As Integer
    Dim n As Integer
    Dim i As Single

    Private Sub txtnumber_Change()
    n = txtnumber.Text
    num1 = 1
    num2 = 1
    temp = 0
    Picture1.Print num1; num2;
    For i = 1 To n
    temp = num1 + num2
    i = i + 1
    Picture1.Print temp;
    num1 = num2
    num2 = temp
    Next i

    End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    What do you mean it doesn't work? DO you get an error? Or do you get the wrong resultS?
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    3
    I get the wrong results. I have tried different ways but can't figure it out. Maybe someone else can.

  4. #4
    Frenzied Member seoptimizer2001's Avatar
    Join Date
    Apr 2001
    Location
    Toledo, Ohio USA GMT -5
    Posts
    1,075
    First of all you don't need the i = i + 1 as the for loop will increment this for you. Try this....
    VB Code:
    1. Dim num1 As Integer
    2. Dim num2 As Integer
    3. Dim temp As Integer
    4. Dim n As Integer
    5. Dim i As Single
    6.  
    7. Private Sub txtnumber_Change()
    8. n = txtNumber.Text
    9. num1 = 1
    10. num2 = 1
    11. temp = 2
    12. Picture1.Print num1 & ", " & num2
    13. For i = 1 To n - 2
    14.     temp = num1 + num2
    15.  
    16.     num1 = num2
    17.     num2 = temp
    18.     Picture1.Print ", " & num2
    19. Next i
    20. End Sub
    seoptimizer2001
    VB 6.0, VC++, VI, ASP, JavaScript, HTML,
    Perl, XML, SQL Server 2000

    If God had intended us to drink beer, He would have given us stomachs.


    Please use the [code] and [vbcode] tags in your posts!
    If you don't know how to use them please go HERE!


  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    3

    Thumbs up

    Thanks that works for almost all. If the user inputs a 1 or 0 then 1, 1 appears instead of 1 for 1 and nothing for 0.

  6. #6
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    Couldn't you store the values in an array like this:
    VB Code:
    1. Private Sub Text1_Change()
    2.  
    3. Dim Fibo() As Integer
    4.  
    5. n = Val(Text1.Text)
    6.  
    7. ReDim Preserve Fibo(n)
    8.  
    9. Fibo(0) = 1
    10. Fibo(1) = 1
    11.  
    12. For i = 2 To n
    13.     Fibo(i) = Fibo(i - 2) + Fibo(i - 1)
    14. Next
    15.  
    16. For i = 0 To n - 1
    17.     Picture1.Print Fibo(i)
    18. Next
    19.  
    20. End Sub

    The biggest problem with putting the code under the Text1_Change event is that if the number 20 is entered, it will print the first 2 numbers in the sequence then the first 20 right after. I would code the event on the click of a button personally.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width