Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create an array with all combinations of n numbers? 1

Status
Not open for further replies.

domxp

Technical User
Feb 18, 2002
25
0
0
PL
Hello everybody! I'm in a deep need. I would like to create an array(31,5) that would contain all possible combinations of five numbers (1, 2, 3, 4, 5) in its rows. The number of such combinations is N = 2^5 - 1 = 31. A couple of first combinations looks as follws:
1, , , ,
2, , , ,
3, , , ,
4, , , ,
5, , , ,
1,2, , ,
1,3, , ,
.....
2,3,5, ,
2,4,5, ,
.....
1,2,3,4,5
If a combinations consist of for example one number then in the rest of filds in a corresponding row of a matrix may be empty or zero.
Please help me! This is for sure simple but not for me today... :(
 
This will do the trick. The iterations are not in the order you illustrated, but all possible combinations are created.
Paul


Private Sub Command1_Click()
Dim A, B, C, D, E As Integer
Dim strAry(1 To 31) As String
Dim intPtr As Integer
For A = 1 To 5
intPtr = intPtr + 1
strAry(intPtr) = Str(A)
For B = A + 1 To 5
intPtr = intPtr + 1
strAry(intPtr) = Str(A) & Str(B)
For C = B + 1 To 5
intPtr = intPtr + 1
strAry(intPtr) = Str(A) & Str(B) & Str(C)
For D = C + 1 To 5
intPtr = intPtr + 1
strAry(intPtr) = Str(A) & Str(B) & Str(C) & Str(D)
For E = D + 1 To 5
intPtr = intPtr + 1
strAry(intPtr) = Str(A) & Str(B) & Str(C) & str(D) & str(E)
Next
Next
Next
Next
Next

End Sub
 
You have done exactly what I needed! I needed strings and here I have them (although i haven't explained it that well). I'm impressed with simplicity of the code.
Thanks a lot !!!
I registered to get a notification and that was really worth it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top