473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

questions for recursion practise.

i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
Nov 4 '08 #1
12 9062
On Nov 3, 11:56*pm, Muzammil <muzammilPeer...@gmail.comwrote:
i want good practice over recursion.
can any one give *me *links for recursion questions site.?? or
question.
Consider trying "C++ recursion" of "C++ Fibonacci" in google.

You need a question? sure...

#include <iostream>

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)
What would happen if a negative number were used instead of 10?
What happens when infinite recursion occurs? Can you explain why?

Nov 4 '08 #2
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.
Nov 4 '08 #3
Muzammil ha scritto:
i want good practice over recursion.
can any one give me links for recursion questions site.?? or
question.
Here's one:

Write a program that computes the sum of x and y without using the "+"
operator or a loop.
(In general, learning the basics of the SML programming language should
give you a good understanding of recursion.)
--
Christian Hackl
ha***@sbox.tugraz.at
Nov 4 '08 #4
On Nov 4, 5:56 am, Muzammil <muzammilPeer...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 4 '08 #5
"James Kanze" <ja*********@gmail.comwrote in message
news:89**********************************@k36g2000 pri.googlegroups.com...
>On Nov 4, 5:56 am, Muzammil <muzammilPeer...@gmail.comwrote:
>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.
The first example I ever saw that showed recursion was useful
(factorial does not, anyone who can understand it can see that
iteration is easier) was the Tower of Hanoi. Summarising the
solution, in a manner that obviously works, and maps directly to
recursion

To move N discs from A to B, first move N-1 discs from A to C,
then move disc N from A to B, then move N-1 discs from B to C.

A non-recursive solution exists (of course) but is a lot less obvious.

(I'm assuming the underlying problem is well-known. If not, I'm sure
Google will give you many explanations.)
Nov 4 '08 #6
blargg wrote:
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.
It looks like it at first glance but note that the decrement is done
AFTER the conditional.

--
George Kettleborough
Nov 4 '08 #7
G Kettleborough wrote:
>>How many times will recurse() call itself? (its not 10 times)
>Really? Looks like it calls itself 10 times to me.

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.)
Nov 4 '08 #8
On Nov 4, 6:09*am, Eberhard Schefold <eberhard.schef...@de.bosch.com>
wrote:
G Kettleborough wrote:
>How many times will recurse() call itself? (its not 10 times)
Really? Looks like it calls itself 10 times to me.
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.)
The original question was worded badly, should have been 'how many
times is recurse called?". 10 recursions and 11 calls is indeed right.

Nov 4 '08 #9
Muzammil <mu*************@gmail.comwrites:
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

--
Ben.
Nov 4 '08 #10
On Nov 4, 9:51*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Muzammil <muzammilPeer...@gmail.comwrites:
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

--
Ben.
thanks. i got my point.enough.
Nov 5 '08 #11
Christian Hackl <ha***@sbox.tugraz.atkirjutas:
>
Write a program that computes the sum of x and y without using the "+"
operator or a loop.
That's easy ;-)

int sum(int x, int y) { return x-(-y);}

Paavo
Nov 6 '08 #12
Paavo Helde wrote:
Christian Hackl <ha***@sbox.tugraz.atkirjutas:
>Write a program that computes the sum of x and y without using the "+"
operator or a loop.

That's easy ;-)

int sum(int x, int y) { return x-(-y);}
That's not a program. :)
Paavo
Schobi
Nov 7 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nick | last post by:
Hi - apologies if these have been asked before. I've been looking around, but can't find answers to my question. Anyway... I'm an experienced programmer with JSP, ASP, and ASP.NET and am...
5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
26
by: pandapower | last post by:
Hi, These were some of the interview questions asked,can someone discuss whats the solution. 1.I have a singly linked list and i have to print the 5th element from the last.I have to print it...
4
by: interviewguy | last post by:
Hi, Please share best interview sites in this thread : I start with : http://dotnetinterview.googlepages.com Thanks.
20
by: athar.mirchi | last post by:
..plz define it.
5
by: feverzsj | last post by:
STL sort() as an implementation of introsort, below is the code snippet from the gnu stl. template<typename _RandomAccessIterator, typename _Size> void __introsort_loop(_RandomAccessIterator...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.