int Max(int a[],int n)
{
int max;
if(n==1) -------------------------------------------------------( 1 )
return a[0];
else
max=Max(a,n-1);
-------------------------------------------------------( 2 )
if(max>a[n-1])
return max;
-------------------------------------------------------( 3 )
}
else
return a[n-1];
-------------------------------------------------------( 4 )
}
Statement (1) will executed when there is only single present in the array
Statement (2) otherwise else part will executed
in this section we calling same function with array index n,n-1,......,0
(position of the elements)
Statement (3) checking whether this present element is larger than previous
one ie here we are comparing ( n )th and
(n-1) th element; if (n) th is greater then it will return its value
Statement (4)
here if (n-1) th is greater so it will return its value
-- Prashant Kulkarni
On Fri, Jun 4, 2010 at 7:13 PM, Raj N <[email protected]> wrote:
> int Max(int a[],int n)
> {
> int max;
> if(n==1)
> return a[0];
> else
> max=Max(a,n-1);
> if(max>a[n-1])
> return max;
> else
> return a[n-1];
> }
>
> Hi, the above is a code to find the max in an array recursively. I
> find very difficult in understanding the flow of recursive programs.
> Can someone help me out in explaining the flow of the program with
> stack sections if possible.
> Thanks!!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.