c/c++ programming combination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessie
    New Member
    • Feb 2007
    • 1

    #1

    c/c++ programming combination

    hi everyone,
    I was trying to output all the r-combinations of n such that if the user inputs n{1,3,6,2,9} and the r is 3 the output would be like this

    1 3 6
    1 3 2
    1 3 9
    3 6 2
    3 6 9
    6 2 9

    i tried to do it with recursion but all i get was an infinite output....
    then i tried also with functions but the output is this:
    1 1
    1 3
    1 6
    1 9
    1 2
    1
    3 3
    3 1
    3 6
    3 9
    3 2
    3
    . . . .
    2 2
    2 1
    2 3
    2 6
    2 9
    2

    anything would be a great help... i'm still a beginner at c/c++ programming that's why i'm having difficulties with these kinds of programs....
  • fitsumtk
    New Member
    • Mar 2007
    • 3

    #2
    Originally posted by jessie
    hi everyone,
    I was trying to output all the r-combinations of n such that if the user inputs n{1,3,6,2,9} and the r is 3 the output would be like this

    1 3 6
    1 3 2
    1 3 9
    3 6 2
    3 6 9
    6 2 9

    i tried to do it with recursion but all i get was an infinite output....
    then i tried also with functions but the output is this:
    1 1
    1 3
    1 6
    1 9
    1 2
    1
    3 3
    3 1
    3 6
    3 9
    3 2
    3
    . . . .
    2 2
    2 1
    2 3
    2 6
    2 9
    2

    anything would be a great help... i'm still a beginner at c/c++ programming that's why i'm having difficulties with these kinds of programs....

    Here's a VB code it is of any help:

    Code:
    Public Function CombN(ByRef Vctr() As Integer, ByVal N As Integer) As Integer()
     ' returns all combinations of 'N' elements of the elements in vector 'Vctr'
       
       Dim i As Integer, j As Integer, k As Integer, cc As Integer, M As Integer, nv As Integer
               
       nv = UBound(Vctr)         ' number of combinations
       
       ReDim Co(1 To nv, 1 To N)
       
       For i = 1 To N
          cc = 1
          For j = 1 To (nv ^ (i - 1))
             For k = 1 To nv
                For M = 1 To (nv ^ (N - i))
                    Co(cc, i) = Vctr(k)
                    cc = cc + 1
                Next
             Next
          Next
       Next
       
    End Function
    Cheers,
    fitsumtk

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      What you will have to do is have the set in an array - this is the first step.

      Now, if you were computing an r combination of set r, how would you do it without a computer? One way is the start with the first element and choose the next r - 1 numbers - this is your combination. Then you move the last number one to the right, and repeat, until you reach the end. Then you move the second to last number to the right and repeat the first process.

      Are you starting to see a pattern here? Here's the first few 3 combinations of a sample set:

      Set n = {1, 2, 3, 4, 5}
      r = 3

      Comb 1: {1, 2, 3}
      Comb 2: {1, 2, 4}
      Comb 3: {1, 2, 5}
      Comb 4: {1, 3, 4}
      Comb 5: {1, 3, 5}
      Comb 6: {1, 4, 5}
      Comb 7: {2, 3, 4}

      etc.

      Comment

      Working...