Thread: I could learn a thing or to about Recursion

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    I could learn a thing or to about Recursion

    I've been tasked with making a program that prints the astri "*" character in a certain pattern. It's been a little difficult for me because I don't quite understand Recursion in Cpp.

    This is what my function looks like. What I was hoping for was "lines" to eventually reach 0 actiavating my else if statement but my mind set was completely wrong. Next I decided a better idea would be to activate an else if statement when lines = i. The problem is lines always equal i. I'm not asking for the anwser but I will ask anyone to explain my code in great detail so I can try better ideas.

    What I'm trying to do is to get my code to print this
    *
    **
    ***
    ****
    ****
    ***
    **
    *
    Function
    Code:
    void printStars(int lines)
    {
     int i;
     
     if (lines > 0)
     {
     printStars(lines - 1);
      
     for (i = 0; i < lines; i++)
      cout << "*"<<lines;  
     cout<< endl<< i;
     }
    }



  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    One way to implement this is to use two parameters, one for the current number of stars, one for the maximum number of stars. Only the current number of stars would change during the recursive calls.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I would also advise that it is a great practice to write a recursive version of your code first and then implement it as a non-recursive version meaning you use a for or while loop instead. I've been burned in the past by code suddenly failing to work for sufficiently deep recursive calls which happens more than I'd have thought.

  4. #4
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Part 2 of 3 is complete. I don't feel good about this code, but I couldn't spend anymore time on this. Thanks rcgldr I got the idea reading your post.

    Code:
    //Chapter 6: Programming Exercise 1
    #include<iostream>
    usingnamespace std;
    void printStars(int lines);
    void printStars2(int lines);
    int main()
    {
    int lines;
    cout << "Enter the number of lines in the grid: " << flush;
    cin >> lines;
    cout << endl;
    printStars(lines);
    printStars2(lines);
    cout << endl;
    system ("pause");
    return 0;
    }
    void printStars(int lines)
    {
    int i;
    if (lines > 0)
    {
    printStars(lines - 1);
    for (i = 0; i < lines; i++)
    cout << "*"; 
    cout<< endl;
    }
    }
    void printStars2(int lines)
    {
    int i;
    if (lines > 0)
    { 
    for (i = 0; i < lines; i++)
    cout << "*"; 
    cout<< endl;
    printStars2(lines - 1);
    }
    }
    Last edited by LAURENT*; 11-19-2013 at 03:44 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    This is what I meant by using two parameters with a single recursive function:

    Code:
    #include<iostream>
    
    using namespace std;
    
    void printStars(int stars, int maxstars);
    
    int main()
    {
    int lines;
        cout << "Enter the number of lines in the grid: " << flush;
        cin >> lines;
        cout << endl;
        printStars(1, lines);
        cout << endl;
        system ("pause");
        return 0;
    }
    
    void printStars(int stars, int maxstars)
    {
        if(stars > maxstars)
            return;
        for(int i = 0; i < stars; i++)
            cout << "*";
        cout << endl;
        printStars(stars+1, maxstars);
        for(int i = 0; i < stars; i++)
            cout << "*";
        cout << endl;
    }
    Last edited by rcgldr; 11-19-2013 at 05:28 PM.

  6. #6
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Quote Originally Posted by rcgldr View Post
    This is what I meant by using two parameters with a single recursive function:

    Code:
    #include<iostream>
    
    using namespace std;
    
    void printStars(int stars, int maxstars);
    
    int main()
    {
    int lines;
        cout << "Enter the number of lines in the grid: " << flush;
        cin >> lines;
        cout << endl;
        printStars(1, lines);
        cout << endl;
        system ("pause");
        return 0;
    }
    
    void printStars(int stars, int maxstars)
    {
        if(stars > maxstars)
            return;
        for(int i = 0; i < stars; i++)
            cout << "*";
        cout << endl;
        printStars(stars+1, maxstars);
        for(int i = 0; i < stars; i++)
            cout << "*";
        cout << endl;
    }
    There are a few things I don't get.

    *could explain how a void function can have the return statement within it?

    *What about the parameter on the function within the main function?

    *printStars(stars+1, maxstars); I just don't get this statement.

    BTW I love how neat your code looks. I want to follow this style.
    Last edited by LAURENT*; 11-21-2013 at 09:09 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LAURENT*
    *could explain how a void function can have the return statement within it?
    The return statement does not return a value, so that's fine. It becomes just a way to pass control back to the caller.

    Quote Originally Posted by LAURENT*
    *What about the parameter on the function within the main function?
    What do you find confusing about this? It is just a function call.

    Quote Originally Posted by LAURENT*
    *printStars(stars+1, maxstars); I just don't get this statement.
    What do you find confusing about this? It is just a recursive function call.
    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

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by LAURENT* View Post
    *What about the parameter on the function within the main function?
    *printStars(stars+1, maxstars); I just don't get this statement.
    In this case, the parameters passed to printStars() are "passed by value", they are copies of the variables, and for each nested recursive call, typically an instance of each of these values is stored on the stack for each level of recursion (The values may be passed via registers or pushed onto the stack. If passed via registers, those registers are typically pushed onto the stack before making a recursive (or any) call).

    The call to printStars() from main is printStars(1, lines), then printStars() calls itself with the values (2,lines), which in turn calls itself with the values (3,line), until the first parameter (stars, ...) is greater than the second parameter (... , maxstars). Before making a recursive call, the stars are printed out, which results in the lines of output with increasing number of stars, then as those calls return back up the nested call chain, the lines are printed again, which results in the lines of output with decreasing number of stars.

    If you have a source level debugger, you can step through the code to see how this works.
    Last edited by rcgldr; 11-21-2013 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If it's not one thing...
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-20-2006, 11:39 AM
  2. You have to learn C in order to learn C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-16-2004, 10:33 AM
  3. WEb thing!
    By Jenny Tey in forum Tech Board
    Replies: 18
    Last Post: 05-23-2003, 08:38 AM
  4. What's The Most Important Thing To Learn In C++?
    By DeanDemon in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2002, 06:49 PM
  5. One more thing
    By aba_abstract2k in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2002, 10:57 PM