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.
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.
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;
}
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.