Solved

Recursive function trace

Posted on 2007-11-16
10
279 Views
Last Modified: 2010-04-15
Hey guys,

I have little experience with recursion and was wondering if someone could help me figure out how to trace it. I need to obtain its output by calling it with a particular parameter, such as 7, for example. Is this as simple as putting printf() statements or am I missing something big?


int MyFunc(int x) {
 
	if (x <= 1) {
		return (1);
	} else {
		return (x * MyFunc(x - 2));	
	}
}

Open in new window

0
Comment
Question by:Koraxen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 10

Expert Comment

by:peetm
ID: 20298833
Something like this?

#include<stdio.h>

int MyFunc(int x)
{
    printf("In MyFunc x = %d\n", x);

        if (x <= 1)
        {
            return 1;
        }
        else
        {
                int d = MyFunc(x - 2);
               
                printf("Tail from 'else', x = %d d = %d\n", x, d);
               
                return x * d;
        }
}

int main(void)
{
    printf("%d\n", MyFunc(10));
   
    getchar();
   
    return 0;
}
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 20298872
Yes, you can just add printf statements to show the progress. Take care to place them in the correct location though. More specifically : if you place the printf somewhere before the recursive call, it will be shown in the order that the functions are recursively called - if you place the printf somewhere after the recursive call, it will be shown in the order that the recursive calls are unwound.

The next code for example will show for input 7 :

7
5
3
1
int MyFunc(int x) {
        printf("%d\n", x);
        if (x <= 1) {
                return (1);
        } else {
                return (x * MyFunc(x - 2));     
        }
}

Open in new window

0
 

Author Comment

by:Koraxen
ID: 20301210
I understand. I think this might be a trick question though, because it seems too straightforward. Given that snippet of code, the actual question is:

What is the value of A, where "A = SpecialFunction (7)"

I can easily throw this into the compiler and run it, and get 105.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20301257
>> I can easily throw this into the compiler and run it, and get 105.

Yes. And what's your question ?
0
 

Author Comment

by:Koraxen
ID: 20302464
It just sounds like one of those questions that's too easy to be true. Either I'm expected to trace it by hand or there's some trick to it that I'm not seeing.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20302477
Is this an assignment ? If so, can you post the exact wording of it ?
0
 

Author Comment

by:Koraxen
ID: 20302501
Yes.

=============
Consider the following function:

int MyFunc(int x) {
      if (x <= 1) {
            return (1);
      } else {
            return (x * MyFunc(x - 2));      
      }
}

What is the value of A where "A = MyFunc(7)" ?
=============
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20302556
Then you've already got the answer - you posted it earlier : 105.

If you need to add a printf to the code, then just do this :

        printf("%d", MyFunc(7));
0
 
LVL 6

Expert Comment

by:billtouch
ID: 20323922
Just for fun, you might add a counter and increment it upon entry and decrement it on exit. Include the counter in your printf(), and you can watch your recursion in action.

Working from peetm's version:

int MyFunc(int x)
{
    int ret;
    static counter=0;
    counter++;
    printf("(%d) In MyFunc x = %d\n", counter, x);

    if (x <= 1) {
        ret = 1;
    } else {
        int d = MyFunc(x - 2);
        printf("(%d) Tail from 'else', x = %d d = %d\n", counter, x, d);
        ret = (x * d);
    }
    counter--;
    return ret;
}

int main(void)
{
    printf("%d\n", MyFunc(10));
    getchar();
    return 0;
}


Bill
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
ID: 20327668
>>>> It just sounds like one of those questions that's too easy to be true.

MyFunc(7) = 7 * MyFunc(5) =
                    7 * 5 *MyFunc(3) =
                    7 * 5 * 3 * MyFunc(1) =
                    7*5*3*1 =
                   105.

Do you have any problems with that?

Regards, Alex

     
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C#, VS15, StructLayout 1 137
C simple state machine for client server handshake 4 446
Why isn't object file created? 6 105
Reactjs with .NET 3 124
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question