### Author Topic: Loop to obtain number sequences  (Read 1528 times)

0 Members and 1 Guest are viewing this topic.

#### Kheilmann

• Guest
##### Loop to obtain number sequences
« on: April 27, 2007, 09:50:22 AM »
I've tried For X = # to # I have not figured it out yet.  I'm not sure if I need to use
Do until or While?

I basically need a loop to return all sequences upto a certain #.
EX.
If the number of items selected = 2, then I need a loop to return 1&2 and 2&1.
If the # of items = 3, then I need to return 1&2&3, 1&3&2, 2&1&3, 2&3&1, 3&2&1, 3&1&2.
etc.
etc.
Basically these items have to be selected in a specific order (for many reasons), and this will make sure
that no matter how someone selects the items, the final result will always be calculated to a certain sequence.

I can give more detail if need be, but for now I just need help creating a loop to return all sequences possible.
The sequence will always be 1 to (# of items selected by user)

Kevin

#### David Hall

• Automatic Duh Generator
• King Gator
• Posts: 4043
##### Re: Loop to obtain number sequences
« Reply #1 on: April 27, 2007, 09:58:33 AM »
I think the number of possible combinations is the number of items picked , factorial.
As you pointed out, for the #2, there are 2 possible combinations.  for 3 #'s,
there are 6 combinations, (3X2X1) or 3 factorial.  So 4 would be 24 right?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

#### David Hall

• Automatic Duh Generator
• King Gator
• Posts: 4043
##### Re: Loop to obtain number sequences
« Reply #2 on: April 27, 2007, 10:13:57 AM »
Here is a function to obtain the factorial number
Code: [Select]
`Function FACTORIAL(x As Integer) As IntegerDim i As IntegerFACTORIAL = 1For i = 1 To xFACTORIAL = FACTORIAL * iNext iEnd Function`
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

#### DaveW

• Guest
##### Re: Loop to obtain number sequences
« Reply #3 on: April 27, 2007, 10:38:28 AM »
You may want to use a couple of for loops after adding your data to an array.

The problem with doing this is that it is a bit involved to remove data out of an array. The way I do it is I create a Type array.

Code: [Select]
`Public Type typPLinesArray  MyHandle As String  MyLineTypeScale As String  Used As Integer  MyStartPointX As Double  MyStartPointY As Double  MyStartPointZ As Double  MyEndPointX As Double  MyEndPointY As Double  MyEndPointZ As DoubleEnd Type`
In your code you need to also create the array to use, because we have only defined a Type above:

Code: [Select]
`Dim MyPLinesArray2() As typPLinesArray`
Now populate MyPLinesArray2() with your data, setting each filed as you go.
Tweak as necessary... You will ahve a dropdown for each area you define in the Type

Code: [Select]
`                    MyPLinesArray2(PolyLinesFound2).MyHandle = MyLWPoly.Handle                MyPLinesArray2(PolyLinesFound2).MyLineTypeScale = MyLWPoly.LinetypeScale                MyPLinesArray2(PolyLinesFound2).Used = 0                MyPLinesArray2(PolyLinesFound2).MyStartPointX = PolyStartPoint(0)                MyPLinesArray2(PolyLinesFound2).MyEndPointX = PolyEndPoint(0)                MyPLinesArray2(PolyLinesFound2).MyStartPointY = PolyStartPoint(1)                MyPLinesArray2(PolyLinesFound2).MyEndPointY = PolyEndPoint(1)                MyPLinesArray2(PolyLinesFound2).MyStartPointZ = PolyStartPoint(2)                MyPLinesArray2(PolyLinesFound2).MyEndPointZ = PolyEndPoint(2)`

Notice the line "Used As Integer"
Once I have used that value, I change its value to 1, so I know I have used it. of course, you have to write a line of code for that.
Code: [Select]
`If MyPLinesArray2(X).Used = 0 Then`
Once your array(based/dimmed on your own personal Type is filled, then you can process it.

The basic outline is:

Code: [Select]
`For X = 0 To UBound(MyPLinesArray2)                    Num1 = MyPLinesArray2(X).MyStartPointX                    'Whatever you are looking for...                          For Y = 0 To UBound(MyPLinesArray2)                                                          If MyPLinesArray2(Y).Used = 0 Then                                   Num2 = MyPLinesArray2(Y).MyStartPointX                                   MyString =  MyString & "," & Trim(Str(Num2))                                   MyPLinesArray2(Y).Used = 1                              End if                         NextNext`

You have quite a bit of code to add, conditional checks for first time through to build your string and will probably need 2 different .used columns, .used1 and .used2, and code to reset 1 of them back to 0 when you want to start over.