indenting using recursion.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yaoc1987
    New Member
    • Mar 2010
    • 8

    indenting using recursion.

    is this even possible with recursion?

    ive only done this with loops but i cant do it with recursion

    anyone can help think of algorithm or a short code for this???

    thanks!
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Recursively calling a function is just calling it from within the function itself. If a recursive function incorporated '\t' in its body such as:
    cout<<"\t this is an indented sentence"<<endl ; //the sentence would be indented.
    Try and write some code round this idea and show it to us with an explanation of where you are having trouble.

    Comment

    • yaoc1987
      New Member
      • Mar 2010
      • 8

      #3
      ok but how can i put more indents(/t) in front of the sentence???

      ie:
      sentence 1st call
      ...sentence 2nd call
      ......sentence 3rd call
      and so on then it goes back the other way
      ......sentence 1st call
      ...sentence 2nd call
      sentence 3rd call

      i dont know if im explaining this properly

      its sort of like the triangle with * but i dont know how to start this using /t

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        You are right.......... your explanation of what you are trying to do is almost non existent. Read the top thread in this forum on how to ask a question. Also tell us what this assignment is about and what the objectives are.

        Comment

        • yaoc1987
          New Member
          • Mar 2010
          • 8

          #5
          non-existent?? wow

          here is my code can u check it out tho

          int rec_fun::indent ed_sentences(si ze_t m, size_t n)
          {
          char sent[] = "this sentence!";

          assert(m <= n);

          int i = 0;

          if(m >=n)
          {
          for(i = 0; i < m; i++)
          cout << " ";
          cout << sent << endl;
          return 0;
          }
          for(i = 0; i < m; i++)
          cout << " ";
          cout << sent << endl;
          triangle(/*outs, */m+1,n);
          for(i = 0; i < m; i++)
          cout << " ";
          cout << sent << endl;
          return 0;
          }
          actual output:

          this sentence!
          ...this sentence!
          ......this sentence!
          .........this sentence!
          ......this sentence!
          ...this sentence!
          this sentence!

          needed output:

          this sentence!
          ...this sentence!
          ......this sentence!
          .........this sentence!
          .........this sentence! <- missing this extra one in the middle
          ......this sentence!
          ...this sentence!
          this sentence!

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            That is using loops not recursion, recursion is where a function calls itself, and a loop is where you use a for, while or do while loop.

            Generally for what you want to do you will not want to print the sentence on each iteration. You will want the iterations to output your tab (or space) characters and then on the last iteration output the sentence as well (or instead).

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              There's a couple ways to do this. Looking at your code I would probably store the iterations and the current indent:
              Code:
              function(i, pre){
                if i is 0 end;
                  print pre + sentence;
                   call function(i - 1, pre + \t)
                  print pre + sentence;
                }
              And originally call the function like function(10, "")

              Note that this will give you the 2 in the middle like you are asking for:
              this sentence!
              this sentence!
              this sentence!
              this sentence!
              this sentence!
              this sentence!
              this sentence!
              this sentence!
              (just felt like trying out indent tags)

              Comment

              Working...