Trying to print a certain shape

Nov 25, 2012 at 7:01pm
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 Nov 25, 2012 at 7:03pm
Nov 25, 2012 at 7:15pm
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 Nov 25, 2012 at 7:16pm
Nov 25, 2012 at 7:21pm
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.
Nov 25, 2012 at 7:25pm
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.
Nov 25, 2012 at 7:35pm
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 Nov 25, 2012 at 7:35pm
Nov 25, 2012 at 7:39pm
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.
Nov 25, 2012 at 7:58pm
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 Nov 25, 2012 at 8:04pm
Nov 25, 2012 at 8:04pm
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.