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. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >(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
    My best code is written with the delete key.

  2. #17
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    char *x="*******";for(int i=0;i++<7;)printf("%.*s %.*s\n",i,x,8-i,x);

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 05:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21