Thread: Need help with program that prints a triangle of @ symbols

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Need help with program that prints a triangle of @ symbols

    I'm making a program that prints a triangle of @ signs given rows (but not columns). For example, the output with rows = 4 would be:
    @@@@
    @@@
    @@
    @
    and rows = 3 would be:
    @@@
    @@
    @
    However, trying to make this has given me a program that does something similar (but not the same): for example, with my current program rows = 4 outputs:
    @@@@
    @@@
    @@
    @
    and rows = 3 gives
    @@@
    @@
    @
    It seems that it's just missing a space (and therefore a setw and setfill), but I found 2 problems:
    1. The space needs to not apply to the first line.
    2. I can't get it to make a space before each row without making a space between each column.
    My current code is:
    Code:
      #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ( ) {
        int rows;
        cout << "How many rows? ";
        cin >> rows;
        cout << endl;
        for(int r = 0; r < rows; r++) {
              
                for(int c = rows; c > r; c--) {
                        cout << "@";
                        }
                       // cout << setw(r);
                        cout << endl;
                       // cout << setw(r);
                        }
                
    }
    I have tried putting in << setws and << setfills of various values but it seems to always apply to between each column as well as at the start of each row- what do I do?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > what do I do?
    Perhaps you should try writing out on paper what the actual steps are to solving the problem.

    You know, like
    Code:
    for each row;
      print zero or more spaces;
      print one or more symbols;
    Now can you see where you should be printing some spaces?

    > I have tried putting in << setws and << setfills of various values but ...
    Whilst random experimentation may be fun, and you may occasionally stumble on the correct answer, it isn't a good long term strategy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Ah. So:
    Code:
    (for loop start)
    cout << " ";
    cout << "@";
    (for loop end)
    Of course, the number of times it does this is controlled by the for loop.
    Last edited by Kelton2; 01-01-2015 at 11:48 PM. Reason: forgot ending [code] bbcode

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by Kelton2 View Post
    Ah. So:
    Code:
    (for loop start)
    cout << " ";
    cout << "@";
    (for loop end)
    Of course, the number of times it does this is controlled by the for loop.
    Ok, that doesn't work. Any other help?

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Never mind, fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  2. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  3. Why the program prints line twice?
    By red463 in forum C Programming
    Replies: 15
    Last Post: 04-30-2010, 05:20 AM
  4. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  5. program that prints itself
    By modec in forum C Programming
    Replies: 1
    Last Post: 10-10-2003, 02:14 PM