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!

Problem with combinations of array per quartets.

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
0
0
GR
I need a little help with my code. I have an array of digits that doesn't have specific bound. When that bound of array is greater than 4, i have to take all the possible combinations per quartet.
I will give an example to understand with characters, i believe it helps more.
Let's give an example supposingly our Array has the following elements a b c d e f
I need to take the following combinations, a b c d, a b c e, a b c f, a b d e, a b d f, a b e f, a c d e, a c d f, a c e f, a d e f, b c d e, b c d f, b c e f, b d e f, c d e f.
My code is the following and my array keeps indexes, for facility TmpArr has the following elements 0 1 2 3 4 5 but it doesn't serve all the combinations I need, I don't know what I miss in the logic. May anybody help me understand better or guide me so that to solve it??? Any help will be much appreciated. Thank you so much in advanced.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim arr(6) As Integer

arr(0) = 0 : arr(1) = 1 : arr(2) = 2 : arr(3) = 3 : arr(4) = 4 : arr(5) = 10 : arr(6) = 5
Dim cnt As Byte
For i As Byte = 0 To arr.Length - 1
If arr(i) <> 10 Then
cnt += 1
End If
Next


Dim TmpArrLst As New List(Of Integer)
For i As Byte = 0 To arr.Length - 1
If arr(i) <> 10 Then
TmpArrLst.Add(arr(i))
End If
Next
TmpArrLst.Sort()

Dim quarteto As New ArrayList
Dim QuartetoClone As New ArrayList

Dim QuartetosLst As New ArrayList
Dim iQ As Byte = 0
Dim TetradesCnt As Byte = 0
For i As Byte = 0 To TmpArrLst.Count - 1
If iQ < 4 Then
quarteto.Add(TmpArrLst(i))
iQ += 1
End If
If iQ = 4 Then
iQ = 0
QuartetosLst.Add(quarteto)
TetradesCnt += 1
Exit For
End If
Next
Dim QuartetoLastElement As Integer = quarteto(quarteto.Count - 1)
Combinatios (quarteto, TmpArrLst, QuartetosLst, QuartetoLastElement)
End Sub
Public Sub Combinations(ByRef Quarteto As ArrayList, ByRef tmpArr As List(Of Integer), ByRef QuartetoLst As ArrayList, ByVal QuartetoLastElement As Integer, _
Optional ByVal indx As Byte = 0)
Dim i As Byte = 1
Dim Value As Integer = QuartetoLastElement
Dim NewQuarteto As New ArrayList
Dim TmpQuarteto As New ArrayList
NewQuarteto = Quarteto.Clone
Dim value2 As Integer
While QuartetoLastElement < tmpArr(tmpArr.Count - 1)
if indx <> 0 Then
value2 = indx

If QuartetoLastElement + 1 = Quarteto.Count - 1 Then
NewQuarteto(QuartetoLastElement) = value2
For iii As Integer = indx + 1 To tmpArr.Count - 1
NewQuarteto(QuartetoLastElement + 1) = iii

QuartetoLst.Add(NewQuarteto)
NewQuarteto = New ArrayList
TmpQuarteto = QuartetoLst(QuartetoLst.Count - 1)
NewQuarteto = TmpQuarteto.Clone
Next
ElseIf QuartetoLastElement + 1 <= Quarteto.Count - 1 Then

Dim Difference As Integer
Dim metritis As Integer = tmpArr.IndexOf(QuartetoLastElement)
value2 = tmpArr.IndexOf(indx)
Difference = ((Quarteto.Count - 1) - metritis)
If Difference > 1 Then
value2 += indx
NewQuarteto(Difference) = value2
If Difference + 1 = Quarteto.Count - 1 Then
QuartetoLst.Add(NewQuarteto)
NewQuarteto = New ArrayList
TmpQuarteto = QuartetoLst(QuartetoLst.Count - 1)
NewQuarteto = TmpQuarteto.Clone
End If
End If
End If
Value = TmpQuarteto(TmpQuarteto.Count - 1)
i += 1
Else
Value += 1
NewQuarteto(QuartetoLastElement) = Value
QuartetoLst.Add(NewQuarteto)
NewQuarteto = New ArrayList
TmpQuarteto = QuartetoLst(QuartetoLst.Count - 1)
NewQuarteto = TmpQuarteto.Clone
End If


If Value = tmpArr(tmpArr.Count - 1) Then
If i <= 4 Then i += 1
QuartetoLastElement = Quarteto(Quarteto.Count - i)
Quarteto = New ArrayList
Quarteto = QuartetoLst(QuartetoLst.Count - 1)
indx = QuartetoLastElement + 1
Combinations(Quarteto, tmpArr, QuartetoLst, QuartetoLastElement, indx)
End If
End While

End Sub
 
This is in response to WomanPro's issue (20th May) which I have only just seen. For some reason the system
did not allow me to reply directly.

She basically is trying to create an array of all combinations of a group (e.g. a, b, c, d) and seems to be using
an overly complex method. The solution is to make use of the fact that the number of possible combinations is
based on the factorial of the number of elements. For example if there are four elements then the number of
permutations is 4 * 3 * 2 * 1 = 24. If 6 elements then the number of permutations is 6 * 5 * 4 * 3 * 2 * 1 = 720
and so on. The coding then has to reflect this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top