c++for-loop

Printing pyramids of stars beside each other (C++)


I have to print pyramids of stars next to each other, all in one line, and the main point is that the pyramids must share a corner star between each other.

I managed to print pyramids correctly, but there are two spaces between each pyramid.

My code:

#include <iostream>
using namespace std;

int main()
{
    int rows, triangle;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of triangles: ";
    cin >> triangle;

    for (int i = 1; i <= rows; i++)
    {
        for (int k = 1; k <= triangle; k++)
        {
            if (k >= 2)
            {
                for (int j = 1; j <= rows - i; j++)
                {
                    cout << "  ";
                }
            }
            else
            {
                for (int j = 1; j <= rows - i; j++)
                {
                    cout << " ";
                }
            }

            for (int j = 1; j <= 2 * i - 1; j++)
            {
                cout << "*";
            }
        }
        cout << endl;
    }

    return 0;
}

Output if e.g. the user chooses 6 rows and 6 triangles (pyramids):

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

-- Edit -- I found a solution without any complex math formula that prints triangles correctly.

#include <iostream>
using namespace std;

int main()
{
    int rows, triangle;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of triangles: ";
    cin >> triangle;

    for (int i = 1; i <= rows; i++)
    {
        for (int k = 1; k <= triangle; k++)
        {
            cout << "\b";
            
            for (int j = 1; j <= rows - i; j++)
            {
                cout << " ";
            }
            for (int j = 1; j <= 2 * i - 1; j++)
            {
                cout << "*";
            }
            for (int j = 1; j <= rows - i; j++)
            {
                cout << " ";
            }
        }
        cout << endl;
    }

    return 0;
}

Output if e.g. the user chooses 6 rows and 4 triangles (pyramids):

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

Solution

  • Let's use some mathematics to reduce code.

    The width of pyramid is rows*2, the width of row is rows*2*triangle.

    Second loop will go through all the row. The decision on what to draw can be calculated as a reminder of division by period and compared with (row number * 2)

    #include <iostream>
    using namespace std;
    
    int main() {
        int rows, triangle;
        cout << "Enter number of rows: "; cin >> rows;
        cout << "Enter number of triangles: "; cin >> triangle;
    
        cout << endl;
        rows--;
        for (int i = 0; i < rows; i++) {
            for (int k = 0; k < rows * triangle * 2; k++) {
                cout << (((k + i + rows) % (rows * 2) < i * 2 + 1) ? '*' : ' ');
            }
            cout << endl;
        }
    
        return 0;
    }
    

    Result:

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

    If pyramids have to be in touch, just change '<' to '<=' in both loops.

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