Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; Code: void countup(int num) { if (num >= 0) { countup(num - 1); cout << num << " "; } ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Recursion

    Code:
    void
    countup(int num)
    {
        if (num >= 0)
        {
            countup(num - 1);
            cout << num << " ";
        } else cout << endl;
    }
    Here is a recursive function that out puts numbers in an ascending order. I'm supposed to add all the numbers up in a new function below:
    Code:
    int
    sumdown(int num)
    {
        int sum = 0;
        if (num >= 0)
        {
            
          sum+=sumdown(num - 1);
         
        } else cout << endl;
    return(sum);
    }
    However, this doesn't work. Please help me.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    sum = num + sumdown(num-1)

    but the condition should be num > 0
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Wow, you're fast. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

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