Thread: String (?) Recursion

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Unhappy String (?) Recursion

    Hey all,
    I have an assignment for my beginning C++ class where my teacher wants us to use recursion to display ten stars ( ********** ) n times vertically. such that if n = 4 it would look like:

    **********
    **********
    **********
    **********

    now normally I would use a void function that just does:

    Code:
    void rectangle(int n){
       if (n >= 1) cout << "**********";
       rectangle (n-1);
    }
    but my teacher would like us to cout the return of ten stars. How is that supposed to look?

    I tried:

    Code:
    #include <iostream>
    using namespace std;
    
    string rectangle(int n){
    	if (n >= 1) return "**********";
    	rectangle(n-1);
    }
    
    int main(){
    	int n;
    	cout << "Please enter an n value: ";
    	cin >> n;
    	cout << rectangle(n);
    	return 0;
    }
    but it doesnt seem to be working.

    Thanks for any and all help.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see where you are using the return value of the call to rectangle inside rectangle. Perhaps you should be adding them all together?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void rectangle(int n){
       if (n >= 1) cout << "**********";
       rectangle (n-1);
    }
    That's an infinitely recursive function. You only want to recurse while n is greater than 0, currently you always recurse so the call to rectangle(0) will not print the stars but it will call rectangle(-1)... which calls rectangle(-2)... which calls rectangle(-3)... etc... Eventually it reaches the point where it calls rectangle(-2147483648) (assuming 4 byte ints and assuming you haven't blown the stack at that point causing the program to crash) and then switches from a negative to a positive value and you'll start printing 2+ billion lines of stars and the cycle just keeps repeating.

    A quick fix is to add in some braces to help control when you'll recurse.
    Code:
    void rectangle(int n){
        if (n >= 1)
        {
            cout << "**********\n";
            rectangle (n-1);
        }
    }
    [edit]Just wanted to make sure some newb that thought to try this out would not wonder what's going wrong. This doesn't really address the question you're asking.[/edit]
    Last edited by hk_mp5kpdw; 11-20-2008 at 01:41 PM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    void rectangle(int n){
       if (n >= 1) cout << "**********";
       rectangle (n-1);
    }
    That's an infinitely recursive function. You only want to recurse while n is greater than 0, currently you always recurse so the call to rectangle(0) will not print the stars but it will call rectangle(-1)... which calls rectangle(-2)... which calls rectangle(-3)... etc... Eventually it reaches the point where it calls rectangle(-2147483648) (assuming 4 byte ints and assuming you haven't blown the stack at that point causing the program to crash) and then switches from a negative to a positive value and you'll start printing 2+ billion lines of stars and the cycle just keeps repeating.

    A quick fix is to add in some braces to help control when you'll recurse.
    Code:
    void rectangle(int n){
        if (n >= 1)
        {
            cout << "**********\n";
            rectangle (n-1);
        }
    }
    [edit]Just wanted to make sure some newb that thought to try this out would not wonder what's going wrong. This doesn't really address the question you're asking.[/edit]
    Code:
    void rectangle(int n){
        if (n >= 1)
        {
            cout << "**********\n";
        }
         rectangle (n-1);
       
    }
    I was doing that.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    I think what was pointed out is that you should try and take a look at the logic of your if-statement and the placement of the call to rectangle within the rectangle function. And see why you will get infinite recursion... Because you're not doing the same in your code as hk_mp5kpdw showed in his code.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's no infinite recursion in the current attempt (the one with infinite recursion is the earlier attempt).

    One problem is that the base case is wrong - it calls the function recursively and the rest of the cases return. It should be the other way around, the function should recurse except in the base case.

    Also each string being returned is ignored except the last one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM