Thread: recursion problem

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    recursion problem

    I need to write a program whose output looks like:

    Code:
    This line was output by call 1.
     This line was output by call 2.
      This line was output by call 3.
       This line was output by call 4.
       This line was ALSO output by call 4.
      This line was ALSO output by call 3.
     This line was ALSO output by call 2.
    This line was ALSO output by call 1.
    I need to use a recursive function that takes in the the recursion depth from the user. I can't figure out how to keep track of the number of lines I print out, or how to indent the lines?

    Any ideas are appreciated.

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What do you mean 'indent the lines'?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    In addition to the other requirements, each line must be indented one space, increasingly and decreasingly. It didn't show up in the post that way.

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It does now

    Code:
    void indent ( int level ) {
      for ( int i = 0 ; i < level ; i++ ) cout << " ";
    }
    
    void recurse ( int level ) {
      indent( level );
      recurse( level + 1 );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    How does the function decrease the number of spaces indented?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The number of spaces written to the screen is proportional to the parameter 'level'. Thus, the higher 'level' is, the greater the indent is.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ that explains an increase, not a decrease...

    This line was output by call 1.
    This line was ALSO output by call 1.

    by this, do you mean that in call one, the first and last line are output, and in call two, lines 2 and 7 are output?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    I don't think it would be possible to print out the first and last lines, then lines in between....would it? The assignment is vague and only gives the output I originally posted and that a recursive function must be used.

    I thought that I could multiply the recursion depth value by -1 and then set up an if statement using > or < 0 to output the correct sentence, but I can't figure out how to stop it. It ends up being an endless loop.

    I'm spent.

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    void indent ( int level )
    {
      for ( int i = 0 ; i < level ; i++ ) cout << " ";
    }
    
    void recurse ( int targetLevel, int level = 0, bool onTheWayBackDown = false)
    {
      indent( level );
      if(level == targetLevel)
        onTheWayBackDown = true;
    
      if(!onTheWayBackDown)
      {
        cout << "This line was output by call " << level + 1 << "\n";
        recurse(targetLevel, level + 1, onTheWayBackDown);
      }
      else
      {
        if(level == 0)
          return;
        cout << "This line was ALSO output by call " << level + 1 << "\n";
        recurse(targetLevel, level - 1, onTheWayBackDown);
      }
    }
    I haven't tested it, but I believe that should be pretty close to something that works.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This doesn't work?
    Code:
    void recurse ( int level ) {
      if ( level > 5 ) return;
      indent( level );
      cout << "foo";
      recurse( level + 1 );
      indent( level );
      cout << "bar";
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    I like the words it outputs, Salem. Quite appropriate. (poor stack... eventually - talk about nickel and diming)

    I like the simplicity, but it needs just one more addition to make it sane (and stack friendly...)

    Code:
    #include <iostream>
    using namespace std;
    
    void indent(int x) {
      for(int i = 0; i < x; ++i)
        cout << " ";
    }
    
    // x = number of times to recurse (number of calls)
    void recurse(int x, int call = 1) {
    
      // have we made enough calls yet?
      if (call > x)
        return; // stop recursing
    
      indent(call - 1);
      cout << "This line was output by call " << call << endl;
      recurse(x, call + 1);
      indent(call - 1);
      cout << "This line was ALSO output by call " << call << endl;
    
      return; // done with current iteration
    }
    
    int main() {
    
      cout << "Calling recurse(4)..." << endl
           << endl;
      recurse(4);
      cout << endl
           << "Calling recurse(7)..." << endl
           << endl;
      recurse(7);
    
    }
    Ahh, the joys of recursion, there are too many to count (until you hit your bottom case).

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah right, I forgot that it can do stuff after calling itself
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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, 09:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM