Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Recursively

Reply
Thread Tools

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 ?
>
> Thx in advans,
> 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".

 
Reply With Quote
 
 
 
 
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 ?
>
> Thx in advans,
> 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".

 
Reply With Quote
 
 
 
 
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")




 
Reply With Quote
 
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 ?
>
> Thx in advans,
> 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.

 
Reply With Quote
 
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?



 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      08-20-2007
elmerpdc wrote:
> just step through the code like below...best regards
>

Why????
 
Reply With Quote
 
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.

>
> Thx in advans,
> 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 *
 
Reply With Quote
 
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 ?

>
> > Thx in advans,
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting bookmarks recursively? taras.di@gmail.com Firefox 0 07-31-2005 06:02 AM
Best way to recursively populate a tree based on SQL Server 2000 backend Mark ASP .Net 9 05-28-2005 08:54 PM
adding subnodes to a tree recursively =?Utf-8?B?RGFuIE5hc2g=?= ASP .Net 10 10-18-2004 12:29 AM
Help - Recursively Opening & Closing All Nodes in Treeview Lanny McDonald ASP .Net 1 04-03-2004 05:11 PM
concatenate files recursively in win32 Mike James Perl 1 09-19-2003 03:51 PM



Advertisments