Triangle program help

Pages: 12
I am trying to write a program that takes in two characters (the range) and outputs a triangle using those characters. The only stipulation is that I cannot use any loops in my code; I have to use recursion. I have figured out the bottom part of the triangle (the triangle's base) but I can't understand how to make the actual triangle. Here is the code for my base, can anyone help me figure the rest out?

1
2
3
4
5
6
7
8
9
10
11
12
void Triangle(char ch1, char ch2)
{
    if (ch1 <= ch2)                 // If first letter is less than second letter
    {
        cout << ch1 << " " ;        // Print out first letter, then a space
        Triangle(ch1 + 1, ch2);     // Keep printing out each letter until we get to second letter
        if (ch1 < ch2)              // For the second half of bottom, you want all letters 
            cout << ch1 << " ";     // except ch2 to print out 
    }
    else
        return;
}


That function produces this output:
a b c d e f g h i j k l m l k j i h g f e d c b a


My function call looked like this:
Triangle('a', 'm');


I need to find out how to create this (but centered, not left-justified):
a
aba
abcba
abcdcba
abcdedcba
abcdefedcba
abcdefgfdecba
abcdefghgfedcba
abcdefghihgfedcba
abcdefghijihgfedcba
abcdefghijkjihgfedcba
abcdefghijklkjihgfedcba
abcdefghijklmlkjihgfedcba

Come on, please?! I'm not asking for code, just some help.
Well, it seems like you have the basic idea of how to do it. If your function works for all of the ranges you need, it shouldn't be that hard to do the triangle.

Try thinking about when each row needs to be printed out. You have a function that prints the last one, so when should you try to print the rows before it.
Try to think of recursion function call.. in your call you are calling
Triangle('a','m')
Triangle('b','m')
Triangle('c','m')
.
.
.
.
Triangle('m','m')
that's not useful at all..
may be you call yo recursion function will look like.
Triangle('a','m')
Triangle('a','b')
Triangle('a','c')
.
.
.
Triangle('a','m')

Think about it that might be useful to you.
Thank you so much for replying! This darn triangle has me stumped. I will work on it and see if I can figure it out.
Thank you for your help!
How is this? Closer to what I want? I'm itching to try this out and see if it works, but I'm at work until this afternoon without CodeBlocks. Does this look better?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Triangle(char ch1, char ch2)
{
     char char2;			            // Variable to skip through ch2
     char2 = ch1			            // Set 2nd variable to same value as 1st 
     // Can’t use loops, so stopping case would be… 
     if (char2 == ch2)		         // If 2nd letter is equal to second letter passed in
     {
           cout << ch1 << “ “;           // Print out first letter, then a space
           Triangle(ch1 + 1, ch2);       // Keep printing out each letter until we get to second letter
           if (ch1 < ch2)	         // For the second half of bottom, you want all letters
                cout << ch1 << “ “ ;	 // except ch2 to print out
      }
      else 
                Triangle(ch1,  (ch2 + 1));// We print one additional letter each line
}
The best way to look at this is just about how you said, you want it centered. You'll need spaces for all the times you're not printing to the screen. ch2 - ch1 will give you the right amount of spaces on the left to center it. Also, your new code is wrong. it's not going to give you the expected results you want. You might want to try numbers first if the letters are confusing you. do Triangle(1, 9) and see if you can get that right, then simply change all ints to chars.

You also have to think that there should be conditions that print out only under those conditions. If your first value is less, print out a space, and call the function again, with a lower second value. Keep doing that until they're equal, then print out the value. Then you need to call triangle again, with a higher first value, and repeat the recursion until it prints that out again as well.

I'll have to type up something similar for my own want now. -.-
Oh, darn! Oh well, I'm almost never right the first time around, so I guess that's okay. The minute I get home I'm going to figure this out. And I had forgotten about the spaces, sounds like a pain. Oh well. I'm pumped! I'm going to figure this problem out - TODAY- if it takes me all night. (How sad that this type of problem could very well take me all night, huh? But I'm not going to let it get me down. I will be a ninja programmer one of these decades, I swear.)

Thanks so much for the help!
I was trying this out, and I can't figure out a way to type this up properly using recursion without a single loop (for, while, etc.) and without using separate functions. If you can use at least a for loop, this is fairly simple, otherwise, it's complicated. Maybe there is an easier way to do this than my 5 different approaches, but nothing is working correctly.
Here is what my teacher just sent me: My function is this top part, and then he added at the bottom...
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
void Triangle(char ch1, char ch2)
{
    if (ch1 <= ch2)                 // If first letter is less than second letter
    {
        cout << ch1 << " " ;        // Print out first letter, then a space
        outfile << ch1 << " " ;
        Triangle(ch1 + 1, ch2);     // Keep printing out each letter until we get to second letter
        if (ch1 < ch2)
        {
            // For the second half of bottom, you want all letters
            cout << ch1 << " ";     // except ch2 to print out
            outfile << ch1 << " ";
        }
    }
    else
        return;
}

The above prints out one line.  Now for multiple lines.

                mline( char c1, char c2)
                {   
                milne( c1 , c2 - 1);   this will print a line one char short (c2 -1)
                            ... I don't actually print the line 'Triangle' until I return 
                        Trianlge( c1, c2)  To print a line
           } 


And oh, complicated, did you say? Lol. Complicated is his middle name. He has to assign everything with a hard, impossible portion in there somewhere, I swear. It's so exhausting.
Last edited on
This is going to defeat the purpose of recursion though. Like granted they call themselves, and each other, but these are supposed to be replaced by loops, loops make things simple and fast. -.-
He hates us? What can I say?
Okay, so I'm trying to fix this program now...and wouldn't you know, I'm stumped the second line in. I thought you could increment your characters by just adding one? Why is the output for this then giving me

a
a 98 a
?

Here's the function:
1
2
3
4
5
void Triangle(char ch1, char ch2)
{
     cout << ch1 << endl;
     cout << ch1 << " " << ch1 +1 << " " << ch1 << endl;
}


Not a function yet, but I'm just trying to get it to do what I want first...
How do I get '98' to output the 'b' I'm looking for?
Because a character is really an integer. When you +1, you're converting from char to int, adding 1, and displaying int. A work around is casting the output to make it display a character:
cout << ch1 << " " << static_cast<char>(ch1 +1) << " " << ch1 << endl;

That should fix that problem, and I have a feeling you're going to be trying to do a for loop, which is what I thought you couldn't do.
Oh, duh! And you're right, I can't do a for loop. Or any loop. This HAS to be done recursively. I was just writing out what I wanted to happen, because I suck at just thinking of what I want to happen. Lol. If I can see it, it's much easier.

I did email my teacher begging for help on this one, and this is what he sent...?

See below. (At end of code.)
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

// Prototypes
int OpenOutput();
void PrintHeader();
void Triangle(char ch1, char ch2);

int main()
{
    OpenOutput();
    PrintHeader();
    Triangle('a', 'm');

return 0;
}

int OpenOutput()
{
    outfile.open("TrianglesOUT");
    if (!outfile)
    {
        cout << "Error opening output file." << endl << endl;
        outfile << "Error opening output file." << endl << endl;
    }
    else
    cout << "Output file opened correctly." << endl << endl;
    outfile << "Output file opened correctly." << endl << endl;
}

void PrintHeader()
{
    cout << "Erin Corona" << endl;
    cout << "CS 372.30137" << endl;
    cout << "Triangles " << endl;
    cout << "** Approved Late Submission **" << endl << endl;

    outfile << "Erin Corona" << endl;
    outfile << "CS 372.30137" << endl;
    outfile << "Triangles " << endl;
    outfile << "** Approved Late Submission **" << endl << endl;
}
void Triangle(char ch1, char ch2)
{
    if (ch1 <= ch2)                 // If first letter is less than second letter
    {
        cout << ch1 << " " ;        // Print out first letter, then a space
        outfile << ch1 << " " ;
        Triangle(ch1 + 1, ch2);     // Keep printing out each letter until we get to second letter
        if (ch1 < ch2)
        {
            // For the second half of bottom, you want all letters
            cout << ch1 << " ";     // except ch2 to print out
            outfile << ch1 << " ";
        }
    }
    else
        return;
}


The above prints out one line. Now for multiple lines.
1
2
3
4
5
6
 mline( char c1, char c2)
                {  
                milne( c1 , c2 - 1);   this will print a line one char short (c2 -1)
                            ... I don't actually print the line 'Triangle' until I return
                        Trianlge( c1, c2)  To print a line
           } 


I'm sure he thought he was helping me, but I don't get it at all...
I am not quite sure how close this is to being what you can use, but I typed this up quickly for you to try. Just need to add your code in that you need:
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
#include <iostream>

void Triangle(char start, char finish);
void PrintLine(char start, char finish, int spaces);

int main() {
   Triangle('a', 'b');

   return 0;
}

void Triangle(char start, char finish) {
   // finish - start is wrong, need to find a new way to calculate the spaces needed
   // To print the first line, we call ourself recursively
   // Until start == finish
   if (start != finish)
      Triangle(start, finish - 1);
   // After the recursion is done, we need to print out our line
   PrintLine(start, finish, finish - start);
}

void PrintLine(char start, char finish, int spaces) {
   // We want to print out our spaces until there is none left to print out
   if (spaces > 0) {
      std::cout << " ";
      PrintLine(start, finish, spaces - 1);
   }
   else {
      // Do code here to print out the letters start - finish - start
      // spaces will have to be 0 here.
   }
}
Last edited on
Thanks, I'll give it a try!
I don't understand how you can do this and have spaces without using for loops. It just doesn't make sense to me. I figured the best thing to do would be to make it work using 'a' and 'b' then making it work for 'c' but the spaces keep causing me issues. You can't pass extra parameters because both of his functions only have two.
I tinkered with it more, and used your code since mine was a bunch of fail. This was the best that I could come up with:
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
#include <iostream>

using namespace std;

void mLine (char ch1, char ch2);
void Triangle(char ch1, char ch2) {
   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', 'c');
   return 0;
}
a
aba
abcba


I was thinking about a static variable, but I couldn't get it to work. I hope you can take it from there though.
I will do my best, thanks so much for helping! I am actually going to see a tutor to help me after work today. He said he is only "recommended" to help with the previous two classes I took, but since he is one class ahead of me now, he'll do his best to help me finish my 3 programs. Here's to hoping!

I'm going to study up at work today and try yet again to have them install an IDE at my desk so I can be more productive at work (on my homework, hee hee.) I have a co-op position, so they are very nice about letting me do homework on my downtime. I don't think they'll let me install Code::Blocks, but what else can I use to code? Visual Express, right? I'm going to see what they have available...

I'll let you all know how my fun programs turn out...I know you're DYING to see. Lol.
Pages: 12