🔒 Locked
Recursive Function
Advertisement
Original Post
/*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?
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.
You just need to figure how permutation works, break it down to base and inductive case, then write the recursion.
Quote:
Original post by alnite
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.
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.
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?
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?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement