CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Posts
    1

    Help With Finding All Combinations for Array length R

    Hi,

    I am currently working on a program that uses Hamming Codes to encode various incoming arrays of 1's and 0's . However, in order to do this i need to create a Generator matrix which requires that I know all possible combinations for an array of length N (by all combinations i just mean binary 1 and 0)... this is so i can find a basis for set and thus create a Matrix.

    Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?

    Just for clarification heres what I mean:
    say array length is 3:

    [1 1 0
    1 0 1
    0 1 1
    1 1 1
    1 0 0
    0 1 0
    0 0 1]


    Thanks!

  2. #2
    Join Date
    Dec 2006
    Posts
    166

    Re: Help With Finding All Combinations for Array length R

    what about "0 0 0"?

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Help With Finding All Combinations for Array length R

    Quote Originally Posted by shahs8
    Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?
    For an array of length R there are 2^R combinations. The bit combinations are the binary numbers between 0 and (2^R)-1, like

    0=000
    1=001
    2=010
    3=011
    4=100
    5=101
    6=110
    7=111

    So in principle if R=3 the numbers 0 to 7 represent all bit combinations. It means that these numbers are a dense representation of your arrays.

  4. #4
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Help With Finding All Combinations for Array length R

    Quote Originally Posted by shahs8
    Hi,

    Was just wondering... can someone explain to me what the logic is/ how to go about using loops to fill in a 2-D array with all possible combinations of 1's and 0's for any n?
    The combinaisons of 1 and 0 of length n is simply the binary representations of all numbers from 0 to (2^n)-1. It should be easy to generate the binary representation of those numbers.

    JeffB
    Last edited by JeffB; April 26th, 2008 at 11:26 AM.
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help With Finding All Combinations for Array length R

    If you don't want to mess around with bit-shifting or masking, you could have a look at Integer.toBinaryString(..) or Long.toBinaryString().

    Any programming problem can be solved by adding a level of indirection...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured