Trying to print a certain shape

Greetings, I'm trying to print this and I'm wondering if someone can help me.

Example:
Input = 5
Prints:

*aaaaaaaa*
**aaaaaa**
***aaaa***
****aa****
**********
[The 'a' is supposed to be a space, but I typed 'a' here because otherwise it changes the shape]

I figured out how to do the triangle with the bottom left corner and bottom right corner separately, but I can't manage to print this specific shape.
Last edited on
Hmm...

How are you doing the triangles? For the left side specifically, are you adding the spaces so that it comes out like this?

1
2
3
4
5
*aaaa
**aaa
***aa
****a
*****

(Where a is a space?)

Or are you just printing the asterisk and going immediately to the next line?

The way I'm seeing it, the solution would be something like

[code]for(int i=1;i<=inputVal;i++)
cout<<"code for left side triangle with spaces""<<"code for right side triangle"<<endl;
Last edited on
I'm not sure what you mean, but when I did the triangles separately this is what I used:

Bottom left corner triangle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void main()
{
	int i, j, k, num;
	cout << "Enter a number:" << endl;           
	cin >> num;                                  
	for (i=1; i<=num; i++)                       
	{                                            
		for (j=1; j<=i; j++)                     
		{
			cout << "*";
		}
		cout << endl;
	}
}


Bottom right corner triangle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void main()
{
	int i, j, k, num;
	cout << "Enter a number:" << endl;               
	cin >> num;                                                               
	for (i=1; i<=num; i++)                                        
	{                                          
		for (k=num-i; k>=1; k--)               
			cout << " ";                          
		for (j=1; j<=i; j++)                     
		{
			cout << "*";
		}
		cout << endl;
	}
}


I'm not sure if I have to use these codes or if that shape is a whole different code.
You could probably combine those two sets of code.
Think of it in terms of generating a single line of the pattern at a time. Of course that is within a loop, thus generating the complete pattern.

Bear in mind that the first one doesn't output any spaces (just asterisks) so you will actually need twice as many spaces as printed in the second example.
Aha.

All you'll need to do is combine your loops.


When you combine your loops to make the bigger shape, your loop will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (i=1; i<=num; i++)                       
{                                            
	for (j=1; j<=i; j++)                //standard left side triangle     
	{
		cout << "*";
	}
        for (k=2*(num-i); k>=1; k--)    //combined the spaces in the middle by doubling the amount           
		cout << " ";                          
	for (j=1; j<=i; j++)                     
	{
		cout << "*";                //copied from right side triangle
	}
	cout << endl;
}
Last edited on
It also depends on whether there are restrictions on which methods you are allowed or expected to use. For example, you might generate a string of characters, such as this: std::string stars(i,'*'); and a related one for the spaces. Then just output the three parts which make up the line.
1
2
3
4
5
6
7
8
9
10
11
unsigned int n = 0;

std::cout << "Enter a positive number: "; std::cin >> n;
std::cout << std::endl;

for ( unsigned int i  = 1; i <= n; i++ )
{
	std::cout << std::setfill( '*' ) << std::setw( i ) << ""
	          << std::setfill( ' ' ) << std::setw( 2 * ( n - i ) ) << ""
		  << std::setfill( '*' ) << std::setw( i ) << "" << std::endl;
}
Last edited on
Thank you guys for the quick and helpful answers. Took me a long time but I finally understand it now. :)
Topic archived. No new replies allowed.