Megalith
Well-known member
- Joined
- Aug 21, 2006
- Messages
- 66
- Programming Experience
- 10+
I am writing an application for doing probability. what the software essentially needs to do is to create an array with every conbination of x items picked from an array of y elements. one solution is to write a section like this
The problem is what if x was 10 or 100 using this method i would have to write a seperate block of increasing complexity for each value of x
does anyone have any suggestions for another way i can go about doing this?
VB.NET:
Private Sub Combination(ByVal x As Integer, ByVal Array() as Integer)
Dim a, b, c, e as Integer
If x = 2 Then
e = 1
For a = 1 To Array.GetLength - 1
for b = a + 1 To array.GetLength
Result(e,0) = Array(a)
Result(e,1) = Array(b)
e += 1
Next
Next
End If
If x = 3 Then
For a = 1 To Array.GetLength - 2
For b = a + 1 To Array.GetLength - 1
For c = b + 1 To Array.GetLength
Result(e,0) = Array(a)
Result(e,1) = Array(b)
Result(e,2) = Array(c)
Next
Next
Next
End If
.........
End Sub
Last edited by a moderator: