Triangle program help

Pages: 12
Okay, I've been playing around with the code, trying to get the spacing correct. So far, I'm only able to move that initial 'a' to the middle. Why doesn't it move everything to the right?

I've also tried putting in a for loop, to see if I could do with one in the first place. Then, if so, I was going to try to convert the loop to a recursive function, but strangely, I can't even get a loop to work.

Once I can get to a debugger, I will be able to step through it and see exactly how it's working, but for now, does anyone know why only that first line is affected?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
 
using namespace std;
 
void mLine (char ch1, char ch2);
void Triangle(char ch1, char ch2) {
   cout << " ";
   if (ch1 < ch2) {
   Triangle(ch1, ch2 - 1);
   }
   mLine(ch1, ch2);
   cout << "\n";
}
 
void mLine(char ch1, char ch2) {
      if(ch1 <= ch2) {
      cout << ch1;
      mLine(ch1 + 1, ch2);
      if(ch1 < ch2)
         cout << ch1;
   }
}
 
int main() {
   Triangle ('a', 'm');
   return 0;
}


Here's the output:

              a
aba
abcba
abcdcba
abcdedcba
abcdefedcba
abcdefgfedcba
abcdefghgfedcba
abcdefghihgfedcba
abcdefghijihgfedcba
abcdefghijkjihgfedcba
abcdefghijklkjihgfedcba
abcdefghijklmlkjihgfedcba
 
 


Last edited on
It's actually not centering the first line, it's adding ALL of the spaces to the first line because of the recursion. I just got an idea about using the ' ', space character, in the mLine function to display the spaces. I'm still working on the concept, but this is something that's very hard to do. I'm still thinking that a static variable is going to be the only option. I'll tinker with it for a bit and see if I can get it to work.
It's actually not centering the first line, it's adding ALL of the spaces to the first line because of the recursion.

Shhhhhhh. Lol.
Tell me you love me =)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

void mLine (char ch1, char ch2);
void Triangle(char ch1, char ch2) {
   // Last line should have no spaces in it
   static int spaceCounter = 0;
   if (ch1 < ch2) {
      // The previous line should have one extra space
      spaceCounter ++;
      Triangle(ch1, ch2 - 1);
   }
   // Display our spaces
   mLine(' ', spaceCounter);
   // Display our letters
   mLine(ch1, ch2);
   spaceCounter --;
   cout << "\n";
}

void mLine(char ch1, char ch2) {
   // Handle our space recursion
   if(ch1 == ' ') {
      // If we still have spaces left
      if(ch2 != 0) {
         cout << ch1;
         mLine(ch1, ch2 - 1);
      }
   }
   else if(ch1 <= ch2) {
      cout << ch1;
      mLine(ch1 + 1, ch2);
      if(ch1 < ch2)
         cout << ch1;
   }
}

int main() {
   Triangle ('a', 'm');
   return 0;
}
            a
           aba
          abcba
         abcdcba
        abcdedcba
       abcdefedcba
      abcdefgfedcba
     abcdefghgfedcba
    abcdefghihgfedcba
   abcdefghijihgfedcba
  abcdefghijkjihgfedcba
 abcdefghijklkjihgfedcba
abcdefghijklmlkjihgfedcba
I LOOOOOVE YOU!!! Whooo hooooo! I am so excited! Thank you so much! That makes my week 33.33% easier now. I'm going to go figure out what you did.

You are my hero.
Oooh, and it makes me doubly happy, because that's totally close to what I was trying! BUT my stupid counter kept going back to zero and I forgot I could fix that. I did not have it in the mLine function though. That is so great! Whoo hooo! Suuure wish I had a debugger now, so I could see exactly how it's working.

Hey wait...are there such things as online debuggers I don't have to download? Lol. I'm going to google that right now, just in case.

Take a piece of paper and a pencil. Walk through it step by step. Recursion is an issue that makes this method much harder, but I'll give you an example of what it should look like.
I only did the first line, but you can see how long something like this can get. Recursions are extremely powerful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
   Call Triangle ('a', 'm')
   R0 ch1 = 'a' ch2 = 'm' spaceCounter = 0
      spaceCounter = 1 Triangle ('a', 'l')
   R1 ch1 = 'a' ch2 = 'l' spaceCounter = 1
      spaceCounter = 2 Triangle ('a', 'k')
   R2 ch1 = 'a' ch2 = 'k' spaceCounter = 2
      spaceCounter = 3 Triangle ('a', 'j')
   R3 ch1 = 'a' ch2 = 'j' spaceCounter = 3
      spaceCounter = 4 Triangle ('a', 'i')
   R4 ch1 = 'a' ch2 = 'i' spaceCounter = 4
      spaceCounter = 5 Triangle ('a', 'h')
   R5 ch1 = 'a' ch2 = 'h' spaceCounter = 5
      spaceCounter = 6 Triangle ('a', 'g')
   R6 ch1 = 'a' ch2 = 'g' spaceCounter = 6
      spaceCounter = 7 Triangle ('a', 'f')
   R7 ch1 = 'a' ch2 = 'f' spaceCounter = 7
      spaceCounter = 8 Triangle ('a', 'e')
   R8 ch1 = 'a' ch2 = 'e' spaceCounter = 8
      spaceCounter = 9 Triangle ('a', 'd')
   R9 ch1 = 'a' ch2 = 'd' spaceCounter = 9
      spaceCounter = 10 Triangle ('a', 'c')
   R10 ch1 = 'a' ch2 = 'c' spaceCounter = 10
      spaceCounter = 11 Triangle ('a', 'b')
   R11 ch1 = 'a' ch2 = 'b' spaceCounter = 11
      spaceCounter = 12 Triangle ('a', 'a')
   R12 ch1 = 'a' ch2 = 'a' spaceCounter = 12
      ch1 == ch2 mLine(' ', 12)
      MLR0  Display (" ") mLine(' ', 11)
      MLR1  Display ("  ") mLine(' ', 10)
      MLR2  Display ("   ") mLine(' ', 9)
      MLR3  Display ("    ") mLine(' ', 8)
      MLR4  Display ("     ") mLine(' ', 7)
      MLR5  Display ("      ") mLine(' ', 6)
      MLR6  Display ("       ") mLine(' ', 5)
      MLR7  Display ("        ") mLine(' ', 4)
      MLR8  Display ("         ") mLine(' ', 3)
      MLR9  Display ("          ") mLine(' ', 2)
      MLR10 Display ("           ") mLine(' ', 1)
      MLR11 Display ("            ") mLine(' ', 0)
      MLR12 ch2 == 0 End All MLR Recursions
      mLine('a', 'a')
      MLR0 ch1 = 'a' ch2 = 'a'
            Display ("            a") mLine('b', 'a')
      MLR1 ch1 = 'b' ch2 = 'a' ch1 > ch2 End All MLR Recursions
      spaceCounter = 11
      Display ("            a")
      Display ("") End R12
*/


Edit: I don't know of any online debuggers, and I personally don't use a debugger unless I get really stuck. My preferred method of debugging is to use getch() or a similar alternative. This will allow you to "Step" through each part of the code. You just throw it in where you want to see your outputs. You need to include the conio.h library, but it is not standard. You can also display each variable you're curious about with a simple cout statement, but as you can see, doing that would mess up the triangle as well, so use it with caution.
Last edited on
Hey, thanks! That may be one of the most helpful posts yet. I try to kind of do this sometimes, but it never looks that nice. I'm going through it now, and matching the format of what you have here, and I must say, I dig it. This is exactly what I need to be doing.

Thanks!

I'll have to try getch. I do usually have a million couts everywhere to check my stuff. Haha.
Last edited on
I got bored so here are the first 3 lines. If a certain number or variable isn't 100% right, it's due to the quick copy pasting. And quite possibly me getting overwhelmed by the extensiveness of the recursion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
   Call Triangle ('a', 'm')
   R0 ch1 = 'a' ch2 = 'm' spaceCounter = 0
      spaceCounter = 1 Triangle ('a', 'l')
   R1 ch1 = 'a' ch2 = 'l' spaceCounter = 1
      spaceCounter = 2 Triangle ('a', 'k')
   R2 ch1 = 'a' ch2 = 'k' spaceCounter = 2
      spaceCounter = 3 Triangle ('a', 'j')
   R3 ch1 = 'a' ch2 = 'j' spaceCounter = 3
      spaceCounter = 4 Triangle ('a', 'i')
   R4 ch1 = 'a' ch2 = 'i' spaceCounter = 4
      spaceCounter = 5 Triangle ('a', 'h')
   R5 ch1 = 'a' ch2 = 'h' spaceCounter = 5
      spaceCounter = 6 Triangle ('a', 'g')
   R6 ch1 = 'a' ch2 = 'g' spaceCounter = 6
      spaceCounter = 7 Triangle ('a', 'f')
   R7 ch1 = 'a' ch2 = 'f' spaceCounter = 7
      spaceCounter = 8 Triangle ('a', 'e')
   R8 ch1 = 'a' ch2 = 'e' spaceCounter = 8
      spaceCounter = 9 Triangle ('a', 'd')
   R9 ch1 = 'a' ch2 = 'd' spaceCounter = 9
      spaceCounter = 10 Triangle ('a', 'c')
   R10 ch1 = 'a' ch2 = 'c' spaceCounter = 10
      spaceCounter = 11 Triangle ('a', 'b')
   R11 ch1 = 'a' ch2 = 'b' spaceCounter = 11
      spaceCounter = 12 Triangle ('a', 'a')
   R12 ch1 = 'a' ch2 = 'a' spaceCounter = 12
      ch1 == ch2 mLine(' ', 12)
      MLR0  Display (" ") mLine(' ', 11)
      MLR1  Display ("  ") mLine(' ', 10)
      MLR2  Display ("   ") mLine(' ', 9)
      MLR3  Display ("    ") mLine(' ', 8)
      MLR4  Display ("     ") mLine(' ', 7)
      MLR5  Display ("      ") mLine(' ', 6)
      MLR6  Display ("       ") mLine(' ', 5)
      MLR7  Display ("        ") mLine(' ', 4)
      MLR8  Display ("         ") mLine(' ', 3)
      MLR9  Display ("          ") mLine(' ', 2)
      MLR10 Display ("           ") mLine(' ', 1)
      MLR11 Display ("            ") mLine(' ', 0)
      MLR12 ch2 == 0 End All MLR Recursions
      mLine('a', 'a')
      MLR0 ch1 = 'a' ch2 = 'a'
            Display ("            a") mLine('b', 'a')
      MLR1 ch1 = 'b' ch2 = 'a' ch1 > ch2 End All MLR Recursions
      spaceCounter = 11
      Display ("            a")
      Display ("") End R12
   R11 ch1 = 'a' ch2 = 'b' spaceCounter = 11
      mLine(' ', 11)
      MLR0  Display ("            a")
            Display (" ") mLine(' ', 10)
      MLR1  Display ("            a")
            Display ("  ") mLine(' ', 9)
      MLR2  Display ("            a")
            Display ("   ") mLine(' ', 8)
      MLR3  Display ("            a")
            Display ("    ") mLine(' ', 7)
      MLR4  Display ("            a")
            Display ("     ") mLine(' ', 6)
      MLR5  Display ("            a")
            Display ("      ") mLine(' ', 5)
      MLR6  Display ("            a")
            Display ("       ") mLine(' ', 4)
      MLR7  Display ("            a")
            Display ("        ") mLine(' ', 3)
      MLR8  Display ("            a")
            Display ("         ") mLine(' ', 2)
      MLR9  Display ("            a")
            Display ("          ") mLine(' ', 1)
      MLR10 Display ("            a")
            Display ("           ") mLine(' ', 0)
      MLR11 ch2 == 0 End All MLR Recursions
      mLine('a', 'b')
      MLR0 ch1 = 'a' ch2 = 'b'
            Display ("            a")
            Display ("           a") mLine('b', 'b')
      MLR1 ch1 = 'b' ch2 = 'b'
            Display ("            a")
            Display ("           ab") mLine('c', 'b')
      MLR2 ch1 = 'c' ch2 = 'b' ch1 > ch2 End MLR2
      MLR1 ch1 = 'b' ch2 = 'b' ch1 == ch2 End MLR1
      MLR0 ch1 = 'a' ch2 = 'b'
            Display ("            a")
            Display ("           aba") End MLR0
      spaceCounter = 10
      Display ("            a")
      Display ("           aba")
      Display ("") End R11
   R10 ch1 = 'a' ch2 = 'c' spaceCounter = 10
      mLine(' ', 10)
      MLR0  Display ("            a")
            Display ("           aba")
            Display (" ") mLine(' ', 9)
      MLR1  Display ("            a")
            Display ("           aba")
            Display ("  ") mLine(' ', 8)
      MLR2  Display ("            a")
            Display ("           aba")
            Display ("   ") mLine(' ', 7)
      MLR3  Display ("            a")
            Display ("           aba")
            Display ("    ") mLine(' ', 6)
      MLR4  Display ("            a")
            Display ("           aba")
            Display ("     ") mLine(' ', 5)
      MLR5  Display ("            a")
            Display ("           aba")
            Display ("      ") mLine(' ', 4)
      MLR6  Display ("            a")
            Display ("           aba")
            Display ("       ") mLine(' ', 3)
      MLR7  Display ("            a")
            Display ("           aba")
            Display ("        ") mLine(' ', 2)
      MLR8  Display ("            a")
            Display ("           aba")
            Display ("         ") mLine(' ', 1)
      MLR9  Display ("            a")
            Display ("           aba")
            Display ("          ") mLine(' ', 0)
      MLR10 ch2 == 0 End All MLR Recursions
      mLine('a', 'c')
      MLR0 ch1 = 'a' ch2 = 'c'
            Display ("            a")
            Display ("           aba")
            Display ("          a") mLine('b', 'c')
      MLR1 ch1 = 'b' ch2 = 'c'
            Display ("            a")
            Display ("           aba")
            Display ("          ab") mLine('c', 'c')
      MLR2 ch1 = 'c' ch2 = 'c'
            Display ("            a")
            Display ("           aba")
            Display ("          abc") mLine('d', 'c')
      MLR3 ch1 = 'd' ch2 = 'c' ch1 > ch2 End MLR3
      MLR2 ch1 = 'c' ch2 = 'c' ch1 == ch2 End MLR2
      MLR1 ch1 = 'b' ch2 = 'c'
            Display ("            a")
            Display ("           aba")
            Display ("          abcb") End MLR1
      MLR0 ch1 = 'a' ch2 = 'c'
            Display ("            a")
            Display ("           aba")
            Display ("          abcba") End MLR0
      spaceCounter = 9
      Display ("            a")
      Display ("           aba")
      Display ("          abcba")
      Display ("") End R10
*/


Edit: The above is an approximately 6000 characters, and 150 lines. This is a great example of recursion power. I can't do any more lines since I'll run out of characters to show it all, but you can see how each call to one of the functions eventually completes the triangle. It might be a better idea to do 'a' and 'd' if you're trying to do it by hand, or even 'a' and 'c'
Last edited on
That is so great, thanks a lot. That helped so much. And I agree, an 'abc' length will be easier for me to see what's going on, so that's what I'm about to do this morning. I'm really excited though to see how you've done it, paper and pencil style, since whenever I do it, it's such a big mess that I can't really follow it, so it defeats the purpose. This format above is so simple.

Yay!
~You know, teachers tell you to do this, but they don't show you HOW. It probably seems very obvious to them, but I'd been using a little table, and it wasn't helpful in the least. This two line method here, of 'going into function' and then 'coming out of function' is great. Wish I'd thought of it myself.
Great, I'm glad it helped. Now get cracking on your other program. I wish I was able to understand it better.
Today is Converting_my_General_List_to_a_Linked_List day. Fun times.
Topic archived. No new replies allowed.
Pages: 12