Results 1 to 15 of 15

Thread: Looping Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    3

    Looping Question

    Hi guys...

    Our class just got into looping and list boxes with VB.Net, and I got a question that I am stuck on and appreciate any help!

    Write a program that will display all the numbers between 1 and 100 that are part of the Fibonacci sequence. The Fabonacci sequence begins 1,1,2,3,5,8,..., where each new number in the sequence is found by adding the previous two numbers in the sequence. Display your results in a list box or a text box with a multilane property set to True.


    Regards

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    I have no idea on VB.Net so I won't post code...

    To start the sequence you need a seed, which is two ones (1,1). Store those in an array at index 0 and 1. Start looping (counter starts at 1, since that's currently the last array index used) . At each loop add array index = counter and array index = counter - 1, if sum is less than 100 then append sum to array index = counter + 1. If sum is equal or greater than 100 then stop the loop.

    Transfer the array values to a listbox or multiline textbox.

    Up to you if you'll use dynamic arrays.

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    VB Code:
    1. dim LastNumber as integer
    2. dim PreviousNumber as integer
    3. dim CurrentNumber as integer
    4.  
    5. listbox1.clear
    6. listbox1.additem 1
    7.  
    8. lastnumber=1
    9. previousnumber=0
    10. currentnumber=0
    11.  
    12. while currentnumber<=100
    13.   currentnumber=previousnumber+lastnumber
    14.   listbox1.additem currentnumber
    15.   previousnumber=lastnumber
    16.   lastnumber=currentnumber
    17. wend

    Not in front of VB, so I can't test this, but it looks ok to me.
    How many marks do you get for this question - I think you owe me some!
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    The way you had it included 144

    VB Code:
    1. Dim LastNumber As Integer
    2. Dim PreviousNumber As Integer
    3. Dim CurrentNumber As Integer
    4.  
    5. listbox1.Clear
    6. listbox1.AddItem 1
    7.  
    8. LastNumber = 1
    9. PreviousNumber = 0
    10. CurrentNumber = 0
    11.  
    12. While CurrentNumber <= 100
    13.   CurrentNumber = PreviousNumber + LastNumber
    14.   If CurrentNumber > 100 Then GoTo SkipLoop
    15.   listbox1.AddItem CurrentNumber
    16.   PreviousNumber = LastNumber
    17.   LastNumber = CurrentNumber
    18. Wend
    19. SkipLoop:

    I do hope that this'll work in .NET too (ie, it was testing in VB6)
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    So the seed is 0,1?

    Why not put the test at the end of the loop since your bound to go through it at least once.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    3

    Thumbs up

    Thanx guyz..I really appreciate it! I will give it a shot and see how it turns out.


    Buzby, This is actually like 3 points on a worksheet but it was killing me so I had to find the answer!

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Make sure you dont use GoTos. They are evil

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by jemidiah
    The way you had it included 144

    VB Code:
    1. Dim LastNumber As Integer
    2. Dim PreviousNumber As Integer
    3. Dim CurrentNumber As Integer
    4.  
    5. listbox1.Clear
    6. listbox1.AddItem 1
    7.  
    8. LastNumber = 1
    9. PreviousNumber = 0
    10. CurrentNumber = 0
    11.  
    12. While CurrentNumber <= 100
    13.   CurrentNumber = PreviousNumber + LastNumber
    14.   If CurrentNumber > 100 Then GoTo SkipLoop
    15.   listbox1.AddItem CurrentNumber
    16.   PreviousNumber = LastNumber
    17.   LastNumber = CurrentNumber
    18. Wend
    19. SkipLoop:

    I do hope that this'll work in .NET too (ie, it was testing in VB6)
    Wends are gone in .net if i am not wrong
    \m/\m/

  9. #9
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by PT Exorcist
    Wends are gone in .net if i am not wrong
    I can't find any reference to that keyword in my book, so yeah they arn't their anymore.

    Though from the code that was posted using it, it seems very useless......

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    just change the wend by next

    i think that'll make the trick
    \m/\m/

  11. #11
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by PT Exorcist
    just change the wend by next

    i think that'll make the trick
    Why??

    What does Wend even do? Does it go back to the top of the loop? Or stop it?

  12. #12
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    does the same as next in a for loop
    \m/\m/

  13. #13
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by PT Exorcist
    does the same as next in a for loop
    But it's a while loop, next and wend are both useless for a while loop....

    meh

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    omg

    its just a FLAG to the compiler know where the while beggins and ends
    \m/\m/

  15. #15
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    While..Wend are my loop of choice (probably just being stubborn to be honest).. the replacement is Do While.. Loop

    VB Code:
    1. dim LastNumber as integer
    2. dim PreviousNumber as integer
    3. dim CurrentNumber as integer
    4.  
    5. listbox1.clear
    6. listbox1.additem 1
    7.  
    8. lastnumber=1
    9. previousnumber=0
    10. currentnumber=0
    11.  
    12. do while currentnumber<=100
    13.   currentnumber=previousnumber+lastnumber
    14.   listbox1.additem currentnumber
    15.   previousnumber=lastnumber
    16.   lastnumber=currentnumber
    17. loop

    or you could do

    VB Code:
    1. do
    2.   currentnumber=previousnumber+lastnumber
    3.   listbox1.additem currentnumber
    4.   previousnumber=lastnumber
    5.   lastnumber=currentnumber
    6. loop until currentnumber>100
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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