array loop problem (VB)

Get help on programming - C++, Java, Delphi, etc.
Post Reply
User avatar
djwez
The next Sheldon Cooper
Posts: 5666
Joined: 20 Aug 2008, 17:51
Processor: AMD PII x3 720 (4th core unlocke
Motherboard: Asus M4A785TD-M EVO
Graphics card: Sapphire 7790 Dual x OC
Memory: 8GB 1600 Vengeance kit
Location: Behind you....

array loop problem (VB)

Post by djwez »

hey guys.
im having some trouble taking values from a 3 dimensional array and putting them into one string. for some reason my loop is only reading the first value (0,0,0) and not moving on to the second (0,0,1)
this is not all the code just the main looping bit

Code: Select all

        checkarray(0, 0, 0) = 2
        checkarray(0, 0, 2) = 3


        Dim str As String
        Dim x, y, z As Integer
        x = 0
        y = 0
        z = 0
        str = ""
        Do

            str = str & " "
            str = str & checkarray(x, y, z)
            Lbl_debug2.Text = z
            If z < 2 Then z += 1
            If z = 2 Then
                y += 1
                z = 0
            End If
            If y < 2 Then y += 1
            If y = 2 Then
                x += 1
                z = 0
                y = 0
            End If

            If x = 2 Then
                Exit Do
            End If

        Loop
        Lbl_debug.Text = str
thanks guys
SykomantiS
Registered User
Posts: 14085
Joined: 06 Oct 2004, 02:00
Location: Location, Location...
Contact:

Re: array loop problem (VB)

Post by SykomantiS »

My head hurts :|
Also I don't have VS at home, but it's pretty clear you aren't looping though all the values.
You are incrementing Y on two separate lines, which causes it to skip over. (There are probably other errors as well, but right now my brain refuses to show them to me)

At the end of each loop, your values for X, Y, Z are
0 0 0
0 1 1
1 0 0
1 1 1
2 0 0

At which time you exit the loop. Clearly, not all possible combinations are being checked (there should be 27 combinations to check- 3 values for each variable- 33)

I would use nested for loops, (for x as integer = 0 to 2) etc.


EDIT: Stupid typo...
Last edited by SykomantiS on 22 May 2011, 11:08, edited 1 time in total.
User avatar
djwez
The next Sheldon Cooper
Posts: 5666
Joined: 20 Aug 2008, 17:51
Processor: AMD PII x3 720 (4th core unlocke
Motherboard: Asus M4A785TD-M EVO
Graphics card: Sapphire 7790 Dual x OC
Memory: 8GB 1600 Vengeance kit
Location: Behind you....

Re: array loop problem (VB)

Post by djwez »

i had a feeling that was the case :)

thanks ill try it out
Post Reply