plz explain the programs output

M

manish sahu

#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
 
M

Malcolm McLean

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.
 
M

manish sahu

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
 
B

Ben Bacarisse

manish sahu said:
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.
 
I

Ike Naar

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.
 
S

Seebs

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
 
B

Barry Schwarz

On Sun, 14 Feb 2010 04:45:05 -0800 (PST), manish sahu


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

Nick Keighley

#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);
        }

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top