C++/C#/Algorithms
A recursive demonstration of printing combinations
The code given above, prints all valid combinations of a given array, starting at an initial index of '0'. The point of re-writing this, in my own format, is to learn that the use of indexes plays an important part in determining the success (efficiency of the code). Realize that here, I have already pushed '0' in the main and then called the Recursive function to print. A miscalculation at part such as, where 'i' in the for-loop runs till '4', that is the limit, might end up going in a tree, where it returns back from the base case and forgets to pop an element at number '4', thus printing the combination '0 1 2' or any other twice or even thrice. When making recursive calls, do make sure to carefully map out a tree and get a clearer idea, it helps to sort out errors.

Comments
Post a Comment