Recursive Function

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

/*Write the routines with the following declarations void permute( const string & str); void permute( const string & str, int low, int high); The first routine is a driver that calls the second and prints all the permutations of the characters in string str. If str is "abc", then the strings that are output are abc, acb, bac, bca, cab, and cba. Use recursion for the second routine*/ any solution?

Share on other sites
I smell me a homework assignment.

Share on other sites
std::next_permutation
std::prev_permutation

Share on other sites
Quote:
 Original post by armI smell me a homework assignment.

no this is a general problem.
i only want to learn how can it can be done?
can you help me please? :)

Share on other sites
nobody is interesting? :(

Share on other sites
Every recursive function has a base case, a case where the function would simply return. Then there is the inductive case, where it would call itself but slightly changed the parameter that it drops down to the base case. If you are writing a fibonacci recursive function, we know that fibonacci formula is fn = fn-1 + fn-2. The base case of fibonacci sequence is when n = 0 and n = 1. If n is 10, it calls itself for 9 and 8. The 9 will call for 8 and 7, and the 8 will call for 7 and 6, and so on until it reaches 0 and 1.

You just need to figure how permutation works, break it down to base and inductive case, then write the recursion.

Share on other sites
Quote:
 Original post by alniteEvery recursive function has a base case, a case where the function would simply return. Then there is the inductive case, where it would call itself but slightly changed the parameter that it drops down to the base case. If you are writing a fibonacci recursive function, we know that fibonacci formula is fn = fn-1 + fn-2. The base case of fibonacci sequence is when n = 0 and n = 1. If n is 10, it calls itself for 9 and 8. The 9 will call for 8 and 7, and the 8 will call for 7 and 6, and so on until it reaches 0 and 1.You just need to figure how permutation works, break it down to base and inductive case, then write the recursion.

i have problems about writing this functions base and inductive case so i couldn't write the recursion.

i searched at internet and no answer for my question.

Share on other sites
Quote:
 Original post by Jingostd::next_permutationstd::prev_permutation
Yeah, just go into the code for the STL header algorithm and you'll find how these are implemented. They're actually quite short.

Share on other sites
low and high are supposed to indicate endpoints of a substring to permute.

What is the shortest possible length for a substring? What are the permutations of such a string?

Given a way to permute n characters, how can you permute n+1 characters? Hint: What are the possibilities for the first character of a permutation? Given the first character, what are the possibilities for the others?

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013550
×