I was having a discussion with a coworker about how to transform the following iterative function into a strictly recursive function. We know that all iterative functions can be transformed into recursive functions; however, my coworker remembered that this particular implementation used only three parameters. We can't re-solve this problem. Are we remembering incorrectly? Or are we missing something simple?
void iterative_function (char a, char b, int width) {
int i = width;
while (i > 0) {
cout << string(i, a) << string(width-i, b) << endl;
i -= 2;
}
i = width % 2;
while (i <= width) {
cout << string(i, a) << string(width-i, b) << endl;
i += 2;
}
}
Where the output looks something like below when called like iterative_function('X', '-', 5).
XXXXX
XXX--
X----
XXX--
XXXXX
EDIT: Here is a small skeleton of what the recursive version might look like:
void recursive_function (char a, char b, int width) {
if (width > -1) {
cout << string(width, a) << endl;
recursive(a, b, width - 2);
cout << string(width, a) << endl;
}
}
Except the problem here is filling the right side out with, say, hyphens.
iis playing.