Velocity Reviews > plz explain the programs output

# plz explain the programs output

manish sahu
Guest
Posts: n/a

 02-14-2010
#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

Malcolm McLean
Guest
Posts: n/a

 02-14-2010
On Feb 14, 2:45*pm, manish sahu <(E-Mail Removed)> wrote:
>
> its out put is 15
> but how we are getting this output?
> plz explain me step by step
>

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.

manish sahu
Guest
Posts: n/a

 02-14-2010
What i am getting is:

main()
|
|
|
|
|
|
|
|
0

finally the o/p should be 12 + 7 - 13 - 4 + 11 - 6
=7
but the o/p is 15 plz explin me

Ben Bacarisse
Guest
Posts: n/a

 02-14-2010
manish sahu <(E-Mail Removed)> writes:

> What i am getting is:
>
> main()
> |
> |
> |
> |
> |
> |
> |
> |
> 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.

Ike Naar
Guest
Posts: n/a

 02-14-2010
In article <(E-Mail Removed)>,
manish sahu <(E-Mail Removed)> wrote:
>What i am getting is:
>
> main()
> |
> |
> |
> |
> |
> |
> |
> |
> 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.

Seebs
Guest
Posts: n/a

 02-14-2010
On 2010-02-14, manish sahu <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Barry Schwarz
Guest
Posts: n/a

 02-14-2010
On Sun, 14 Feb 2010 04:45:05 -0800 (PST), manish sahu
<(E-Mail Removed)> wrote:

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

--
Remove del for email

Seebs
Guest
Posts: n/a

 02-15-2010
On 2010-02-15, manish sahu <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Nick Keighley
Guest
Posts: n/a

 02-15-2010
On 14 Feb, 12:45, manish sahu <(E-Mail Removed)> wrote:
> #include<stdio.h>
>
> int f(int *a,int n)
> {

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

learn to be more polite when begging for help