# triangle of *

This is a discussion on triangle of * within the C++ Programming forums, part of the General Programming Boards category; >(Danged if I know how that young lady figured this out, though. ) The trick was finding the correct pattern. ...

1. >(Danged if I know how that young lady figured this out, though. )
The trick was finding the correct pattern. I'll admit I cheated a bit because I knew a similar pattern by bit-wise complementing both the inner and outer counters of a triangle pattern loop:
Code:
```int main()
{
for ( int n = 0; n < 8; ++n ) {
for ( int k = 0; k <= n; ++k ) {
int r = ( ~n & ~k );
std::cout<< r;
}

std::cout<<'\n';
}

std::cin.get();
}```
This prints the pattern

-1
-2-2
-3-4-3
-4-4-4-4
-5-6-7-8-5
-6-6-8-8-6-6
-7-8-7-8-7-8-7
-8-8-8-8-8-8-8-8

Notice how the outer edges are predictable as well as the repeating pattern of values inside the outer triangle. I knew that I could take advantage of this if I could get the alternating patterns to have a predictable value as well as a pattern. In this case, 0 was a good value for the values matching the outer edges of the triangle. The inner alternating values couldn't easily be made to a single value, so I chose to use non-zero.

Now, since I wanted the outer edges to match the counter for the outer loop, I couldn't remove the complement for that counter or the pattern would change to the less predictable pattern of

0
10
220
3210
44440
545410
6644220
76543210

I could still work with this, but seeing as how I was closer with the complement on the outer counter, I instead removed the complement on the inner loop counter, resulting in this pattern

0
00
010
0000
01230
002200
0101010
00000000

This was perfect, so I changed the code to print characters instead of integers and the end result was:
Code:
```*
**
* *
****
*   *
**  **
* * * *
********```
Instant Sierpinski triangle.

-Prelude

2. char *x="*******";for(int i=0;i++<7;)printf("%.*s %.*s\n",i,x,8-i,x);

Page 2 of 2 First 12
Popular pages Recent additions