Thread: permutation code

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10

    permutation code

    can someone modify this code to do general permutations? what I mean by general permutation is that I should be able to do 5p0 or 5p5 or 5p3...the source codes google helped me to find where all doing "all permutations"...

    this is a code to do general combinations using backtracking, hopefully someone can modify it to do general permutations

    Code:
    void combinationUtil(int arr[], int store[], int start, int end, int stInd, int r) 
    {    // Current combination is ready to be printed, print it 
     
        if (stInd == r) 
        { 
            for (int j=0; j<r; j++) 
            { 
             printf("%d ", store[j]); 
            } 
            
            printf("  \n"); 
        } 
        // replace index with all possible elements. The condition 
        // "end-i+1 >= r-index" makes sure that including one element 
        // at index will make a combination with remaining elements 
        // at remaining positions 
        else 
        { 
          for (int str=start; str <= end && end-str+1 >= r-stInd; str++) 
          { 
            store[stInd] = arr[str]; 
            combinationUtil(arr, store, str+1, end, stInd+1, r); 
          } 
        } 
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    Quote Originally Posted by donfire View Post
    can someone modify this code to do general permutations?
    You need to make the first attempt at making the changes you want. We then can look at your code and make suggestions.

    We cannot and do not write the code for you.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Location
    Milan, Italy, Italy
    Posts
    10
    Quote Originally Posted by rstanley View Post
    You need to make the first attempt at making the changes you want. We then can look at your code and make suggestions.

    We cannot and do not write the code for you.
    I have tried and tried for a week now and I couldn't make the code do the permutations correctly, starting by using the combination code I posted...I have edited my attempts which didn't work soo much, out of frustrations I deleted it all, if you wanna help help...I need this to insert into a bigger program I'm writing...

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    Rather then deleting your attempt at the permutation code, you should post it here in Code Tags so we can see where you went wrong.

    Please post your attempted function, along with a small main(), that creates a test program, that someone here can compile and test.

    Otherwise, we cannot hep you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. permutation
    By cmp in forum C Programming
    Replies: 2
    Last Post: 03-21-2014, 08:18 PM
  2. Help with Permutation
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 11:13 PM
  3. Permutation?
    By koolguysj in forum C Programming
    Replies: 1
    Last Post: 04-02-2005, 09:24 AM
  4. permutation
    By cerin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2005, 08:22 PM
  5. permutation
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-01-2001, 04:13 AM

Tags for this Thread