Thread: permutations

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68

    permutations

    how to make permutations?
    Code:
    if we input 1
    1
    
    input 2
    1 2
    2 1
    
    input 3
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    
    until we input 5..(so it will print 120, from 5!=5*4*3*2*1)
    can anyone understand or get it?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So how would you make a program that prints out permutations like this?

    maybe:

    put the numbers into an array

    sort the array into ascending (low to high) order

    You can see the numbers, if they were digits on a car's odometer, just reflect the odometer's wheels, as the car logs in km or miles.

    You could use nested for loops for these "wheels".

    You could also use a while or do while loop and handled the logic, inside that loop. Perhaps just by a system of swapping them around.

    Play with that by hand, and see if it puts any light on the matter.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    I don't really get whole what you mean,, but I will try, and help me if I wrong alright.. ^^

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't see those permutations moving in the same way as the digits on a car's odometer?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    hmm..
    the last number will add continously until the limit is 3, after that, one before the last, will add one, and if same with the first one, it will change each other(1 to 2, and 2 to 1)?
    can I use like that? sorry if my english is not really good..

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    damn! it won't work.. it will return to the beginning..

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If the numbers are consecutive, then they can work like an odometer's "wheels".

    If the numbers are not next to each other in value, then you need to use the swap type of logic.

    Wikipedia has a big page on permutations, btw.

    This is the list of all the permutations for 3 numbers between 1 and 3, in order. Your list did not include all the permutations.


    Code:
      for(n1 = 1; n1 < 4; n1++)  {
        printf("\n");
        for(n2 = 1; n2 < 4; n2++) {
          for(n3 = 1; n3 < 4; n3++) {
            printf("%d%d%d   ", n1, n2, n3);
          }
        }
      }
    Which gives us:

    111 112 113 121 122 123 131 132 133

    211 212 213 221 222 223 231 232 233

    311 312 313 321 322 323 331 332 333

    press enter when ready

    When it's put into a program and run.

    Were you thinking of just all the combinations, instead?
    Last edited by Adak; 10-31-2009 at 03:45 PM.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    waw.... after that, we have just only use "\b" or throw away that have the same number right?
    by the way,, how it would be, if we must use recursive function?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want the combinations, you don't want to generate all the permutations to get them.
    Generating all combinations is much quicker than generating all the permutations.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    can we use recursif function to print the permutations?

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    int a;
    char huruf[6];
    
    int permutasi(char set[6],int mulai,int selesai)
    {
    	int jarak,i;
    	char sementara;
    	jarak=selesai-mulai;
    	if(jarak==1)
    	{
    	printf(" %s\n",set);
    	}
    	else
    	{
    	for(i=0;i<jarak;i++)
    	{
    		sementara=set[mulai+i];
    		set[mulai+i]=set[mulai];
    		set[mulai]=sementara;
    	permutasi(set,mulai+1,selesai);
    	sementara=set[mulai+i];
    	set[mulai+i]=set[mulai];
    	set[mulai]=sementara;
    	}
    	}
    return 0;
    }
    
    int main()
    {	int a;
    	
    
    do{do{a=0;
    	printf("masukkan n(1-5) : ");
    	scanf("%d",&a);fflush(stdin);
    }while(!(a>0));
    }while(a>1&&a>5);
    	switch(a)
    	{
    	case 1:
    		strcpy(huruf,"1");
    		break;
    	case 2:
    		strcpy(huruf,"12");
    		break;
    	case 3:
    		strcpy(huruf,"123");
    		break;
    	case 4:
    		strcpy(huruf,"1234");
    		break;
    	case 5:
    		strcpy(huruf,"12345");
    		break;
    	}
    
    	permutasi(huruf,0,strlen(huruf));
    		getchar();
    }
    I have already finished it, by using char.. but I still cann't get it,, how the recursift function work.. T_T

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Permutations
    By bumfluff in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2008, 12:33 PM
  2. recursive permutations
    By wd_kendrick in forum C Programming
    Replies: 7
    Last Post: 06-02-2008, 03:11 PM
  3. permutations of a string
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-23-2006, 08:52 AM
  4. permutations
    By packer in forum C Programming
    Replies: 13
    Last Post: 06-02-2005, 12:54 AM
  5. Permutations
    By kiddy in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 09:53 AM