Ok, I just can't figure this problem out. I have to print out any size triangle in characters inputting two letters. Ex: range(c, j) and it has to be recursive. I can't have any loops in my code. Output is supposed to look like this:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
And so on... So, if you notice, It prints forwards until it gets to the newest letter, then prints backwards. Any suggestions on how to do this are GREATLY appreciated.
EDIT: I am NOT trying to cheat. I am totally stuck on how to approach this so I'm asking for suggestions on that, not for someone to give me the code for the whole program. Thanks.
The solution of aardvarkk is the best but if you want a version with only one function :
#include <iostream>
using namespace std;
void recursivePrintLettersTriangle (char start, char end, char current, int space, bool newLine) {
// Print spaces
if ((current + space) < end) {
std::cout << ' ';
recursivePrintLettersTriangle(start, end, current, space + 1, newLine);
// Print letters
} else if (start <= current) {
std::cout << start;
if (start < current) {
recursivePrintLettersTriangle(start + 1, end, current, space, false);
std::cout << start;
}
// Go to next line
if (newLine) {
std::cout << std::endl;
if (current < end) {
recursivePrintLettersTriangle(start, end, current + 1, 0, newLine);
}
}
}
}
void showLettersTriangle (char start, char end) {
recursivePrintLettersTriangle(start, end, start, 0, true);
}
int main() {
showLettersTriangle('a', 'g');
return 0;
}