The Glade 4.0
https://gladerebooted.net/

Assistance appreciated (Visual basic)
https://gladerebooted.net/viewtopic.php?f=5&t=5814
Page 1 of 1

Author:  darksiege [ Fri Mar 25, 2011 10:06 pm ]
Post subject:  Assistance appreciated (Visual basic)

I am trying to write a silly little program that will let me run and display a do loop that will allow me to have a little popup display that shows the Fibonacci sequence generated.

I am looking to see where I would need to go next on this as I cannot figure out how to get the results of the loop into the Message String for display.

If anyone can take a look and point me to what I am missing... it would be appreciated.

ALSO: by using the Do Until cnt >= 610, am I generating 610 numbers or 15? I know F15 = 610. But I am not sure if I am coding that wrong.
Code:
Private Sub DoLoopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoLoopButton.Click
        Dim X As Integer
        Dim Y As Integer
        Dim cnt As Integer 'Our counter.
        Dim MessageString As String
       
        cnt = 1

        Do Until cnt >= 610
           
            X = Y + X
            Y = X - Y
            cnt = cnt + 1
        Loop
       
        MessageString = "Do Loop Results:" & Environment.NewLine & Environment.NewLine
        MessageBox.Show(MessageString, "Do Loop Testing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


    End Sub

Author:  darksiege [ Fri Mar 25, 2011 10:17 pm ]
Post subject:  Re: Assistance appreciated

unneeded at this time.

Author:  Shelgeyr [ Fri Mar 25, 2011 10:58 pm ]
Post subject: 

3 problems with the code you posted:
  • It will in fact cycle 610 times as cnt is only incremented by 1 on each iteration.
  • The values of X and Y will never stray from 0 as they haven't been initialized with any positive values
  • Without code to update the MessageString within the loop, your end result won't contain any of the computed numerical values

Try something a bit more like this:
Code:
    Dim X(0 to 1) as Double
    Dim cnt as Double
    Dim MessageString as String

    MessageString = "Do Loop Results:" & VbCrLf & VbCrLf & 1 & VbCrLf
    X(0) = 0
    X(1) = 1
    cnt = 1
    Do Until X(cnt) >= 610
        cnt = cnt + 1
        ReDim Preserve X(0 to cnt)
        X(cnt) = X(cnt-1) + X(cnt-2)
        MessageString = MessageString & X(cnt) & VbCrLf
    Loop

Don't know anything about the "Environment.NewLine" bit, as I've never encountered it myself, but I've used "VbCrLf" (visual basic carriage return line feed) plenty, and am certain it does what you're looking for. In any case, the above code will initialize X as a small array and then continually expand it as needed until the cutoff is reached; all while adding the newly computed values to the MessageString for later output.

Author:  darksiege [ Fri Mar 25, 2011 11:15 pm ]
Post subject: 

Shel,

that did exactly what I was looking for.

Sadly, it did not do the same as I tried to modify it for a "For/Next Loop" but I am diggin into that.

Thank you very much.

Author:  Shelgeyr [ Fri Mar 25, 2011 11:25 pm ]
Post subject: 

Yeah, the loop I wrote is designed to be open-ended in that all you need do if you change your mind about how many iterations of the sequence you wish to display is change "610" to ... some other number. Writing open-ended code has begun to transition from second-nature to first-nature for me in the past couple of years.
If, on the other hand, you're certain of the size of your closed-end loop at the outset, you can change it up like this:
Code:
        Dim X(0 to 15) as Double
        Dim cnt as Double
        Dim MessageString as String

        MessageString = "Do Loop Results:" & VbCrLf & VbCrLf & 1 & VbCrLf
        X(0) = 0
        X(1) = 1
        For cnt = 2 to 15
            X(cnt) = X(cnt-1) + X(cnt-2)
            MessageString = MessageString & X(cnt) & VbCrLf
        Next cnt

Author:  Corolinth [ Sat Mar 26, 2011 11:08 am ]
Post subject: 

I would initialize both variables to be some particular value (1 in the case of a Fibonacci sequence) before going into the loop.

Another way you can do it would be to create an one-dimensional array. Initialize the first two values and start the counter at 3. Add a new array(counter) = array(counter - 1) + array(counter - 2) value to it for every iteration of the loop. That lets you move your output command outside of the loop. It's also easier to debug, because your loop is just the formula for the Fibonacci sequence.

Author:  Lex Luthor [ Sat Mar 26, 2011 11:34 am ]
Post subject: 

Shel has the right idea. Also I advise to never use "do until". It's very non-standard and makes code confusing. Use "while" or "for".

Author:  darksiege [ Sat Mar 26, 2011 12:34 pm ]
Post subject: 

we had to do a For Loop separately. Arrays are chapter 8, which I am currently reading/working on.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/