Velocity Reviews > Recursively

# Recursively

IDDLife
Guest
Posts: n/a

 08-20-2007
On 8 19 , 4 39 , karthikbalaguru <(E-Mail Removed)>
wrote:
> Hi,
>
> Need some clarification regarding the following example w.r.t
> recursive .
>
> void f(char *) ;
> main()
> {
> f("123");}
>
> void f(char a[])
> {
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c", a[1]);
>
> }
>
> The output for this program has been stated as 332.
> How is it possible ?
>
> It should actually return without printing anything.
> How can one get '332' as output ?
>
> Karthik Balaguru

f("123") ------- a[1]=2
|_f("23") ----------a[1] = 3
|_f("3")
------------ a[1] = '\0' -------- return;
|_f("3")
-------------a[1] = '\0' --------- return;
|
_printf("%c", a[1]); ---------"3"
|_f("23") ----------a[1] = 3
|_f("3")
------------ a[1] = '\0' -------- return;
|_f("3")
-------------a[1] = '\0' --------- return;
|
_printf("%c", a[1]); ---------"3"
|_printf("%c", a[1]); ---------"2"

So the result is the "332".

IDDLife
Guest
Posts: n/a

 08-20-2007
On 8 19 , 4 39 , karthikbalaguru <(E-Mail Removed)>
wrote:
> Hi,
>
> Need some clarification regarding the following example w.r.t
> recursive .
>
> void f(char *) ;
> main()
> {
> f("123");}
>
> void f(char a[])
> {
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c", a[1]);
>
> }
>
> The output for this program has been stated as 332.
> How is it possible ?
>
> It should actually return without printing anything.
> How can one get '332' as output ?
>
> Karthik Balaguru

f("123") ------ a[1]='2'
|_ f("23") -------- a[1] ='3'
|_f("3")
---------- a[1]='\0' -----------return;
|_f("3")
---------- a[1]='\0' -----------return;
|_printf("%c",
a[1]) -------"3"
|_ f("23") -------- a[1] ='3'
|_f("3")
---------- a[1]='\0' -----------return;
|_f("3")
---------- a[1]='\0' -----------return;
|_printf("%c",
a[1]) -------"3"
|_ printf("%c", a[1]) -------"2"

So the result is the "332".

elmerpdc
Guest
Posts: n/a

 08-20-2007
just step through the code like below...best regards

f("123")
{
if ('2' == '\0') // false
return;
f("23")
{
if ('3' == '\0') // false
return;
f("3")
{
if ('\0' == '\0') // true
return;
}
f("3")
{
if ('\0' == '\0') // true
return;
}
printf("%c", a[1]); // prints "3"
} // f("23")
f("23")
{
if ('3' == '\0') // false
return;
f("3")
{
if ('\0' == '\0') // true
return;
}
f("3")
{
if ('\0' == '\0') // true
return;
}
printf("%c", a[1]); // prints "3"
} // f("23")
printf("%c", a[1]); // prints "2"
} // f("123")

Dibyendu
Guest
Posts: n/a

 08-20-2007
On Aug 19, 1:39 pm, karthikbalaguru <(E-Mail Removed)>
wrote:
> Hi,
>
> Need some clarification regarding the following example w.r.t
> recursive .
>
> void f(char *) ;
> main()
> {
> f("123");}
>
> void f(char a[])
> {
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c", a[1]);
>
> }
>
> The output for this program has been stated as 332.
> How is it possible ?
>
> It should actually return without printing anything.
> How can one get '332' as output ?
>
> Karthik Balaguru

Modify the function like:

void f(char *a)
{
printf("\nstart ->> %c %c %c\n", a[0],a[1],a[2]);

if(a[1]=='\0')
return;
f(a+1);
f(a+1);
printf("%c\n", a[1]);

}

Peter Dickerson
Guest
Posts: n/a

 08-20-2007
karthikbalaguru" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> Need some clarification regarding the following example w.r.t
> recursive .
>
> void f(char *) ;
> main()
> {
> f("123");
> }
> void f(char a[])
> {
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c", a[1]);

and what's wrong with putchar?

> }
>
> The output for this program has been stated as 332.
> How is it possible ?

Why not compile it and see.

> It should actually return without printing anything.

Why do you think that?

> How can one get '332' as output ?

Why not debug the code to see what it does and why?

Sjouke Burry
Guest
Posts: n/a

 08-20-2007
elmerpdc wrote:
> just step through the code like below...best regards
>

Why????

Ian Shef
Guest
Posts: n/a

 08-21-2007
karthikbalaguru <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> Hi,
>
> Need some clarification regarding the following example w.r.t
> recursive .
>
>
> void f(char *) ;
> main()
> {
> f("123");
> }
> void f(char a[])
> {
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c", a[1]);
> }
>
> The output for this program has been stated as 332.
> How is it possible ?

Quite simple. This is a test of certain concepts, such as:
duality between arrays and pointers
how arrays are indexed (zero-based)
how character strings are stored (null terminated).
>
> It should actually return without printing anything.

Wrong. It is simple to compile and run this program. Have you even tried?

> How can one get '332' as output ?

It is time for you to do your own homework.
If you want help, you should supply some information yourself, such as how
far you get when you pretend to be the computer yourself.
Alternatively, you might use a debugger or stick in some additional printf
statements to follow the progress of the program as it executes.

It may help to play computer, and keep track of the variables on paper.
Remeber that each call to the function f sets up a new definition of what
a[] is, until that particular call returns.

>
> Karthik Balaguru
>
>

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *

karthikbalaguru
Guest
Posts: n/a

 08-21-2007
On Aug 20, 2:20 pm, Dibyendu <(E-Mail Removed)> wrote:
> On Aug 19, 1:39 pm, karthikbalaguru <(E-Mail Removed)>
> wrote:
>
>
>
>
>
> > Hi,

>
> > Need some clarification regarding the following example w.r.t
> > recursive .

>
> > void f(char *) ;
> > main()
> > {
> > f("123");}

>
> > void f(char a[])
> > {
> > if(a[1]=='\0')
> > return;
> > f(a+1);
> > f(a+1);
> > printf("%c", a[1]);

>
> > }

>
> > The output for this program has been stated as 332.
> > How is it possible ?

>
> > It should actually return without printing anything.
> > How can one get '332' as output ?

>
> > Karthik Balaguru

>
> Modify the function like:
>
> void f(char *a)
> {
> printf("\nstart ->> %c %c %c\n", a[0],a[1],a[2]);
>
> if(a[1]=='\0')
> return;
> f(a+1);
> f(a+1);
> printf("%c\n", a[1]);
>
> }
>
> If there is any more problem, please ask me.- Hide quoted text -
>
> - Show quoted text -

Thx for your tricks to track the flow.

Thx,
Karthik Balaguru