-
returning random value.
should return 15 but returning random big value.
here is the code :
Code:
#include <stdio.h>
int main(void)
{
int a;
int sumdig(int num);
a = sumdig(12345);
printf("\n%d",a);
}
int sumdig(int num)
{
static int sum;
int a,b;
a = num%10;
b = (num-a)/10;
sum = sum +a;
if (b!=0)
sumdig(b);
else
return(sum);
}
-
What do you do with the result of the call on line 21?
-
It's a recursive function so value of b goes as num value in sumdig(int num) and the process will start again till b becomes 0.
-
I can see that.
Code:
$ gcc -Wall bar.c
bar.c: In function ‘sumdig’:
bar.c:24:3: warning: control reaches end of non-void function [-Wreturn-type]
bar.c: In function ‘main’:
bar.c:10:2: warning: control reaches end of non-void function [-Wreturn-type]
$ ./a.out
15$
It works OK for one call.
But if you keep calling it, you just get the accumulated static value stored in your result from last time.
sum is 0 ONLY on the first ever use of this function.
-
why do you have a function declaration inside your main function?
-
-
What if you changed the function to
int sumdig( int sum, int num );
Try and figure that out.
-
> so what should i do??
Use the return result.
In the recursive case, something like
return a + sumdig(b);
-
Hi,
I moved the function declaration outside the main as suggested by Elkvis. Seems to be running fine. As suggested by Salem, I think its only working because you declared sum as static int. So sum is given the value 15 and then is locked to that value.
Code:
#include <stdio.h>
int sumdig(int num);
int main(void)
{
int a;
a = sumdig(12345); //line BBB
printf("\n%d",a);
getchar();
return 0;
}
int sumdig(int num)
{
static int sum;
int a,b;
a = num%10;
b = (num-a)/10;
sum = sum +a;
if (b!=0)
sumdig(b); //line AAA
else
return(sum);
}
Also, is this how the instructions are stored in stack (high level view)?
http://i40.tinypic.com/2qc45eh.png
Starting from the bottom of the stack:
1. sumdig function called. Value is 12345 and the line to be returned is BBB. Next set of instructions are stored.
2. sumdig function called. Value is 1234 and the line to be returned is aaa. Next set of instructions are stored.
etc
I am having a little trouble once the stack begins to unwind. return sum where sum is 15. This value of 15 goes where? sumdig(b) = 15?
-
1. I copy pasted your code and ran it but its still giving me random value i.e 4199558 as answer.
2. yes this is how instructions are stored in stack but in the third step value of num will be 123 not 1233.Also sum wil not be 15 .(see 3rd point)
3.IN the first stwep default value of sum is 0 and the value of "a" is added (which is num%10 = 5) which makes sum = 5.this value of sum(i.e 5) is stored in the variable "sum".
Now in next step num is 1234 and a becomes num%10 =4, which is added to variable "sum " making sum =5+4 = 9.In this way it goes on and we get sum = 5+4+3+2+1.
Now when num becomes 1 ,a = num%10.a = 1 and b becomes 0.Here recursive call ends and we return "sum" as a return value which prints out as answer.
But the program is not working accordingly i cant find out whats wrong.i know the whole procedure but don't know what the problem it is there that it is giving random value as answer .
-
What is your current code?
I strongly suggest that you get rid of the static local variable. Although the variable has local scope, it has global state that makes it harder to reason about the code, especially where recursion is involved. Rather, think recursively: to sum the digits of 12345, imagine that you have another function that will sum the digits of 1234. Then, other than the base case, all you have to do is extract the 5, then add it to the result of calling that other function, and return the result. It so happens that that other function is the very function that you are trying to write.
-
i know i can do that the other way but in order to find what is wrong with this program first i have to run the code .I cant point out any error in this program ,so why this method is not working out?.(I just want to improve my concept that is why i am asking)
-
Then what is your current code?
-
Code:
#include <stdio.h>
int main( )
{
int a;
a = sumdig(12345);
printf ( "\n%d",a);
return 0;
}
sumdig ( int num )
{
static int sum;
int a, b;
a = num%10;
b = (num-a)/10;
sum = sum+a;
if (b!=0)
sumdig(b);
else
return(sum);
}
-
static should not cause a problem because without it the whole program will not work as sum will always be initialized each time with the value we give and will not work as expected by the program