Possible duplicate:
generate lines with all permutation of characters
I am starting in C ++ and I really need your help. I am using a recursive permutation program. Here is my code, but the output is strange, repeated many times and spaces. I could not understand what the problem is, or maybe I need to add smth more. Please help me. Here is my code:
#include <iostream>
using namespace std;
#define swap(x,y,t) ((t)=(x), (x)=(y), (y)=(t))
void perm(char *list, int i, int n);
int main(){
char a[4]={'a','b','c'};
perm(a,0,3);
return 0;
}
void perm(char *list, int i, int n){
int j, temp;
if (i==n){
for (j=0; j<=n; j++)
printf("%c", list[j]);
printf(" ");
}
else {
for (j=i; j<=n; j++){
swap(list[i],list[j],temp);
perm(list,i+1,n);
swap(list[i],list[j],temp);
cout<<list<<endl;
}
}
}
source
share