Broken Triangle when printed out

Jul 21, 2020 at 10:07pm
So I am supposed to create a triangle with the base the size of what the user enters. For example if the user enters 5:
1
2
3
4
5
6
7
8
9
*
**
***
****
*****
****
***
**
*


But instead I get a weird looking shape. It looks right, but obviously there's some issue. How would I modify the bottom part to make it right?

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
#include <iostream>

using namespace std;
void triangle(int height);
void drawTriangle(int height);
void drawTriangleBottom(int height);
int main()

{
	int height;
	cout << "What is your desired triangle size? ";
	cin >> height;
	triangle(height);
}

	void triangle(int height) // places the 2 parts together
	{
		for(int i = 0; i <= height; i++)
		{
			drawTriangle(i);
			drawTriangleBottom(i);
			cout << endl; 
		}
	}
	
	void drawTriangle(int height) // top part
	{
	for(int i = 0; i < height; i++)
		{
			cout << "*"; 
		}
	}
	
	void drawTriangleBottom(int height) // for the bottom part
	{
	for(int i = height; i > -1; --i)
		{
			cout << "*"; 
		}
	}
Jul 21, 2020 at 10:55pm
Make the two functions print different characters. That helps you see what you actually do.

How many lines do you print? How many endl?
Jul 22, 2020 at 12:05am
It prints over on the same lines, basically doing the same thing. How would I flip that? I tried changing around the things on line 36, but it would only give me errors.
Jul 22, 2020 at 12:17am
In triangle, have two separate for loops.
First one counts up from 0 to ~<max value>, calling drawTriangle each time.
The second counts down from ~<max value> to 0, calling drawTriangle each time.

You don't even need your drawTriangleBottom function, it's completely redundant with drawTriangle.

Hint: Pay attention to which half should actually draw the peak of the triangle. You don't want to draw a trapezoid instead.

Also, careful of your assignment's wording. The base of the triangle is the number of rows in your example. 5 is the height of the triangle in your example. The base in your example is 5 * 2 - 1 = 9.
Last edited on Jul 22, 2020 at 12:17am
Jul 22, 2020 at 12:39am
Thank you! I did the 2 for loops in triangle, subtracted 1 from height, and managed to reverse it. Which made exactly what I needed!
Jul 22, 2020 at 6:47am
Lets think about the name:
1
2
3
4
5
6
7
void drawTriangle( int height )
{
  for ( int i = 0; i < height; i++ )
  {
    cout << "*"; 
  }
}

The name implies that this function draws a triangle.

However, that is hardly what it actually does. It prints height asterisks on same line.

It does not print a newline, so
1
2
drawTriangle( X );
drawTriangle( Y );

will print X+Y asterisks on same line.
Same result as if one would call drawTriangle( X + Y );

I would not call a line a triangle.

Names can be anything, but if they are not descriptive, then they can cloud our thinking.


1
2
3
4
5
6
// prints N tokens
void print( int N, char token ) {
  for ( int i = 0; i < N; i++ ) {
    cout << token; 
  }
}

Jul 22, 2020 at 12:39pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

string triangle( int h, int w = 1 )
{
   string row( w, '*');   row += '\n';
   return row + ( w < h ? triangle( h, w + 1 ) + row : "" );
}

int main()
{
	int height;
	cout << "What is your desired triangle size? ";   	cin >> height;
	cout << triangle(height);
}
Jul 22, 2020 at 1:38pm
1
2
3
4
5
6
7
//Pseudocode:

int height = 0;
cin >> height;

for(int i = 1; i <= height; ++i) std::cout << std::string(i, '*') << '\n';
for(int i = height - 1; i > 0; --i) std::cout<< std::string(i, '*') << '\n';
Last edited on Jul 22, 2020 at 1:38pm
Jul 22, 2020 at 2:24pm
1
2
3
4
5
6
int height = 0;
std::cin >> height;
for ( int row = 1; row < 2*height; ++row ) {
    int length = std::min( 2*height - row, row );
    std::cout << std::string(length, '*') << '\n';
}
Jul 22, 2020 at 3:09pm
You can do it with a single loop with the use of abs():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
using std::cin;
using std::cout;

void print(char ch, int count)
{
    for (int i=0; i<count; ++i) {
	cout << ch;
    }
    cout << '\n';
}
	  
int main()
{
    int n;
    cout << "Enter triangle size: ";
    cin >> n;
    for (int i = -n+1; i < n; ++i) {
	print('*', n-abs(i));
    }
}

Jul 22, 2020 at 3:34pm
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
#include <iostream>

using namespace std;
void triangle(int height);
void drawTriangle(int height);
void drawTriangleBottom(int height);
int main()

{
	int height;
	cout << "What is your desired triangle size? ";
	cin >> height;
	triangle(height);
	
	return 0;
}

	void triangle(int height) // places the 2 parts together
	{
			drawTriangle(height);
			drawTriangleBottom(height);
			cout << endl; 
	}
	
	void drawTriangle(int height) // top part
	{
	  for (int i = 0; i < height; i++) {
            for(int j = -1; j < i; j++) {
              cout << "*";
            }
        cout << endl;
      }
    }
	
	void drawTriangleBottom(int height) // for the bottom part
	{
	  for (int i = height -1; i > 0; i--) {
        for(int j = i; j > 0; j--) {
          cout << "*";
        }
        cout << endl;
      }
	}
Last edited on Jul 22, 2020 at 3:34pm
Jul 22, 2020 at 3:37pm
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
#include <iostream>

using namespace std;

void triangle(int height);
void drawTriangleTop(int height);
void drawTriangleBottom(int height);

int main()

{
    int height;
    cout << "What is your desired triangle size? ";
    cin >> height;
    triangle(height);
}

void triangle(int height) // places the 2 parts together
{
    drawTriangleTop(height);
    drawTriangleBottom(height);
    cout << endl;
}

void drawTriangleTop(int height) // top part
{
    for(int row = 0; row < height; row++)
    {
        for(int i = 0; i < row; i++)
        {
            cout << "p";
        }
        cout << endl;
    }
}

void drawTriangleBottom(int height) // for the bottom part
{
    for(int row = 0; row < height; row++)
    {
        for(int i = 0; i < height - row; i++)
        {
            cout << "q";
        }
        cout << endl;
    }
}
Topic archived. No new replies allowed.