If you want to print the sequence in order and also want to use recursion, then keep a global variable N which varies from 1 to 3 (if you want max length of string as 3). In permute(n) method, change condition to 'if (n<N-1)' and call permute(0) 3 times. See code below :
int N;
void permute(int n){
for(int i=0; i<5; ++i){
A[n]=i+1;
if(n<N-1){
permute(n+1);
} else {
for(int j=0; j<N; ++j)
cout << A[j];
cout << endl;
}
}
}
int main()
for(int i=0; i<5; ++i){
A[n]=i+1;
if(n<N-1){
permute(n+1);
} else {
for(int j=0; j<N; ++j)
cout << A[j];
cout << endl;
}
}
}
int main()
{
....
for (N=1; N<=3; N++)
permute(0);
}
On 11/28/05, Abhi <[EMAIL PROTECTED]> wrote:
Hi gaijinco,
I think the result which you want can be best achieved just using N+1
loops. Also the non-recursive programs are much faster to execute and
easier to understand.
Regards,
-Abhi
--
Vishal Padwal
-----------------------------------
Master of Science
Computer Science Dept.
Stony Brook University
Stony Brook, NY
Tel : 631-645-1406