Thread: Need help making a program that prints a triangle of symbols in a certain direction

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

    Need help making a program that prints a triangle of symbols in a certain direction

    So I need to make a program that prints a triangle of symbols in a certain direction.
    For example:
    Code:
    How many rows? 3
    
      @
     @@@
    @@@@@
    but I can't figure out how to do that. It's rare that I post without figuring anything out, but I'm just simply not sure. I have a program that prints a triangle in a similar direction, so maybe if I could get some hints as to what to do with that (HINTS, not direct source code) it would help.
    My code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ( ) {
        int rows;
        cout << "How many rows? ";
        cin >> rows;
        rows = rows + 1;
        cout << endl;
        for(int r = 0; r < rows; r++) {
                for(int c = 0; c < r; c++) {
                        cout << "@";
                        }
                        cout << endl;
                        }
                        }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Can anyone help?

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, you know the maximum number of @'s is dependent upon the number of rows. You go from 1 to 3 to 5.

    You have 5 maximum indices. You know for 0, only index 2 gets printed. For row 1, indices 1, 2, 3 get printed. For row 2, all 5 indices get printed.

    So just figure out when you should print @ for an index in a row and a blank space.

    Basically, imagine a 3x5 array. Some elements of the array are @ and other elements are blank spaces. Assign the proper values to the array and then it will print properly.
    Last edited by MutantJohn; 01-12-2015 at 02:31 PM.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay, here's another hint. Notice how the middle column index is 2? (This makes sense as the possible indices are 0, 1, 2, 3, 4)

    Well, when you're assigning, you can use that to your advantage. For row 0, array[0][2] = @.

    For row 1, array[1][1], array[1][2], array[1][3] are @.

    For row 2, array[2][0 - 4] are @.

    You can write a for-loop to assign values in both directions using the row number.

    What I want you to see is that 2 - 1 = 1 and 2 + 1 = 3.

    2 - 2 = 0, 2 - 1 = 1 and 2 + 1 = 3 and 2 + 2 = 4.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by MutantJohn View Post
    Well, you know the maximum number of @'s is dependent upon the number of rows. You go from 1 to 3 to 5.

    You have 5 maximum indices. You know for 0, only index 2 gets printed. For row 1, indices 1, 2, 3 get printed. For row 2, all 5 indices get printed.

    So just figure out when you should print @ for an index in a row and a blank space.

    Basically, imagine a 3x5 array. Some elements of the array are @ and other elements are blank spaces. Assign the proper values to the array and then it will print properly.
    OK, I need it to print @ after a certain number of spaces depending on row, but how do I do that?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kelton2
    OK, I need it to print @ after a certain number of spaces depending on row, but how do I do that?
    Look for patterns. For example, observe that if the input is 3, the leading spaces on each line follow a pattern of: 2, 1, 0. If the input is 4, the leading spaces on each line line follow a pattern of: 3, 2, 1, 0.

    Likewise, look for patterns for the number of @ to print on each line. Can you deduce a formula?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A good start would be better variable names.
    Code:
    for ( row = 0 ; row < maxRows ; row++ ) {
      int numSpaces = ...
      int numSymbols = ...
    }
    As laserlight says, once you know the functions to tell you the answers for these two variables, writing loops to just do it is a doddle.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-02-2015, 12:21 AM
  2. Replies: 1
    Last Post: 09-12-2013, 11:18 PM
  3. Help making Triangle
    By jensklemp in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2010, 03:59 PM
  4. DLL Making and Symbols - 2 Queries
    By Tronic in forum C++ Programming
    Replies: 9
    Last Post: 12-31-2004, 01:11 AM
  5. Undefined symbols from ld? ; making bundle; gcc 3.3
    By profxtjb in forum C Programming
    Replies: 5
    Last Post: 06-15-2004, 01:31 AM