how to determine all combinations of array elements

This is a discussion on how to determine all combinations of array elements within the C++ Programming forums, part of the General Programming Boards category; I'm writing a genetics program working w/7 pairs of genes. Code: int parent[14] // comprised of 7 pairs of genes. ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    how to determine all combinations of array elements

    I'm writing a genetics program working w/7 pairs of genes.

    Code:
    int parent[14] // comprised of 7 pairs of genes. pairs are 0-1,2-3,4-5,6-7,8-9,10-11,12-13
    int parentdata[128][7] // all combinations of parent's genotype
    I need to parse all parent combinations into parentdata.
    Is there a way I can do this w/o writing 128 lines of code?

    ty
    Last edited by DaleZ; 10-25-2010 at 11:53 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Have you done a google search for "permutations and combinations"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    I've looked at permutations. I'm not sure if that would do what I want. Total valid combinations is 128.

    This is how I'm doing it now to get the desired results.

    Code:
    int parent[14] 
    int parentdata[128][7]
    
    /* populate siredata array /*
    /* En                  |   A                 |    B                 |   C                   | D                   |   E                    | V /*
    siredata[0][0]=sire[0],siredata[0][1]=sire[2],siredata[0][2]=sire[4],siredata[0][3]=sire[6],siredata[0][4]=sire[8],siredata[0][5]=sire[10],siredata[0][6]=sire[12];
    siredata[1][0]=sire[0],siredata[1][1]=sire[2],siredata[1][2]=sire[4],siredata[1][3]=sire[6],siredata[1][4]=sire[8],siredata[1][5]=sire[10],siredata[1][6]=sire[13];
    siredata[2][0]=sire[0],siredata[2][1]=sire[2],siredata[2][2]=sire[4],siredata[2][3]=sire[6],siredata[2][4]=sire[8],siredata[2][5]=sire[11],siredata[2][6]=sire[12];
    siredata[3][0]=sire[0],siredata[3][1]=sire[2],siredata[3][2]=sire[4],siredata[3][3]=sire[6],siredata[3][4]=sire[8],siredata[3][5]=sire[11],siredata[3][6]=sire[13];
    siredata[4][0]=sire[0],siredata[4][1]=sire[2],siredata[4][2]=sire[4],siredata[4][3]=sire[6],siredata[4][4]=sire[9],siredata[4][5]=sire[10],siredata[4][6]=sire[12]; //d2e1v1
    siredata[5][0]=sire[0],siredata[5][1]=sire[2],siredata[5][2]=sire[4],siredata[5][3]=sire[6],siredata[5][4]=sire[9],siredata[5][5]=sire[10],siredata[5][6]=sire[13];
    siredata[6][0]=sire[0],siredata[6][1]=sire[2],siredata[6][2]=sire[4],siredata[6][3]=sire[6],siredata[6][4]=sire[9],siredata[6][5]=sire[11],siredata[6][6]=sire[12]; //d2e2v1
    siredata[7][0]=sire[0],siredata[7][1]=sire[2],siredata[7][2]=sire[4],siredata[7][3]=sire[6],siredata[7][4]=sire[9],siredata[7][5]=sire[11],siredata[7][6]=sire[13];
    siredata[8][0]=sire[0],siredata[8][1]=sire[2],siredata[8][2]=sire[4],siredata[8][3]=sire[7],siredata[8][4]=sire[8],siredata[8][5]=sire[10],siredata[8][6]=sire[12]; //c2d1e1v1
    siredata[9][0]=sire[0],siredata[9][1]=sire[2],siredata[9][2]=sire[4],siredata[9][3]=sire[7],siredata[9][4]=sire[8],siredata[9][5]=sire[10],siredata[9][6]=sire[13];
    siredata[10][0]=sire[0],siredata[10][1]=sire[2],siredata[10][2]=sire[4],siredata[10][3]=sire[7],siredata[10][4]=sire[8],siredata[10][5]=sire[11],siredata[10][6]=sire[12]; //c2d1e2v1
    siredata[11][0]=sire[0],siredata[11][1]=sire[2],siredata[11][2]=sire[4],siredata[11][3]=sire[7],siredata[11][4]=sire[8],siredata[11][5]=sire[11],siredata[11][6]=sire[13];
    siredata[12][0]=sire[0],siredata[12][1]=sire[2],siredata[12][2]=sire[4],siredata[12][3]=sire[7],siredata[12][4]=sire[9],siredata[12][5]=sire[10],siredata[12][6]=sire[12]; //c2d2e1v1
    siredata[13][0]=sire[0],siredata[13][1]=sire[2],siredata[13][2]=sire[4],siredata[13][3]=sire[7],siredata[13][4]=sire[9],siredata[13][5]=sire[10],siredata[13][6]=sire[13];
    siredata{14][0]=sire[0],siredata[14][1]=sire[2],siredata[14][2]=sire[4],siredata[14][3]=sire[7],siredata[14][4]=sire[9],siredata[14][5]=sire[11],siredata[14][6]=sire[12]; //c2d2e2v1
    siredata[15][0]=sire[0],siredata[15][1]=sire[2],siredata[15][2]=sire[4],siredata[15][3]=sire[7],siredata[15][4]=sire[9],siredata[15][5]=sire[11],siredata[15][6]=sire[13];
    siredata[16][0]=sire[0],siredata[16][1]=sire[2],siredata[16][2]=sire[5],siredata[16][3]=sire[6],siredata[16][4]=sire[8],siredata[16][5]=sire[10],siredata[16][6]=sire[12]; //b2c1d1e1v1
    siredata[17][0]=sire[0],siredata[17][1]=sire[2],siredata[17][2]=sire[5],siredata[17][3]=sire[6],siredata[17][4]=sire[8],siredata[17][5]=sire[10],siredata[17][6]=sire[12];
    Is there a way I can make this simplier to code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I skip over zero elements in an array?
    By Roon Hapoon in forum C Programming
    Replies: 7
    Last Post: 10-18-2010, 04:58 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Determine length of an array
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-21-2007, 08:32 PM
  4. Replies: 42
    Last Post: 12-19-2004, 07:59 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21