M
Muzammil
i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
can any one give me links for recursion questions site.?? or
question.
i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
Well, factorial and fibonaci are the classical examples,
although both have iterative solutions which are perhaps
simpler. What you might try is a simple expression parser which
handles parentheses.
blargg said:Salt_Peter wrote:
[...]void recurse(int n)
{
std::cout << "n = " << n << std::endl;
if(n > 0)
recurse(--n);
}
int main()
{
recurse(10);
}
How many times will recurse() call itself? (its not 10 times)
Really? Looks like it calls itself 10 times to me.
G said:It looks like it at first glance but note that the decrement is done
AFTER the conditional.
I, too, believe that recurse() calls itself 10 times. (The number of all
calls to recurse() is 11.)
Muzammil said:i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
Muzammil said:i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
The best examples are ones where a recursive solution is natural.
This is often the case with nested structures (binary tree algorithms
for example) or which use "divide and conquer" (merge sort of a linked
list). If these involve data structures you have not yet learnt, then
here are some other examples:
Counting change. Given a set of denominations for coinage (e.g. {1,
2, 5, 10, 20, 50}) how many ways are there of making up a given amount
of change? For example, there are 68 ways to make 25 from those
denominations.
Solve the Towers of Hanoi puzzle (just search the web for it, but
avert your eye from any solutions you might come across!).
If have access to a graphics package, write programs to draw some
fractal shapes. The Koch snowflake is one of my favourites, though
you can have more creative fun drawing recursive trees: each tree is a
trunk with 2 or maybe 3 trees growing from the top at randomly chosen
angles. At the limit of the recursion, draw a leaf. You can have
great fun altering the range of angles you choose for the branches and
the way the trunk length shrinks (or grows!) with the recursion.
Searching for solutions to puzzles is often naturally recursive. For
example, how many ways are there of putting N queens on an NxN chess
board so that no two queens are on the same row, rank or diagonal?
Write a simple pattern matcher. A pattern is either a primitive or a
sequence of primitives, or a primitive followed by * to indicate zero
or more repetitions of the preceding primitive. It helps to have a
primitive like . that can match any single character. Once you've got
a really clean implementation of this, add in ()s that can turn a
sequence into a primitive. I.e. a(bc)*d maches "ad", "abcd", "abcbcd"
etc.
That should be enough for a while!
[1]http://en.wikipedia.org/wiki/Koch_snowflake
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.