Thread: permutation

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    permutation

    I didn't have a clue where to start on this help-free challenge. So I examined the solution and then stared blankly at it.
    Please explain this:
    Code:
    #include <iostream>
    #include <string>
    
    string switch(string topermute, int x, int y)
    {
    	string newstring = topermute;
    	newstring[x] = newstring[y];
    	newstring[y] = topermute[x]; //avoids temp variable
    	return newstring;
    }
    void permute(string topermute, int place)
    {
    	if(place == topermute.length - 1)
    	{
    		cout<<topermute<<endl;
    	}
    	for(int nextchar = place; nextchar < 
    	    topermute.length(); nextchar++)
    	{
    		permute(switch(topermute, place, nextchar),
    		        place+1);
    	}
    }
    
    int main(int argc, char* argv[])
    {
    	if(argc!=2)
    	{
    		cout<<"Proper input is 'permute string'";
    	}
    	else
    	{
    		permute(argv[1], 0);
    	}
    	return 0;
    }
    My computer is awesome.

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Sure. Would you like some beer while we explain it to you?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nothing to explain as it won't compile:
    Code:
    string switch(string topermute, int x, int y)
    You can't have a function called switch

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Oh well then tell the webmaster that cause I copy and pasted into the post.
    My computer is awesome.

  5. #5
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    1,012
    Indeed, I read Thantos's post and have fixed the code on the site.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Problem creating a recursive string permutation function
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2006, 10:09 AM
  3. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  4. Permutation Calculation
    By Eric Hansen in forum C++ Programming
    Replies: 21
    Last Post: 06-11-2003, 04:03 PM
  5. INT ARRAY Permutation!
    By arthur5005 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 05:30 AM