how to trace a recursive function C++
#include <iostream>
using names开发者_如何学运维pace std;
int g(float A[] , int L , int H)
{
if (L==H)
if (A[L] > 0.0)
return 1;
else
return 0;
int M = (L+H)/2;
return g(A,L,M)+ g(A,M+1,H);
}
int main (void)
{
float A[] = {-1.5 ,3.1,-5.2,0.0};
g(A,0,3);
system ("pause");
return 0;
}
its asking me what is return by the function g and what the function does
here is what i got so far
first call is g(A , 0 ,3) -total skip the IF statement and M = 1 since it its a int -return g(A,1,3) + g(A,2 3)
second call - g(A,1,3) skipp the if statement again - M = 0; - g(A,2 3) skip the if statement again - M= 2;
third call -g(A, 0,0,) return 0 -g(A,3,3) return 0;
so it just return 0?
and i am guessing it is dividing the middle value and some sort of binary search?
It's a convoluted way to count how many numbers in the array is greater than 0. And if you try to run this in a compiler, the return value is 1 because the only number that is greater than 0 in the array is 3.1.
at first run:
{-1.5, 3.1, -5.2, 0.0}
0 1 2 3
L H
then since L=0
and H=3
, M = (0+3)/2 = 3/2 = 1
when you get to g(A, L, M) + g(A, M+1, H)
, you branch into two:
{-1.5, 3.1, -5.2, 0.0}
0 1 2 3
L H
L1 H1 L2 H2
let's do the left part g(A, L1, H1) = g(A, 0, 1)
first:
{-1.5, 3.1, -5.2, 0.0}
0 1 2 3
L H
L1 H1 L2 H2
^^^^^^^
again since L1=0
, H1=1
, and so M1 = (0+1)/2 = 1/2 = 0
and you branch into two again g(A, 0, 0)
and g(A, 1, 1)
:
{-1.5, 3.1, -5.2, 0.0}
0 1 2 3
L H
L1 H1 L2 H2
L11,H11 L12,H12
on the left part, since -1.5 <= 0
therefore g(A, L11, H11) = g(A, 0, 0) = 0
, on the right part, since 3.1 > 0
therefore g(A, L12, H12) = g(A, 1, 1) = 1
.
So therefore g(A, 0, 1) = g(A, 0, 0) + g(A, 1, 1) = 1
.
Do the same with g(A, L2, H2)
, and you get that g(A, L, H) = g(A, L1, H1) + g(A, L2, H2) = 1 + 0 = 1
.
@Nawaz had a good idea of visualizing this into a binary tree, basically you start with at the root of the tree:
{-1.5, 3.1, -5.2, 0.0}
At the second layer of iteration, you split the array into two:
{-1.5, 3.1, -5.2, 0.0}
/ \
/ \
/ \
/ \
{-1.5, 3.1} {-5.2, 0.0}
At the third layer, you split again:
{-1.5, 3.1, -5.2, 0.0}
/ \
/ \
/ \
/ \
{-1.5, 3.1} {-5.2, 0.0}
/ \ / \
/ \ / \
{-1.5} {3.1} {-5.2} {0.0}
At this point L==H
so, we can evaluate the nodes:
{-1.5, 3.1, -5.2, 0.0}
/ \
/ \
/ \
/ \
{-1.5, 3.1} {-5.2, 0.0}
/ \ / \
/ \ / \
{-1.5} {3.1} {-5.2} {0.0}
| | | |
0 1 0 0
and to find the return values, we sum up:
{-1.5, 3.1, -5.2, 0.0}
/ \
/ \
/ \
/ \
{-1.5, 3.1} {-5.2, 0.0}
0+1=1 0+0=0
and lastly
{-1.5, 3.1, -5.2, 0.0}
1+0=1
and M = 1 since it its a int -return g(A,0,3) + g(A,2 3)
Here is first problem. If M = 1, then how do you say it's return g(A,0,3) + g(A,2, 3)
?
It should be:
return g(A,0,1) + g(A,2, 3)
//^ this should be 1
And since you're wrong at the first step, all the consecutive steps will be wrong.
My suggestion would be :
- Take a pencil and white paper. Be ready to draw a binary tree, that expands downwards
- Write the root node as
g(A,0,3);
. This is the first call. - Then make two child nodes: left and right
- Write
g(A,0,1)
as the left node, andg(A,2,3)
as right node. - Then again make two child nodes of each child, and keep drawing nodes by repeating this step, till the
if
condition becomes true. - Once the
if
condition becomes true, stop making new nodes (in that branch), and go up towards the root node, summing the values of nodes which you meet on the way. - What you get at the root node, is the return value of original call from
main()
.
精彩评论