Suja wrote:
>
> Hi,
>
> Pls help me in solving the code snippet below.
> It would be helpful if you could provide explanation on deriving the answer?
>
> Find the output for this function. Let the value of m = 10;
>
> int f1( int m)
> {
> if( m <= 0 )
> {
> return 1;
> }
> else
> {
> return ( (f1(m-1)) + (f1(m-2)) );
> }
> }
>
> How many times this loop will run?
>
> a) 34
> b) 55
> c) 89
> d) Infinite loop
>
> Thanks,
> Suja.
>
>
>
> Regards,
> Suja
Various answers that are correct but not listed:
- What loop? A recursive function is precisely that - recursive.
There is no loop.
- 0. main() is missing, therefore the code will not compile and
therefore it will not run.
To get the answer your teacher is probably looking for, throw in a
main(), a global int as a counter, and a printf() and increment
statements into the function. Compile, run it, and you have your
answer. Or you could set a breakpoint with a debugger and count the
number of times it hits the breakpoint, but that could be confusing and
the first solution is cleaner.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/