Clipping a Triangle of Digits

This is a discussion on Clipping a Triangle of Digits within the C++ Programming forums, part of the General Programming Boards category; I'm attempting to create a computer program that prints a triangle of digits, as well as a "ruler" that measures ...

  1. #1
    Registered User towely's Avatar
    Join Date
    Oct 2009
    Posts
    12

    Clipping a Triangle of Digits

    I'm attempting to create a computer program that prints a triangle of digits, as well as a "ruler" that measures the user specified width of the screen, showing the width in digits. The ruler must have a tens row and a ones row.

    The program reads three integers:
    a) the screen width (maximum 80)
    b) the width of the triangle.
    c) the position of the 1 column of the triangle, relative to the left margin.

    The '1' column of the triangle must be printed at the given position on the screen. If the screen width is not wide enough to fit the entire triangle, the triangle must be clipped. In the extreme case, if the position is greater than the screen width, nothing is drawn (the entire triangle is clipped).

    Here's a couple examples of what the output should look like:
    (Sorry about the code tags, it's the only way I could show the correct spacing)

    Example one:
    Code:
    Enter screen width: 20
    Enter triangle width: 4
    Enter position: 7
             1         2
    12345678901234567890
    
          1234
          123
          12
          1
    Example two:
    Enter screen width: 10
    Enter triangle width: 4
    Enter position: 8
    Code:
             1
    1234567890
    
           123
           123
           12
           1
    I've gotten the triangle and ruler to print correctly, and I've figured out how to properly put spaces before the triangle.
    My problem is with the code for Clipping the end of the triangle off. For some reason, my program stops printing the triangle two columns early.

    Why does it stop printing 2 lines early?

    Here's what the output looks like, followed by my code. Any quick help would be appreciated.

    Code:
    Enter Screen Width: 30
    Enter Triangle Width: 20
    Enter Position: 21
             1         2         3
    123456789012345678901234567890
    
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        12345678
                        1234567
                        123456
                        12345
                        1234
                        123
                        12
                        1
    So there you can see that it stops printing two lines early.
    Here's my code, where am I going wrong?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {
       int screenWidth, triWidth, position;
    
       cout << "Enter Screen Width: ";
       cin >> screenWidth;
       cout << "Enter Triangle Width: ";
       cin >> triWidth;
       cout << "Enter Position: ";
       cin >> position;
    
       int ruler = (screenWidth / 10);
    
       for (int topRuler = 1; topRuler <= ruler; topRuler++)
       {
          cout << "         " << topRuler; // Prints the 1st ruler                                              
       }
       cout << endl;
    
       int showNextRuler;
       for (int nextRuler = 1; nextRuler <= screenWidth; nextRuler++)
       {
          int showNextRuler = (nextRuler % 10);
          cout << showNextRuler;  // Prints the 2nd ruler                                                       
       }
       cout << endl;
       cout << endl;
    
       for (int i = triWidth; i >= 1; i--)
       {
    
          for (int y = 1; y < position; y++)
          {
             cout.put (' '); // Prints spaces before the triangle                                               
          }
    
          for (int triangle = 1; triangle <= i && (triangle + position) <= screenWidth; triangle++)
          {
             int showTriangle;
    
             showTriangle = (triangle % 10);
    
                if (triangle + position >= screenWidth)
                {
                   break; // Clips the Triangle - Why does it begin clipping 2 spaces early? 
                }
                else
                {
                   cout << showTriangle;
                }
          }
          cout << endl;
       }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You lose one spot by failing to count your triangle properly (your formula assumes it starts at 0, but you start at 1), and you lose one spot by using an equal sign, since that would mean you don't print when the position equals the size (i.e., the last column).

  3. #3
    Registered User ARUNTEJ's Avatar
    Join Date
    Oct 2009
    Posts
    1
    Can you tell me how to get this output:
    *
    * *
    * * *
    * * * *
    please!!

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    ok just do one thing


    Code:
      for (int outer_count = 0; outer_cout < N; ++outer_count) {
        for (int inner_count = 0; inner_count < outer_count; ++iner_count)
          printf("*  ");
        printf("\n");
      }

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