Like Tree2Likes

iterative vs. recursive

This is a discussion on iterative vs. recursive within the C Programming forums, part of the General Programming Boards category; I have this program that calculates the sum of the digits in a number but I have to make it ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    iterative vs. recursive

    I have this program that calculates the sum of the digits in a number but I have to make it recursive and i have no idea how a recursive function would even work for this kind of problem.
    Where would I start?


    Code:
            int digit = 0; 
    	int sum = 0;
    	int number;
    	
    	printf("Enter a number:\n");
    	scanf("%d", &number);
    	
    	while(number != 0)
    	{
    		digit = number % 10;
    		
    		sum = sum + digit;
    		
    		number = number / 10;
    	}
    	printf("The sum of the digits is:\n%d", sum);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Recursion just needs to know when to stop. What tells your loop to stop? That's how you know to stop recusion. So, you are looking to pass it a number, and to stop when that number is zero. Since you are doing something with each number, you probably need to be returning something, and to be handling that something as well.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Do you understand recursive functions at all? If not take a look here, and Google around for some more tutorials. Work some simpler examples (factorial is a classic).

    Take a stab at it and post your code, and we'll help you (sorry, no free code here). To get you started, your loop condition (when to keep looping) is often the opposite of your base case (when to stop recursing). That is, if you keep looping while number is non-zero, than your base case should be number == 0. What comes after the base case should be similar to what's inside your loop.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Maybe something like:
    Code:
    int recursive_func(int num)
    {
      int sum;
    
      assign last digit of num to sum
      if num has more than one digit
        pass all but the last digit of num to recursive_func() and add the return value to sum
      return sum
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Recursion just needs to know when to stop. What tells your loop to stop? That's how you know to stop recusion. So, you are looking to pass it a number, and to stop when that number is zero. Since you are doing something with each number, you probably need to be returning something, and to be handling that something as well.


    Quzah.
    Well, there is also the matter that a recursive function calls itself.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Well, there is also the matter that a recursive function calls itself.
    I assumed that since he was being instructed to "make it recursive" that they told him what recursion was.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    I don't know much about recursive functions at all, but so far i have this. Is this anything close to what i should have?

    Code:
    int sum(int x, int y)
    {
    	if (number=0) 
    	{
    		return 0;
    	}
    	else
    		return (sum(sum, digit));

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    = is for assignment, == is for equality. Look at what everyone here has said so far. Pay attention to what itsme said.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laurenlb View Post
    I don't know much about recursive functions at all, but so far i have this. Is this anything close to what i should have?

    Code:
    int sum(int x, int y)
    {
    	if (number=0) 
    	{
    		return 0;
    	}
    	else
    		return (sum(sum, digit));
    Which number are you referring to?
    If you intend to test it's value you need == .. single = is for assignment

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Quote Originally Posted by laurenlb View Post
    I don't know much about recursive functions at all, but so far i have this. Is this anything close to what i should have?

    Code:
    int sum(int x, int y)
    {
    	if (number=0) 
    	{
    		return 0;
    	}
    	else
    		return (sum(sum, digit));
    You're not summing anything. There should at least be a + sign somewhere in there. But yea, you're on the right track.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    if(number=0) // don't. Instead do,
    if(number==0) //only if you want to compare
    It's recursive but will not exactly do what you want it to do.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by laurenlb View Post
    I don't know much about recursive functions at all, but so far i have this. Is this anything close to what i should have?

    Code:
    		return (sum(sum, digit));
    You also have a problem here. You shouldn't be passing sum as a parameter to sum. sum is a function, you need to pass an integer for both parameters.

    You have the right base case (assuming you switch = to == as many have already pointed out. Now, you need to use the body of your while loop as the "guts" of your recursion. There will be a little tweaking involved, but you're on the right track.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I assumed that since he was being instructed to "make it recursive" that they told him what recursion was.


    Quzah.
    And do you still think that?

  14. #14
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by quzah View Post
    I assumed that since he was being instructed to "make it recursive" that they told him what recursion was.


    Quzah.
    Lol :-)


    Really?
    If he/she was taught and he/she understood it well, then i think he/she shouldn't even write such a code. If he/she was told to call a function by itself, he/she should also be told about some ending condition. Ain't it?
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to learn to read.
    but I have to make it recursive and i have no idea how a recursive function would even work for this kind of problem.
    He didn't say he didn't understand recursion, or that he didn't know what recursion meant. He said he didn't know how to apply it to this problem. That's why I gave him the benefit of the doubt that he knew what it was. Besides, I already had answered the = and == issue before you two comedians showed up.

    Learn to read. That goes for the OP too. You've already been hand fed the answer, and told that you've been hand fed the answer.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary tree but iterative, not recursive!
    By budala in forum C Programming
    Replies: 2
    Last Post: 08-06-2009, 12:55 PM
  2. Using both a Recursive and Iterative Functions
    By belkins in forum C Programming
    Replies: 12
    Last Post: 11-02-2008, 11:31 AM
  3. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 04:51 PM
  4. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21