473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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!
Apr 7 '10 #1
6 5543
whodgson
542 512MB
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.
Apr 7 '10 #2
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
Apr 7 '10 #3
whodgson
542 512MB
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.
Apr 7 '10 #4
non-existent?? wow

here is my code can u check it out tho

int rec_fun::indented_sentences(size_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!
Apr 7 '10 #5
Banfa
9,065 Expert Mod 8TB
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).
Apr 7 '10 #6
jkmyoung
2,057 Expert 2GB
There's a couple ways to do this. Looking at your code I would probably store the iterations and the current indent:
Expand|Select|Wrap|Line Numbers
  1. function(i, pre){
  2.   if i is 0 end;
  3.     print pre + sentence;
  4.      call function(i - 1, pre + \t)
  5.     print pre + sentence;
  6.   }
  7.  
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)
Apr 7 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
2
by: RobG | last post by:
Some posters have been critical of the lack of formatting in some posts. Pleae be aware that posting through the Google group's web interface removes *all* leading white space - tabs, spaces, the...
9
by: bob | last post by:
What tool do most of you people use to indent your code? Doing so by hand is a bit tedious, I think.
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Leon Friesema | last post by:
Hiyall! I have a RichTextBox inside a UserControl which should be able to indent a block off text (or UnIndent for that matter) like the SourceEditor of VS2003 or WinWord does. All controls on...
7
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped...
0
by: D. Shane Fowlkes | last post by:
The Smart Indenting "feature" is driving me absolutlely crazy in VWD. Problem: VWD in Code View insists on tabbing a lot of my code over to it's own liking and effects mainly my comments in the...
4
by: Christopher | last post by:
How would I go about indenenting for each level of recursion, if I am trying to output the contents of a class which contains its own type? where AttributeGroupMap is typdef...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.