-3

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.

user1118321
  • 25,567
  • 4
  • 55
  • 86
lisabits
  • 1
  • 1
  • 8

2 Answers2

0

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;
}
Johnmph
  • 3,391
  • 24
  • 32
  • Yeah, I'm not going to use one function because it turned out to be more than I thought and I'm still new at this. Just have a hard teacher. Thank you for answering! – lisabits Oct 25 '14 at 00:35
-1

This will help you. Try it here.

#include <iostream>
using namespace std;

void spaces(char space) {
    if (space <= 0) {
        return;
    }
    std::cout << " ";
    spaces(space-1);
}

void line(char start, char end, char space) {
    if (start != end) {
        std::cout << start;
        line(start+1, end, space-1);
        std::cout << start;
    } else {
        std::cout << end;
    }
}

void countdown(char start, char end, char space) {
    ++space;
    if (start != end) {
        countdown(start, end-1, space);
    }
    spaces(space);
    line(start, end, space);
    spaces(space);
    std::cout << std::endl;
}

void range(char start, char end) {
    countdown(start, end, 0);
}

int main() {
    range('a', 'g');
    return 0;
}
aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Huh, I was thinking that the whole thing would be just one recursive function. But I see you have spaces recursively. – lisabits Oct 23 '14 at 20:23
  • It could be possible to do it with one function. I'm not clever enough for such tasks :) – aardvarkk Oct 23 '14 at 20:26
  • Well, thank you for giving me a direction to go in instead of being a jerk like some people on this site :) ... Like them voting me down and snarky comments. – lisabits Oct 23 '14 at 20:34
  • @lisabits Yeah, this site can be a bit tough. In general, people here want to be sure that everybody is putting in the effort and not asking for problems to be solved for them. Many times it's justified, but a lot of people take it over the top. Anyway, I hope you can glean something from my answer and use it to better your programming skills! – aardvarkk Oct 23 '14 at 21:22