Thread: returning random value.

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    What do you do with the result of the call on line 21?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why do you have a function declaration inside your main function?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    so what should i do??

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    What if you changed the function to
    int sumdig( int sum, int num );

    Try and figure that out.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    > so what should i do??
    Use the return result.

    In the recursive case, something like
    return a + sumdig(b);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    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?
    Last edited by bos1234; 09-10-2013 at 05:54 PM. Reason: Expanded on comments.

  10. #10
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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 .
    Last edited by coder1; 09-11-2013 at 12:17 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,730
    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.
    Last edited by laserlight; 09-11-2013 at 12:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,730
    Then what is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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);
    }

  15. #15
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. Replies: 1
    Last Post: 12-14-2002, 01:51 AM