The Glade 4.0

"Turn the lights down, the party just got wilder."
It is currently Sun Nov 24, 2024 2:37 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Mar 25, 2011 10:06 pm 
Offline
Grrr... Eat your oatmeal!!
User avatar

Joined: Wed Sep 02, 2009 11:07 pm
Posts: 5073
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

_________________
Darksiege
Traveller, Calé, Whisperer
Lead me not into temptation; for I know a shortcut


Last edited by darksiege on Fri Mar 25, 2011 10:18 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 25, 2011 10:17 pm 
Offline
Grrr... Eat your oatmeal!!
User avatar

Joined: Wed Sep 02, 2009 11:07 pm
Posts: 5073
unneeded at this time.

_________________
Darksiege
Traveller, Calé, Whisperer
Lead me not into temptation; for I know a shortcut


Last edited by darksiege on Fri Mar 25, 2011 11:16 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Mar 25, 2011 10:58 pm 
Offline
Sensitive Ponytail Guy
User avatar

Joined: Fri Sep 04, 2009 10:18 pm
Posts: 2765
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.

_________________
Go back to zero, take a pill, and get well ~ Lemmy Kilmister


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Mar 25, 2011 11:15 pm 
Offline
Grrr... Eat your oatmeal!!
User avatar

Joined: Wed Sep 02, 2009 11:07 pm
Posts: 5073
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.

_________________
Darksiege
Traveller, Calé, Whisperer
Lead me not into temptation; for I know a shortcut


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Mar 25, 2011 11:25 pm 
Offline
Sensitive Ponytail Guy
User avatar

Joined: Fri Sep 04, 2009 10:18 pm
Posts: 2765
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

_________________
Go back to zero, take a pill, and get well ~ Lemmy Kilmister


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Mar 26, 2011 11:08 am 
Offline
Manchurian Mod
User avatar

Joined: Fri Sep 04, 2009 9:40 am
Posts: 5866
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.

_________________
Buckle your pants or they might fall down.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Mar 26, 2011 11:34 am 
Offline

Joined: Thu Sep 03, 2009 10:03 am
Posts: 4922
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".


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Mar 26, 2011 12:34 pm 
Offline
Grrr... Eat your oatmeal!!
User avatar

Joined: Wed Sep 02, 2009 11:07 pm
Posts: 5073
we had to do a For Loop separately. Arrays are chapter 8, which I am currently reading/working on.

_________________
Darksiege
Traveller, Calé, Whisperer
Lead me not into temptation; for I know a shortcut


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 106 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group