Which resources can be useful to learn recursive functions?
-
I am a beginner level c++ programmer, I have great difficulty in understanding recursive functions. I understand the theory, but when it comes to coding I can't code them. Which problems shou
-
Answer:
Do you understand recursion? If not, see this Quora question:
Alistair Israel at Quora Visit the source
Other answers
From my experience, the two most important concepts you need to fully grasp about recursion are the state and the base case, and I'll try explain what I mean with the 8-queen problem. Basically, the problem is: find a way (or all ways) to put 8 queens on a chessboard, such that no queen attacks another. Let's pretend we have a function called solve_queens(). We probably need to pass a chessboard structure into this function to get a solution. First, let's think about what determines a chessboard in this case? Well, we know that the only way this is solvable is if there is exactly one queen in each row, so if we have an array of ints (e.g. [2, 4, 8, 6, 3, 5, 1, 7]), we can interpret this as "queen at: 1st row, 2nd column; 2nd row, 4th column; etc.", thereby constructing our current state with just this array. Or, does it? We know we need to pass a chessboard (aka. array of ints) into the function, but it turns out we also need to pass something else: the column number we're currently at! Otherwise, we'll have a very hard time recursing down to the "base case", since we don't know where we can put a queen next. So, let's see if we can construct the function now: int * solve_queens (int chessboard[], int col) { for (every cell in the current column) { if (no_conflicts(cell, chessboard)) { //add this cell to a copy of the chessboard: temp_chessboard int * ans = solve_queens(temp_chessboard, col+1); if (is_valid(ans)) return ans; } } } It should be obvious to see how our solution reduces the question, column-by-column. However, we're still missing something in our solution: the base case! We haven't noted what we want to return when, say, col = 8. This is crucial. We must remember to put that in there: int * solve_queens (int chessboard[], int col) { if (col == 8) return chessboard; for (every cell in the current column) { if (no_conflicts(cell, chessboard)) { //add this cell to a copy of the chessboard: temp_chessboard int * ans = solve_queens(temp_chessboard, col+1); if (is_valid(ans)) return ans; } } } Therefore, we have a recursive function with clear states (chessboard, col), and a clear base case (when col == 8, or when we went through 8 columns already).
Shine Wang
Related Q & A:
- Can you help me learn how to copy and paste an html link to my website?Best solution by Yahoo! Answers
- Can I restrict which members can access the Files?Best solution by Drupal Answers
- Which foreign language is difficult to learn and speak?Best solution by ChaCha
- Which language would be best to learn if I'm going into the Medical Field?Best solution by Yahoo! Answers
- Can a duty statement be recursive?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.