1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem in Permutations C++

  1. Sep 10, 2008 #1
    I have a serious problem which i am not being able to figure. This program finds all the permutations of a given string using recursion. The diagnostic outputs show that the values of the 'choice' and 'final' character arrays is not maintained at a position (I don't know why). Please help.(marked out in red)

    Code (Text):


    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    using namespace std;

    class Permutation
    {
      private:
        static char s[20];
        int width,l;
      public:
        void input();
        void compute();
        void recur(char choice[],char final[],int);
    };
    char Permutation::s[20];

    void Permutation::input()                                
    {
      cout<<"Enter the string:";
      gets(s);
      cout<<"Places to arrange:";
      cin>>width;
    };

    void Permutation::compute()
    {
      l=strlen(s);
      char s1[20];
      recur(s,s1,0);
    }

    void Permutation::recur(char choice[],char final[],int p)
    {
       int i,j,chk,c=0;
       if(p==width){//base condition
         cout<<"OUT: "<<final;
         cout<<endl;
         return;
       }     
       //setting choice variables
       for(i=0;i<l;i++){
           chk=0;                          
           for(j=0;j<strlen(final);j++){
             
                 if(s[i]==final[j])
                   chk=1;
           }
           if(chk==0){choice[c++]=s[i];}
       }
       choice[c]='\0';
             
      //traversing recursion     
      for(i=0;i<c-1;i++){
       
        final[p]=choice[i];
        final[p+1]='\0';cout<<"Beforein:\t "<<choice<<" -\t "<<final<<" -\t "<<p+1<<" -\t"<<i+1<<" "<<c<<endl<<endl;
        recur(choice,final,p+1);
       
        cout<<"Returned: \t"<<choice<<" - \t"<<final<<" - \t"<<p+1<<"-\t"<<i+1<<" "<<c<<endl<<endl;
      }    
    }

    int main()
    {
       Permutation arrange;
       arrange.input();
       arrange.compute();
       getch();
       return 0;
    }  

     
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Problem in Permutations C++
  1. A C problem (Replies: 3)

  2. C++ problem (Replies: 11)

  3. Problems in C++ (Replies: 5)

  4. A problem with C (Replies: 4)

Loading...