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
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.
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.
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 / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
Are you going to ask this group to do everyone of your homework
problems?
--
Remove del for email
YES, THANKYOU MY DEAR FRIENDS NOW I GOT THE POINT.
ANY ONE CAN SEND ME GOOD RECURSION NOTES
But have you learned from it?
> ANY ONE CAN SEND ME GOOD RECURSION NOTES
Yes, you can. Once you've written them. :)
If there is anyone who is standing closer to Doug Hofstadter than I am,
could they please send "manish sahu" good recursion notes?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
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
And learn to accept that not everyone has English as a first
language. His request was perfectly polite.
--
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c
<snip>
>> 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
The above looks to me to be a fairly sincere effort to be polite. Okay,
so he's using silly abbrevs and not terribly good grammar. How are your
Urdu classes coming along?