query in reccursion

Discussion in 'C Programming' started by bnslpayal@gmail.com, Dec 11, 2007.

  1. Guest

    int giValue = 5; /* Global Variable Declaration */
    void fnReverse()
    {
    if (giValue > 0) {
    giValue--;
    fnReverse();
    }
    printf("%d\n",giValue);
    }

    /
    ************************************************************************************************************
    * why the answer is 0 six times????????????????

    **********************************************************************************************************/
    , Dec 11, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > int giValue = 5; /* Global Variable Declaration */
    > void fnReverse()
    > {
    > if (giValue > 0) {
    > giValue--;
    > fnReverse();
    > }
    > printf("%d\n",giValue);
    > }
    >
    > /
    > ************************************************************************************************************
    > * why the answer is 0 six times????????????????
    >
    > **********************************************************************************************************/


    "Six times" because the values 5,4,3,2,1,0 are a set
    of cardinality six.

    "0" because giValue is equal to zero at each of the
    six executions of the printf call.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 11, 2007
    #2
    1. Advertising

  3. wrote:
    > int giValue = 5; /* Global Variable Declaration */
    > void fnReverse()
    > {
    > if (giValue > 0) {
    > giValue--;
    > fnReverse();
    > }
    > printf("%d\n",giValue);
    > }
    >
    > /
    > ************************************************************************************************************
    > * why the answer is 0 six times????????????????
    >
    > **********************************************************************************************************/


    Because you hit the printf statement 6 times for this loop. Consider this, if
    giValue was initialized to zero you would still print it's value once.
    Golden California Girls, Dec 11, 2007
    #3
  4. Chris Dollin Guest

    wrote:

    > int giValue = 5; /* Global Variable Declaration */
    > void fnReverse()
    > {
    > if (giValue > 0) {
    > giValue--;
    > fnReverse();
    > }
    > printf("%d\n",giValue);
    > }
    >
    > /
    > ************************************************************************************************************
    > * why the answer is 0 six times????????????????
    >
    > **********************************************************************************************************/


    Because you print the value of `giValue` after the recursion has been
    all the way down to the bottom.

    As would have been obvious if you'd tried "play machine" or attempted the
    induction -- two techniques that I'd advise adding to your kit.

    --
    Chris "once a formalist, always (but not only) a formalist" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, Dec 11, 2007
    #4
    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. Learner
    Replies:
    1
    Views:
    976
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,445
    Anonymous
    Oct 13, 2005
  3. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    781
    David Gordon
    May 18, 2005
  4. Eric Nelson
    Replies:
    5
    Views:
    1,510
    Alexey Smirnov
    Feb 4, 2009
  5. Jon F.

    CAML Query: Multiple Query Fields Issue

    Jon F., May 12, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    744
    Jon F.
    May 12, 2004
Loading...

Share This Page