C program to print combination of 3 letter word -


i want print combination of 3 letters without using nested loop in c programming ?

following code combination of 3 letters "abc". can execute program using 3 loops. dont want that. want 1 loop , in non-recursive way.but not getting logic.

#include <stdio.h> #include <stdlib.h>  int main() {      int i=0,j=0;     char b[10],a[] = "abc";     strcpy(b,a);     while(i < 3)     {         b[j]=a[i];          if(i==0)         {             b[j+1]=a[i+1];             b[j+2]=a[i+2];             printf("\n%s",b);              b[j+1]=a[i+2];             b[j+2]=a[i+1];             printf("\n%s",b);          }else if(i==1)         {             b[j+1]=a[i+1];             b[j+2]=a[i-1];             printf("\n%s",b);              b[j+1]=a[i-1];             b[j+2]=a[i+1];             printf("\n%s",b);          }else         {             b[j+1]=a[i-1];             b[j+2]=a[i-2];             printf("\n%s",b);              b[j+1]=a[i-2];             b[j+2]=a[i-1];             printf("\n%s",b);         }          i++;      }      getch();     return 0; } 

if know it's going length of three, can use pre-generation minimise complexity and/or code. pre-generation act of moving complex calculations run time compile time or before hand, , can productive complex calculations.

one example (though little contrived) write program calculate possible unsigned 64-bit factorials (there's ninety of them) have program generate source code (lookup table , function) can lookup them. complexity of generating table done once, long before compilation of program use it.

this means factorial operation in program uses it, changes being possibly time consuming series of multiplications more akin 1 multiplication , addition (the table lookup).

applying same method permutations (though generation of lookup table done in head since it's not complex) give code like:

#include <stdio.h>  // permutations.  static int perm3[] = { 0,1,2,  0,2,1,                        1,0,2,  1,2,0,                        2,0,1,  2,1,0 };  int main (void) {     int i;     char x[] = {'a', 'b', 'c'};      // permute using table indexes.      (i = 0; < sizeof(perm3) / sizeof(perm3[0]); += 3)         printf ("%c %c %c\n", x[perm3[i]], x[perm3[i+1]], x[perm3[i+2]]);      return 0; } 

now, people may consider "cheating" i'll honest here, of fastest programs can write cheat in manner :-)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -