-
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
-
What do you mean 'indent the lines'?
-
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
-
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 );
}
-
How does the function decrease the number of spaces indented?
-
The number of spaces written to the screen is proportional to the parameter 'level'. Thus, the higher 'level' is, the greater the indent is.
-
^ 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?
-
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
-
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.
-
This doesn't work?
Code:
void recurse ( int level ) {
if ( level > 5 ) return;
indent( level );
cout << "foo";
recurse( level + 1 );
indent( level );
cout << "bar";
}
-
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).
-
Ah right, I forgot that it can do stuff after calling itself :D