vendredi 27 octobre 2017

How to use recursive function to generate an array of m-length strings using 4 chars

Using c++ on visual studio I want a function that generates a string array, assuming that each string in this array has the length m, and the letters to be used in the string production are A, C, G, T. For example If m = 4, then the array should contain 4^4 items "64" just like {AAAA, AAAC, AAAG, AAAT, AACA, AAGA,AATA, ............., TTTA, TTTC,TTTG,TTTT} I manged to get such array using nested for loops but I can not change m with nested for loops:

string AllPatterns [10000];
string n [4] = {'A','C','G','T'};
string tempPattern ="AAAA";
int count =0;
for(unsigned int i=0 ; i<4; i++)
 {
  for(unsigned int j=0 ; j<4; j++)
   {
     for(unsigned int k=0 ;k<4 ; k++)
     {
      for(unsigned int l=0 ;l<4 ; l++)
      {
        tempPattern[0] = n[i];
        tempPattern[1] = n[j];
        tempPattern[2] = n[k];
        tempPattern[3] = n[l];

        AllPatterns[count] = tempPattern;
        count++;
      }
    }
  }
}

I need a recursive solution, but I don't know how to do it.

Aucun commentaire:

Enregistrer un commentaire