plz explain the programs output

Discussion in 'C Programming' started by manish sahu, Feb 14, 2010.

  1. manish sahu

    manish sahu Guest

    #include<stdio.h>

    int f(int *a,int n)
    {
    if(n<=0)
    {
    return 0;
    }
    else if( *a%2==0)
    {
    return *a+f(a+1,n-1);
    }
    else
    {
    return *a-f(a+1,n-1);
    }


    }
    main()
    {
    int a[]={12,7,13,4,11,6};
    printf("\nsum is = %d ",f(a,6));
    return 0;
    }


    its out put is 15
    but how we are getting this output?
    plz explain me step by step
    thanks in advance
     
    manish sahu, Feb 14, 2010
    #1
    1. Advertising

  2. On Feb 14, 2:45 pm, manish sahu <> wrote:
    >
    > its out put is 15
    > but how we are getting this output?
    > plz explain me step by step
    > thanks in advance
    >

    f is a recursive fucntion, which is being called with the array a and
    the number of elements in the array, n. The function will operate on
    the array and, at some point, the recursion must terminate.
    You need to put diagnostic printfs in the code if you can't follow
    through the logic by eye.
     
    Malcolm McLean, Feb 14, 2010
    #2
    1. Advertising

  3. manish sahu

    manish sahu Guest

    What i am getting is:

    main()
    |
    f(base_add,6)
    |
    12 + f(next_add,5)
    |
    7 - f(next_add,4)
    |
    13 - f(next_add,3)
    |
    4 + f(next_add,2)
    |
    11 - f(next_add,1)
    |
    6 + f(next_add,0)
    |
    0

    finally the o/p should be 12 + 7 - 13 - 4 + 11 - 6
    =7
    but the o/p is 15 plz explin me
     
    manish sahu, Feb 14, 2010
    #3
  4. manish sahu <> writes:

    > What i am getting is:
    >
    > main()
    > |
    > f(base_add,6)
    > |
    > 12 + f(next_add,5)
    > |
    > 7 - f(next_add,4)
    > |
    > 13 - f(next_add,3)
    > |
    > 4 + f(next_add,2)
    > |
    > 11 - f(next_add,1)
    > |
    > 6 + f(next_add,0)
    > |
    > 0
    >
    > finally the o/p should be 12 + 7 - 13 - 4 + 11 - 6
    > =7
    > but the o/p is 15 plz explin me


    The expression is: 12 + (7 - (13 - (4 + (11 - 6)))) = 15. At each
    call the effect to add or subtract the result of the whole
    sub-expression.

    --
    Ben.
     
    Ben Bacarisse, Feb 14, 2010
    #4
  5. manish sahu

    Ike Naar Guest

    In article <>,
    manish sahu <> wrote:
    >What i am getting is:
    >
    > main()
    > |
    > f(base_add,6)
    > |
    > 12 + f(next_add,5)
    > |
    > 7 - f(next_add,4)
    > |
    > 13 - f(next_add,3)
    > |
    > 4 + f(next_add,2)
    > |
    > 11 - f(next_add,1)
    > |
    > 6 + f(next_add,0)
    > |
    > 0
    >
    > finally the o/p should be 12 + 7 - 13 - 4 + 11 - 6


    You should evaluate that from inside out (right to left)
    12 + (7 - (13 - (4 + (11 - 6)))) = 15

    >
    >but the o/p is 15 plz explin me


    Your output would be more readable if you avoided the silly abbreviations.
     
    Ike Naar, Feb 14, 2010
    #5
  6. manish sahu

    Seebs Guest

    On 2010-02-14, manish sahu <> wrote:
    > but how we are getting this output?


    I suggest that if your instructor really wishes to know, perhaps your
    instructor should post the question here directly.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Feb 14, 2010
    #6
  7. On Sun, 14 Feb 2010 04:45:05 -0800 (PST), manish sahu
    <> wrote:


    Are you going to ask this group to do everyone of your homework
    problems?

    --
    Remove del for email
     
    Barry Schwarz, Feb 14, 2010
    #7
  8. manish sahu

    Seebs Guest

    On 2010-02-15, manish sahu <> wrote:
    > YES, THANKYOU MY DEAR FRIENDS NOW I GOT THE POINT.


    But have you learned from it?

    > ANY ONE CAN SEND ME GOOD RECURSION NOTES


    Yes, you can. Once you've written them. :)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Feb 15, 2010
    #8
  9. On 14 Feb, 12:45, manish sahu <> wrote:
    > #include<stdio.h>
    >
    > int f(int *a,int n)
    > {


    add this line
    printf ("call f(%d, %d)\n", *a, n);
    and run the program again


    >         if(n<=0)
    >          {
    >                 return 0;
    >         }
    >         else if( *a%2==0)
    >                 {
    >                         return *a+f(a+1,n-1);
    >                 }
    >         else
    >         {
    >               return *a-f(a+1,n-1);
    >         }


    > }
    >
    > main()


    better is
    int main (void)

    > {
    >         int a[]={12,7,13,4,11,6};
    >         printf("\nsum is = %d   ",f(a,6));
    >         return 0;
    >
    > }
    >
    > its out put is 15
    > but how we are getting this output?
    > plz explain me step by step
    > thanks in advance


    learn to be more polite when begging for help
     
    Nick Keighley, Feb 15, 2010
    #9
    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. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    443
    -berlin.de
    Nov 25, 2004
  2. Replies:
    1
    Views:
    415
  3. Replies:
    2
    Views:
    367
    Nick Keighley
    Nov 24, 2006
  4. Replies:
    1
    Views:
    383
  5. nocturnal
    Replies:
    1
    Views:
    560
    nocturnal
    Jul 10, 2009
Loading...

Share This Page