C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2013, 10:59 AM
burle burle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 burle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 55 sec
Reputation Power: 0
How multiple recursion is working in c?

Please explain how the following recursion is working and how the output is coming.

#include<stdio.h>
#include<conio.h>
int fact(int n)
{
int f,r;
if(n==1||n==0)
return 1;
f=n*fact(n-1);
printf("%d",f);
r=n+fact(n-1);
printf(" %d ",r);
return f;
}
void main()
{
clrscr();
printf("%d",fact(4));
getch();
}

Reply With Quote
  #2  
Old February 8th, 2013, 11:15 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 49 m 45 sec
Reputation Power: 1774
Please edit your code to include [code][/code] tags.
Just look at a few other recent posts to see how it's done, and what code looks like when it is done.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 9th, 2013, 10:09 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 12 sec
Reputation Power: 1800
Quote:
Originally Posted by burle
Please explain how the following recursion is working and how the output is coming.
I rather suspect that that is the question that your professor has asked of you.

A problem that can be expressed in terms of a smaller problem of the same type can be implemented by recursion. For example:

3! = 3 x 2 x 1

but also:

3! = 3 x 2!

So in general:

n! = n * (n-1)!

with an exception that:

1! = 1


So give a factorial function, the factorial of any value n can be expressed as an expression involving a further smaller factorial.

A terminating condition that is necessary that (a) guaranteed to be reached, and (b) provides an absolute answer. When the terminating condition is not met, the recursive expression is used.

To see this working in a practical sense, run your code in a debugger using single stepping and step-into the fact() function, with the call-stack display open.

Reply With Quote
  #4  
Old February 9th, 2013, 07:11 PM
burle burle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 burle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 55 sec
Reputation Power: 0
Thank u clifford
But I am unable to get your following statement.

To see this working in a practical sense, run your code in a debugger using single stepping and step-into the fact() function, with the call-stack display open.[/QUOTE]

If possible please indicate how to make call-stack display open by using the debugger

Reply With Quote
  #5  
Old February 10th, 2013, 02:22 AM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
At risk of accidentally solving a different homework problem you have, I will show you an example of recursion very similar to your factorial problem to give you an idea of what you should be going for here.

Consider the problem of exponentiation over non-negative integers. An integer base, b, raised to an integer power, e, can be expressed with a recursive function which takes two variables.

Lets call this function exp, and, because we're only considering integers (particularly, non-negative ones but I don't want to over-complicate this for you) it will return an integer and I'll use ^ to mean a mathematical exponentiation operator, not as any sort of legitimate programming syntax for you to use in your homework.

b^e = b*b*b*b*b*.....*b*b (e times)

This problem appears to have one part which you will use to recur on, and that is the exponent.

This means we have the following prototype for our exp function:

Code:
int exp(int b, int e)


For this problem, the base won't change; we're just passing it in so we know what to multiply by in our recursion. The exponent is what we will recur on. Our base case is when e = 0, since for any integer b, b^0 = 1. It is important to identify the part of the problem you will recur on, and your base case for any problem you approach with recursion. Don't confuse the terms base case and base when I mean base of the exponent. They are completely different.

So how will this function be used? When we want 10^5, for example, we will call exp(10, 5) and expect it to return 100,000 because 10^5 = 100,000.

Now lets get under the hood of this problem. We have our base, b, and our exponent e. Since the exponent is the piece we are recurring on, the first thing we always do is check for the base case for e = 0 with the following:

Code:
if(e <= 0) return 1;


That is the most important, and easiest, part of the problem. Note that I include negative ints as part of the base case, since we're only concerned with non-negative exponents and for simplicity we'll just treat any negative int as a 0.

Now comes the recursion part.
b^e = b*b*b*b*b*.....*b*b (e times)
Any time we do not have the base case, we will multiply b by the return value of a recursive call to our exp function. Since we start with exponent e, it is logical to recursively call exp with the same b and e - 1 as arguments.

Code:
else return b*exp(b, e - 1);


As a recursive function recurs down to its base case, it accumulates a stack of function calls. Lets look at the complete exp function:

Code:
int exp(int b, int e)
{
	if(e <= 0)
		return 1;
	else
		return b*exp(b, e - 1);
}


Suppose we call exp(2, 3), or in other words, we want to find 2^3.

Call 1:
e is 3, so we do not have the base case
our b is 2, and is what we will multiply by
so we recursively call exp with b, and e-1
return 2*exp(2, 2)

Call 2:
e is 2, so we do not have the base case
exp is recursively called with b, and e - 1
return 2*exp(2, 1)

Call 3:
e is 1, we don't have the base case quite yet
exp is recursively called with b, and e - 1
return 2*exp(2, 0)

Call 4:
e is 0, the base case is met and we return 1 and go back to the return statement in Call 3 ->

Call 3: return 2*1 back to the return statement in Call 2 ->
Call 2: return 2*2 back to the return statement in Call 1 ->
Call 1: return 2*4 = 8

And then finally, the exp finishes computing 2^3 and returns 8.

Last edited by jakotheshadows : February 10th, 2013 at 02:32 AM.

Reply With Quote
  #6  
Old February 10th, 2013, 04:54 AM
burle burle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 burle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 55 sec
Reputation Power: 0
Thank you very much jakotheshadows for such a wonderful explanation on recursion.

But my problem is still their i.e. how the function call and returning of values are working in multiple recursions.

Reply With Quote
  #7  
Old February 10th, 2013, 09:51 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 12 sec
Reputation Power: 1800
Quote:
Originally Posted by burle
If possible please indicate how to make call-stack display open by using the debugger
That would depend entirely on what tool chain (compiler, debugger, IDE etc.) you are using. I can't be more specific without knowing what you are using.

Reply With Quote
  #8  
Old February 10th, 2013, 10:02 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 12 sec
Reputation Power: 1800
Quote:
Originally Posted by burle
But my problem is still their i.e. how the function call and returning of values are working in multiple recursions.


Here is an illustration of calling fact(3), fact() is called recursively until the argument is 1, which returns, where it is multiplied by 2 (1 x 2 = 2) and returned and multiplied by three (3 x 2 = 6).
Code:

fact(3)
| 3 x fact(2)
|     | 2 x fact(1)
|     |     |
|     |     return(1)
|     return(2)
return(6)

Reply With Quote
  #9  
Old February 10th, 2013, 05:50 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
Quote:
Originally Posted by burle
Thank you very much jakotheshadows for such a wonderful explanation on recursion.

But my problem is still their i.e. how the function call and returning of values are working in multiple recursions.


How its working? You need to be more specific with your question. What do you mean by "multiple recursions"? Are you referring to how your fact function makes two recursive calls to fact or something else?

Reply With Quote
  #10  
Old February 11th, 2013, 10:50 AM
burle burle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 burle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 55 sec
Reputation Power: 0
Yes your guess is right. Actually I want to know how fact function is two recursive calls i.e. whether it is working simultaneously or after finishing the work of one recursion the second recursion is going on.

Reply With Quote
  #11  
Old February 11th, 2013, 07:36 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
I'll try to show you how to fish. For example,

Code:
f=n*fact(n-1);
printf("%d",f);
r=n+fact(n-1);
printf(" %d ",r);


if you want to know the exact sequence of the particular calls, try putting a printf just before the assignment to f that says "n*fact(n-1)" and put a printf just before the assignment to r that says "n+fact(n-1)", and for now comment out the original printfs. Also include the current value of n in each of these new printfs.

The first printf should look something like this:

Code:
printf("n*fact(n-1), n = %d\n", n);

Reply With Quote
  #12  
Old February 11th, 2013, 07:45 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
And also, no the two separate calls do not happen simultaneously. There is a predictable sequence to it.

Reply With Quote
  #13  
Old February 12th, 2013, 07:48 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 12 sec
Reputation Power: 1800
Quote:
Originally Posted by burle
Actually I want to know how fact function is two recursive calls i.e. whether it is working simultaneously or after finishing the work of one recursion the second recursion is going on.
recursion is not the same as concurrancy.

In the expression

3! = 3 x 2!

When you call fact(3), fact(2) runs as a sub-routione of fact(2) and in turn fact(1) runs as a sub-routine of fact(2).

As I said before, you can see this clearly by using a debugger, but after your last query on that you have not been forthcoming with the information. Moreover I attempted to illustrate the call stack in my previous response.

To be honest it can hardly be expressed more clearly than it has already, so if you don't get it yet, read the responses again and perhaps walk through the code on paper.

Reply With Quote
  #14  
Old February 12th, 2013, 08:13 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 12 sec
Reputation Power: 1800
Maybe this will make it clearer.

Code:
#include <stdio.h>

static int r_count = 0 ;
int fact(int n)
{
    int f, r;
    r_count++ ;
    r = r_count ;

    if(n==1||n==0)
    {
        printf( "%d: fact(%d) = 1\n", r, n ) ;
        return 1;
    }
    f=n*fact(n-1);

    printf( "%d: fact(%d) = %d\n", r, n, f ) ;
    return f;
}

int  main()
{
    r_count = 0 ;
    printf("%d",fact(4));
}


The output is:

Code:
4: fact(1) = 1
3: fact(2) = 2
2: fact(3) = 6
1: fact(4) = 24
24

The print is done in each case just before fact() returns and starts with a recursion count r. Notice the recursion depth starts at 4 and "unwinds" back to the top level call. i.e.:

main() calls fact(4)
fact(4) calls fact(3)
fact(3) calls fact(2)
fact(2) calls fact(1),
fact (1) prints and returns to fact(2)
fact(2) prints and returns to fact(3)
fact(3) prints and returns to fact(4)
fact(4) prints and returns to main()
main() prints the result
Comments on this post
ptr2void agrees!

Reply With Quote
  #15  
Old February 12th, 2013, 07:00 PM
burle burle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 burle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 55 sec
Reputation Power: 0
Thank u clifford.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How multiple recursion is working in c?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap