By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 454,948 Members | 1,215 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

# questions for recursion practise.

 P: n/a i want good practice over recursion. can any one give me links for recursion questions site.?? or question. Nov 4 '08 #1
Share this Question
12 Replies

 P: n/a On Nov 3, 11:56*pm, Muzammil 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

 P: n/a 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

 P: n/a 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

 P: n/a On Nov 4, 5:56 am, Muzammil

 P: n/a "James Kanze" On Nov 4, 5:56 am, Muzammil i want good practice over recursion.can any one give me links for recursion questions site.?? orquestion. >Well, factorial and fibonaci are the classical examples,although both have iterative solutions which are perhapssimpler. What you might try is a simple expression parser whichhandles 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

 P: n/a 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

 P: n/a 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

 P: n/a On Nov 4, 6:09*am, Eberhard Schefold 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

 P: n/a Muzammil

 P: n/a On Nov 4, 9:51*pm, Ben Bacarisse

 P: n/a Christian Hackl 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

 P: n/a Paavo Helde wrote: Christian Hackl 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 discussion thread is closed

Replies have been disabled for this discussion.