Recursively

Discussion in 'C Programming' started by karthikbalaguru, Aug 19, 2007.

  1. 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
    karthikbalaguru, Aug 19, 2007
    #1
    1. Advertising

  2. karthikbalaguru

    Ian Collins Guest

    karthikbalaguru 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 ?
    >

    Why? What happens when f is called with "3"?

    --
    Ian Collins.
    Ian Collins, Aug 19, 2007
    #2
    1. Advertising

  3. In article <>,
    karthikbalaguru <> wrote:

    >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 ?


    f("123") is called. It calls f("23") twice, then prints "2".
    f("23") calls f("3") twice, then prints "3".
    f("3") doesn't print anything.

    So "3" gets printed twice, then "2".

    Whether you actually see anything is system dependent, because no
    linefeed is printed at the end.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Aug 19, 2007
    #3
  4. karthikbalaguru

    Chris Dollin Guest

    karthikbalaguru wrote:

    > 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 ?


    That's the way it works.

    > It should actually return without printing anything.


    How? If you consider only the outermost call, /that/
    returns printing something [1] -- "123"[1] isn't '\0',
    so it will do the final `printf`.

    > How can one get '332' as output ?


    Let's look at the code.

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


    I'd actually rewrite it as

    if (a[1] != '\0')
    {
    f( a + 1 );
    f( a + 1 );
    putchar( a[1] );
    }

    [Actually I'd write that first bit as `if (a[1])`; I have
    no fears of C's conditional tests.]

    The test is "is there are second character in this string?"
    If so, print the rest of the string, again. Then print this
    second character. No problem.

    [Whoever wrote the code was either playing head-games with the
    student's ideas about indexing or didn't really understand C's
    indexing themselves. Or something.]

    [1] Except there's no final newline, so perhaps it won't print
    anything -- easily fixed by adding `putchar( '\n' );` [and
    `return 0;`] at the end of `main`.

    --
    Primitive, Recursive, Hedgehog
    "A facility for quotation covers the absence of original thought." /Gaudy Night/
    Chris Dollin, Aug 19, 2007
    #4
  5. On Sun, 19 Aug 2007 01:39:00 -0700, karthikbalaguru
    <> 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


    Have you actually tried to execute this program? Try step through it.
    You SHOULD get 332.

    Sounds like a homework question.
    The Real Andy, Aug 19, 2007
    #5
  6. "karthikbalaguru" <> wrote in message
    news:...
    > 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 ?
    >

    If you don't understand the logic, put in some diagnostics.

    void f(char a[])
    {
    printf("called with ***%s***\n", a);
    if(a[1]=='\0')
    {
    printf("Returning(1)\n");
    return;
    }
    f(a+1);
    printf("finished call one\n");
    f(a+1);
    printf("finished call two\n"):
    printf("%c", a[1]);
    printf("Returning(2)\n");
    }
    Malcolm McLean, Aug 19, 2007
    #6
  7. karthikbalaguru

    CBFalconer Guest

    karthikbalaguru wrote:
    >
    > 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 ?


    You've gotten answers. If you really want a recursive means of
    writing a string, try the following.

    #include <stdio.h>

    void putout(const char *s) {
    if (*s) {
    putchar(*s);
    putout(s + 1);
    }
    }

    int main(void) {
    putout("123");
    putchar('\n');
    return 0;
    }

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Aug 19, 2007
    #7
  8. karthikbalaguru

    Tim Wescott Guest

    On Sun, 19 Aug 2007 01:39:00 -0700, karthikbalaguru 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


    I suggest you code it up, compile it, link it, and try it out. Preferably
    with a debugger running so you can step through it.

    --
    Tim Wescott
    Control systems and communications consulting
    http://www.wescottdesign.com

    Need to learn how to apply control theory in your embedded system?
    "Applied Control Theory for Embedded Systems" by Tim Wescott
    Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
    Tim Wescott, Aug 19, 2007
    #8
  9. karthikbalaguru

    runner Guest

    "karthikbalaguru" <> wrote in message
    news:...
    > 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
    >


    The first column shows the depth. The depth is also
    shown by the indentation.

    1 a[1] => 2 f(123)
    2 a[1] => 3 f(23)
    3 a[1] => f(3)
    3 a[1] => f(3)
    2 a[1] => 3 print 3
    2 a[1] => 3 f(23)
    3 a[1] => f(3)
    3 a[1] => f(3)
    2 a[1] => 3 print 3
    1 a[1] => 2 print 2
    runner, Aug 19, 2007
    #9
  10. karthikbalaguru wrote:

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

    (snip)
    > It should actually return without printing anything.

    Bzzzttt!!!
    > How can one get '332' as output ?


    Even if you can't execute this in your head, try single-stepping with a
    source-level debugger.

    Or if you couldn't be bothered with that, don't hand your assignment in at
    all!

    Regards,

    --
    Mark McDougall, Engineer
    Virtual Logic Pty Ltd, <http://www.vl.com.au>
    21-25 King St, Rockdale, 2216
    Ph: +612-9599-3255 Fax: +612-9599-3266
    Mark McDougall, Aug 20, 2007
    #10
  11. karthikbalaguru

    IDDLife Guest

    On 8 19 , 4 39 , karthikbalaguru <>
    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".
    IDDLife, Aug 20, 2007
    #11
  12. karthikbalaguru

    IDDLife Guest

    On 8 19 , 4 39 , karthikbalaguru <>
    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".
    IDDLife, Aug 20, 2007
    #12
  13. karthikbalaguru

    elmerpdc Guest

    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")
    elmerpdc, Aug 20, 2007
    #13
  14. karthikbalaguru

    Dibyendu Guest

    On Aug 19, 1:39 pm, karthikbalaguru <>
    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.
    Dibyendu, Aug 20, 2007
    #14
  15. karthikbalaguru" <> wrote in message
    news:...
    > 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?
    Peter Dickerson, Aug 20, 2007
    #15
  16. karthikbalaguru

    Sjouke Burry Guest

    elmerpdc wrote:
    > just step through the code like below...best regards
    >

    Why????
    Sjouke Burry, Aug 20, 2007
    #16
  17. karthikbalaguru

    Ian Shef Guest

    karthikbalaguru <> wrote in
    news::

    > 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 *
    Ian Shef, Aug 21, 2007
    #17
  18. On Aug 20, 2:20 pm, Dibyendu <> wrote:
    > On Aug 19, 1:39 pm, karthikbalaguru <>
    > 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
    karthikbalaguru, Aug 21, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    9
    Views:
    5,092
  2. Mike James
    Replies:
    1
    Views:
    1,817
    Jim Gibson
    Sep 19, 2003
  3. Lanny McDonald
    Replies:
    1
    Views:
    333
    Alvin Bruney [MVP]
    Apr 3, 2004
  4. =?Utf-8?B?RGFuIE5hc2g=?=

    adding subnodes to a tree recursively

    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 16, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    659
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 18, 2004
  5. Dan Sikorsky
    Replies:
    1
    Views:
    1,812
    Dan Sikorsky
    Nov 4, 2004
Loading...

Share This Page