Search code examples
c++triangle

How can I do an empty triangle with stars in c++ to after do the British flag?


This is the flag that I have to get at the end:

*******************
**       *       **
* *      *      * *
*  *     *     *  *
*   *    *    *   *
*    *   *   *    *
*     *  *  *     *
*      * * *      *
*       ***       *
*******************
*       ***       *
*      * * *      *
*     *  *  *     *
*    *   *   *    *
*   *    *    *   *
*  *     *     *  *
* *      *      * *
**       *       **
*******************

I know how to do full star triangles but when it's empty on the inside I have no idea about how to proceed. Can anyone help me?

I tried and I just know how to do full star triangles and a star square/rectangle empty on the inside here is the code:

int main(void)
{
    int i, j, length, width;

    cout << "Length of rectangle? ";
    cin >> length;
    cout << endl;

    cout << "Width of rectangle? ";
    cin >> width;
    cout << endl;

    for ( i = 0; i < length; i++ )
        cout << "*";

    cout << endl;

    for ( i = 1; i < width - 1; i++ )
    {
        cout << "*";

        for ( j = 1; j < length - 1; j++ )
        {
            cout << " ";
        }

        cout << "*";
        cout << endl;
    }

    for ( i = 0; i < length; i++)
        cout << "*";

    cout << endl;

    return 0;
}

Solution

  • Lines 1, 10 and 19 are easy, as they each consist only of 19 *.

    The problem is the lines 2 to 9 and 11 to 19.

    However, do you notice a pattern in lines 2 to 9?

    Line 2 consists of one *

    • followed by 0 spaces
    • followed by one *
    • followed by 7 spaces
    • followed by one *
    • followed by 7 spaces
    • followed by one *
    • followed by 0 spaces

    followed by one *

    Line 3 consists of one *

    • followed by 1 spaces
    • followed by one *
    • followed by 6 spaces
    • followed by one *
    • followed by 6 spaces
    • followed by one *
    • followed by 1 spaces

    followed by one *.

    Line 4 consists of one *

    • followed by 2 spaces
    • followed by one *
    • followed by 5 spaces
    • followed by one *
    • followed by 5 spaces
    • followed by one *
    • followed by 2 spaces

    followed by one *.

    Line 5 consists of one *

    • followed by 3 spaces
    • followed by one *
    • followed by 4 spaces
    • followed by one *
    • followed by 4 spaces
    • followed by one *
    • followed by 3 spaces

    followed by one *.

    Line 6 consists of one *

    • followed by 4 spaces
    • followed by one *
    • followed by 3 spaces
    • followed by one *
    • followed by 3 spaces
    • followed by one *
    • followed by 4 spaces

    followed by one *.

    Line 7 consists of one *

    • followed by 5 spaces
    • followed by one *
    • followed by 2 spaces
    • followed by one *
    • followed by 2 spaces
    • followed by one *
    • followed by 5 spaces

    followed by one *.

    Line 8 consists of one *

    • followed by 6 spaces
    • followed by one *
    • followed by 1 spaces
    • followed by one *
    • followed by 1 spaces
    • followed by one *
    • followed by 6 spaces

    followed by one *.

    Line 9 consists of one *

    • followed by 7 spaces
    • followed by one *
    • followed by 0 spaces
    • followed by one *
    • followed by 0 spaces
    • followed by one *
    • followed by 7 spaces

    followed by one *.

    The pattern is the following:

    Assuming that size is the total size of the triangle (which is 19 in your case), then

    line n consists of one *

    • followed by n-2 spaces
    • followed by one *
    • followed by (size/2) - n spaces
    • followed by one *
    • followed by (size/2) - n spaces
    • followed by one *
    • followed by n-2 spaces

    followed by one *.

    Note that in C, the result of 19 / 2 is 9, as the fractional part of the division is discarded.

    Using this information about the pattern, you should be able to create a loop that in every loop iteration, prints one line as described above. That way, you should be able to solve the problem of printing the lines 2 to 9.

    Printing the lines 11 to 19 should be easy afterwards, because these lines must only be printed in reverse order of the lines 2 to 9.

    I will now also post my solution, which solves the problem as described above:

    #include <iostream>
    
    const int MAP_SIZE = 19;
    
    static_assert( MAP_SIZE % 2 == 1, "MAP_SIZE must be odd" );
    
    int main( void )
    {
        //print first horizontal line
        for ( int i = 0; i < MAP_SIZE; i++ )
            std::cout << '*';
        std::cout << '\n';
    
        //print top half of flag
        for ( int i = 0; i < MAP_SIZE / 2 - 1; i++ )
        {
            std::cout << '*';
            for ( int j = 0; j < i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < i; j++ )
                std::cout << ' ';
            std::cout << '*';
            std::cout << '\n';
        }
    
        //print second horizontal line
        for ( int i = 0; i < MAP_SIZE; i++ )
            std::cout << '*';
        std::cout << '\n';
    
        //print bottom half of flag
        for ( int i = 0; i < MAP_SIZE / 2 - 1; i++ )
        {
            std::cout << '*';
            for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < i; j++ )
                std::cout << ' ';
            std::cout << '*';
            for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
                std::cout << ' ';
            std::cout << '*';
            std::cout << '\n';
        }
    
        //print third horizontal line
        for ( int i = 0; i < MAP_SIZE; i++ )
            std::cout << '*';
        std::cout << '\n';
    }
    

    However, I think that this problem is easier to solve using a 2D array (which you stated that you are not allowed to use). The 2D array is initialized to spaces and then the 3 horizontal, 3 vertical and 2 diagonal lines are drawn:

    #include <iostream>
    
    const int MAP_SIZE = 19;
    
    static_assert( MAP_SIZE % 2 == 1, "MAP_SIZE must be odd" );
    
    int main( void )
    {
        char map[MAP_SIZE][MAP_SIZE];
    
        //initialize 2D array to spaces
        for ( int i = 0; i < MAP_SIZE; i++ )
            for ( int j = 0; j < MAP_SIZE; j++ )
                map[i][j] = ' ';
    
        //draw the 3 horizontal lines
        for ( int i = 0; i < MAP_SIZE; i++ )
        {
            map[         0][i] = '*';
            map[MAP_SIZE/2][i] = '*';
            map[MAP_SIZE-1][i] = '*';
        }
    
        //draw the 3 vertical lines
        for ( int i = 0; i < MAP_SIZE; i++ )
        {
            map[i][         0] = '*';
            map[i][MAP_SIZE/2] = '*';
            map[i][MAP_SIZE-1] = '*';
        }
    
        //draw the 2 diagonal lines
        for ( int i = 0; i < MAP_SIZE; i++ )
        {
            map[i][           i] = '*';
            map[i][MAP_SIZE-i-1] = '*';
        }
    
        //print the result
        for ( int i = 0; i < MAP_SIZE; i++ )
        {
            std::cout.write( map[i], MAP_SIZE );
            std::cout.put( '\n' );
        }
    }