Solved

Generating Combinations in VB5

Posted on 2000-05-05
4
135 Views
Last Modified: 2011-09-20
Does anyone have a routine for generating each combination of a given group of numbers.

Example:
6 numbers has 6 groups of 5 numbers

1 2 3 4 5
1 2 3 4 6
1 3 4 5 6
1 2 4 5 6
1 2 3 5 6
2 3 4 5 6



0
Comment
Question by:backflash
  • 2
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2783169
i take it that the algorithm states the numbers in the group must be in ascending order?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2783176
add two textboxes to hold the values (e.g. 5 and 6) and another textbox that is set to multiline to display the results.  add a command button that will execute the code:


Option Explicit

Private Sub MchoseN(ByVal N As Integer, ByVal first_allowed As Integer, ByVal last_allowed As Integer, ByVal solutions As Collection)
    Dim i As Integer
    Dim txt As String
    Dim partial_solutions As Collection

    If N < 1 Then

    ElseIf N > last_allowed - first_allowed + 1 Then

    ElseIf N = last_allowed - first_allowed + 1 Then

        txt = Format$(first_allowed)
        For i = first_allowed + 1 To last_allowed
            txt = txt & " " & Format$(i)
        Next i
        solutions.Add txt
    Else
        Set partial_solutions = New Collection
        If N = 1 Then
            partial_solutions.Add ""
        Else
            MchoseN N - 1, first_allowed + 1, last_allowed, partial_solutions
        End If

        For i = 1 To partial_solutions.Count
            solutions.Add Format$(first_allowed) & _
                " " & partial_solutions(i)
        Next i

        Set partial_solutions = New Collection
        MchoseN N, first_allowed + 1, last_allowed, partial_solutions

        For i = 1 To partial_solutions.Count
            solutions.Add partial_solutions(i)
        Next i
    End If
End Sub
Private Sub Command1_Click()
    Dim solutions As Collection
    Dim i As Integer
    Dim txt As String

    Set solutions = New Collection
    MchoseN CInt(txtN.Text), 1, CInt(txtM.Text), solutions

    For i = 1 To solutions.Count
        txt = txt & solutions(i) & vbCrLf
    Next i

    txtResult.Text = txt
    lblSolutions.Caption = Format$(solutions.Count)
End Sub

Private Sub Form_Resize()
    Dim hgt As Single

    hgt = ScaleHeight - txtResult.Top
    If hgt < 120 Then hgt = 120
    txtResult.Move 0, txtResult.Top, ScaleWidth, hgt
End Sub

0
 
LVL 9

Accepted Solution

by:
GivenRandy earned 100 total points
ID: 2783204
I think you'll find this faster and easier (List1 is where output is sent, Command1 starts by calling Combinate):

Option Explicit

Private Sub Command1_Click()
    Combinate 6, 5
End Sub

Private Sub Combinate(N As Long, K As Long)
    Dim Counter As Long
   
    ' Error trap for N < K, N > 31
    List1.Clear
    For Counter = 0 To (2 ^ N - 1)
        If (BitCount(Counter) = K) Then
            List1.AddItem Result(Counter)
        End If
    Next
End Sub

Private Function BitCount(Number As Long) As Integer
  Dim Temp As Long
 
  BitCount = 0
  Temp = Number
  Do While (Temp)
    Temp = Temp And (Temp - 1)
    BitCount = BitCount + 1
  Loop
End Function

Private Function Result(ByVal Number As Long) As String
    Dim Temp As Long
   
    Result = ""
    Temp = 1
    Do While Number
        If ((Number Mod 2) = 1) Then
            Result = Result & Temp & " "
        End If
        Number = Number \ 2
        Temp = Temp + 1
    Loop
End Function
0
 

Expert Comment

by:Unicorn061399
ID: 2784827
Why do u need a number generating algoritm? By simply assigning the values to an array u generate less code.
Or is this an arithmic question on binary logic or something?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now