Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGenerate unordered combinations of 5 numbers out of a bigger set
Keywordscombinations, combinatorics, sequences
CategoriesAlgorithms
 
Thanks to Phil McCarthy.

This program generates all unordered combinations of 5 numbers chosen from a set of 5 to 28 numbers.

The basic idea is to use a loop for each of the five values being produced. Each loop runs from the number after the one previously selected to the end of the list. This generates the values in sorted order.

 
Private Sub Command1_Click()
    ' no start without a range
    If Text1.Text <> "" Then
        ' establish the array of numbers
        ' In this example the numbers are ranges from 1 to
        ' the entered number
        Dim view(28) As String
        Dim x As Integer

        For x = 1 To Text1.Text
            view(x) = x
        Next x

        For x = 1 To Text1.Text
            List1.AddItem x
        Next x

        ' Dim things
        Dim one As Integer
        Dim two As Integer
        Dim three As Integer
        Dim four As Integer
        Dim five As Integer
        Dim counter As Long
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer
        Dim e As Integer
        Dim record$

        ' establish variables
        a = 1

        ' Begin the loop
        Do
            ' set the 5 loops in motion.
            For one = a To Text1.Text - 4
                b = one + 1
                For two = b To Text1.Text - 3
                    c = two + 1
                    For three = c To Text1.Text - 2
                        d = three + 1
                        For four = d To Text1.Text - 1
                            e = four + 1
                            For five = e To Text1.Text
                                record$ = one & " " & two & _
                                    " " & three & " " & _
                                    four & " " & five
                                counter = counter + 1
                                Label1.Caption = counter
                                result$(counter) = record$
                            Next five
                        Next four
                    Next three
                Next two
            Next one
        Loop Until record$ = Text1.Text - 4 & " " & _
            Text1.Text - 3 & " " & Text1.Text - 2 & " " & _
            Text1.Text - 1 & " " & Text1.Text

        ' show results
        Label1.Caption = "Total records = " & counter & " " _
            & vbCrLf & "Last record =" & record$
    End If
End Sub
 
Exercise: Write a program that can generate combinations of any length chosen from any number of items. In other words, for N numbers generate all possible combinations of K of them.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated