Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plz explain the programs output

6 views
Skip to first unread message

manish sahu

unread,
Feb 14, 2010, 7:45:05 AM2/14/10
to
#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

Malcolm McLean

unread,
Feb 14, 2010, 7:51:38 AM2/14/10
to
On Feb 14, 2:45 pm, manish sahu <manish.com...@gmail.com> 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.

manish sahu

unread,
Feb 14, 2010, 10:02:19 AM2/14/10
to
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

Ben Bacarisse

unread,
Feb 14, 2010, 10:18:34 AM2/14/10
to
manish sahu <manish...@gmail.com> writes:

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

unread,
Feb 14, 2010, 10:23:54 AM2/14/10
to
In article <52280889-dd5e-41c8...@t17g2000prg.googlegroups.com>,

manish sahu <manish...@gmail.com> 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.

Seebs

unread,
Feb 14, 2010, 11:57:20 AM2/14/10
to
On 2010-02-14, manish sahu <manish...@gmail.com> 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 / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Barry Schwarz

unread,
Feb 14, 2010, 12:36:10 PM2/14/10
to
On Sun, 14 Feb 2010 04:45:05 -0800 (PST), manish sahu
<manish...@gmail.com> wrote:


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

--
Remove del for email

manish sahu

unread,
Feb 15, 2010, 12:17:33 AM2/15/10
to
On Feb 14, 10:36 pm, Barry Schwarz <schwa...@dqel.com> wrote:
> On Sun, 14 Feb 2010 04:45:05 -0800 (PST), manish sahu
>
> <manish.com...@gmail.com> wrote:
>
> 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

Seebs

unread,
Feb 15, 2010, 1:27:33 AM2/15/10
to
On 2010-02-15, manish sahu <manish...@gmail.com> 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. :)

Richard Heathfield

unread,
Feb 15, 2010, 2:29:46 AM2/15/10
to
manish sahu wrote:
> ANY ONE CAN SEND ME GOOD RECURSION NOTES

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

Nick Keighley

unread,
Feb 15, 2010, 3:51:58 AM2/15/10
to
On 14 Feb, 12:45, manish sahu <manish.com...@gmail.com> 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

Richard

unread,
Feb 15, 2010, 8:50:39 AM2/15/10
to
Nick Keighley <nick_keigh...@hotmail.com> writes:

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

Richard Heathfield

unread,
Feb 15, 2010, 9:51:46 AM2/15/10
to
Nick Keighley wrote:
> On 14 Feb, 12:45, manish sahu <manish.com...@gmail.com> wrote:

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

0 new messages