Click here to Skip to main content
15,914,221 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to get the subkey value using c++ Pin
Mark Salsbery10-Sep-08 7:21
Mark Salsbery10-Sep-08 7:21 
GeneralRe: how to get the subkey value using c++ Pin
jeron110-Sep-08 7:27
jeron110-Sep-08 7:27 
GeneralRe: how to get the subkey value using c++ Pin
Perspx10-Sep-08 8:28
Perspx10-Sep-08 8:28 
QuestionProblem in Permutations Pin
ron_jay10-Sep-08 4:44
ron_jay10-Sep-08 4:44 
QuestionRe: Problem in Permutations Pin
CPallini10-Sep-08 5:18
mveCPallini10-Sep-08 5:18 
AnswerRe: Problem in Permutations Pin
ron_jay10-Sep-08 6:05
ron_jay10-Sep-08 6:05 
GeneralRe: Problem in Permutations Pin
CPallini10-Sep-08 11:09
mveCPallini10-Sep-08 11:09 
GeneralRe: Problem in Permutations Pin
ron_jay10-Sep-08 20:19
ron_jay10-Sep-08 20:19 
I really appreciate the the code you compiled. Smile | :) It is simplified.I would like to state my problems very clearly with the code I wrote.The crux of the problem is my inability to understand why the recursion procedure is not following the pattern it was supposed to:

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];


the above is the class definition, easy enough.

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


function to enter the values.

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


this is the function which calls the recursive function

RECURSIVE PART:

1.
void Permutation::recur(char choice[],char final[],int p)


choice[]: contains all possible letters which can take the position
eg. care - for the first position > c,a,r,e is stored as 'care' when the first is set to 'c' then for the second position is 'a,r,e' and so on..

final[]: stores the string to be printed..when all the spaces are filled , it is printed

p: holds the position where a character has to be inserted from the choice[] to final[]
2.

int i,j,chk,c=0;
   if(p==width){//base condition
     cout<<"OUT: "<<final;
	 cout<<endl;
	 return;
   }	  >


this is the base conditon: if the width is reached it prints the string

3.

//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';


This is the part in the recursive function which finds all the possible characters which can be put into a space...it checks the initial string and compares it to final[] ...the choice[] are the ones which aren't present in final.

4.

//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;
  }	    
}


The chart of the recursive function is shown here:
http://i452.photobucket.com/albums/qq241/ronjay_2008/Misc/chart.jpg[^]

the characters assigned for a position on final[] are taken from choice[]

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


Simple class construction

5. What I did not understand:

Since I wasn't getting the output, I added diagnostic check in the recursive function to check how the values were changing on each step. I knew that in the stack for each recursive iteration a separate instance of the array is created (choice[],final[] and p),but the output did not conform.

check this:
http://i452.photobucket.com/albums/qq241/ronjay_2008/Misc/OUTPUT.jpg[^]

The part in yellow shows the state at an earlier iteration. Therefore, when 'recur' return to that state later, it should have printed the same values of choice[] and final[] which is not so. Only p,i and c are consistent. Whatever happened to choice[] and final[]? They should have retained their original values which according to the ouput it hasn't.

Maybe, there is minor error which i have overlooked. Please sort this.

If you want the .exe file its here:

output_permutation.exe

QuestionHow to identify an EXE is whether a COM component or Not Pin
NiceNaidu10-Sep-08 3:20
NiceNaidu10-Sep-08 3:20 
AnswerRe: How to identify an EXE is whether a COM component or Not Pin
Chris Losinger10-Sep-08 7:00
professionalChris Losinger10-Sep-08 7:00 
GeneralRe: How to identify an EXE is whether a COM component or Not Pin
Michael Dunn10-Sep-08 11:31
sitebuilderMichael Dunn10-Sep-08 11:31 
GeneralRe: How to identify an EXE is whether a COM component or Not Pin
Chris Losinger10-Sep-08 11:51
professionalChris Losinger10-Sep-08 11:51 
GeneralRe: How to identify an EXE is whether a COM component or Not Pin
NiceNaidu10-Sep-08 18:35
NiceNaidu10-Sep-08 18:35 
QuestionDual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 3:02
logiqworks10-Sep-08 3:02 
AnswerRe: Dual monitor, pop up window goes to primary display Pin
SandipG 10-Sep-08 3:29
SandipG 10-Sep-08 3:29 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 4:50
logiqworks10-Sep-08 4:50 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 5:01
logiqworks10-Sep-08 5:01 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
vishalmore10-Sep-08 20:05
vishalmore10-Sep-08 20:05 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 21:58
logiqworks10-Sep-08 21:58 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
SandipG 10-Sep-08 20:21
SandipG 10-Sep-08 20:21 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 21:56
logiqworks10-Sep-08 21:56 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
SandipG 10-Sep-08 22:46
SandipG 10-Sep-08 22:46 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 23:21
logiqworks10-Sep-08 23:21 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
SandipG 10-Sep-08 23:34
SandipG 10-Sep-08 23:34 
GeneralRe: Dual monitor, pop up window goes to primary display Pin
logiqworks10-Sep-08 23:58
logiqworks10-Sep-08 23:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.