Thread: Jumping into C++ Recursion Example Question

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    7

    Jumping into C++ Recursion Example Question

    Will someone please explain the second half of this code sequence: 123456789987654321

    I understand the first half (123456789...) but at num=9, it kicks out of the If condition, cout's one last time and then return from the function ... How does the sequence decrease?

    Code:
    
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    void printNum (int num)
    {
                                    // the two calls in this function to cout will sandwich an inner
                                    // sequence containing the numbers (num+1)...99...(num+1)
    cout << num;
                                    // While begin is less than 9, we need to recursively print
                                    // the sequence for (num+1) ... 99 ... (num+1)
    if ( num < 9 )
        {
        printNum( num + 1 );
    
    
        }
    cout << num;
    
    
    }
    
    
    
    
    int main ()
    {
    printNum( 1 );
    }

  2. #2
    Guest
    Guest
    The reason is that each invocation of printNum will print out its own value after the if statement.

    Code:
    printnum(1) prints "1", calls printnum(1 + 1), does not call cout after the if yet.
      printnum(2) prints "2", calls …
         (…)
             printnum(8) "8"
               printnum(9) "9", if (n < 9) == false, so recursion stops. Prints another "9" at the end of its body, returns to printnum(8) by which it was invoked.
             printnum(8) "8" at the end of its body.
           printnum(7) "7" at the end of its body.
         …etc.
    p.s. In the future, please tidy up your code a bit for others to read, e.g.:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void printNum(int num)
    {
        cout << num; // 1, 2, 3 … 9
    
        if (num < 9)
        {
            printNum(num + 1);
        }
    
        cout << num; // 9, 8, 7 … 1
    }
    
    int main()
    {
        printNum(1);
    }
    This also makes it more sensible to refer to line numbers when discussing code.
    Last edited by Guest; 08-20-2017 at 06:16 PM.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    7
    Aaaah, OK. That helps.

    In the future, I will tidy up my code. Not a problem.

    Thanks for the help Adrian!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Jumping into C++] Chapter 7 Problem 1 Question
    By LordAbolition in forum C++ Programming
    Replies: 16
    Last Post: 07-26-2017, 10:05 AM
  2. Jumping in C++ Chapter 5 Question 2
    By etricity in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2014, 02:18 AM
  3. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  4. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM
  5. Replies: 8
    Last Post: 10-19-2013, 10:42 AM

Tags for this Thread