cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2011 · EasyPatterns & output

A Dotted Triangle

National Olympiad — Municipal round (2011 season)

Submission name: fig.cpp

Problem summary

Print a left-aligned triangle of n rows. Its boundary consists of stars and its interior of dots. Separate adjacent symbols by exactly one space.

Input

One integer n.

Output

The n-row triangle.

Constraints

3 < n < 20.

Worked example

An independently prepared example for this explanation.

Input

5

Output

*
* *
* . *
* . . *
* * * * *

Solution analysis

  1. Row r contains exactly r symbols. A symbol is on the boundary if it is the first or last in its row, or the row is the final row.
  2. For each position print a star on the boundary and a dot otherwise. Print a separator before every symbol except the first, avoiding trailing spaces.
  3. With n = 5, row 4 is * . . * because its two middle positions are internal. Row 5 contains only stars because it is the bottom edge.

Why it works

The nested loops visit exactly the positions of the triangular shape. The boundary condition identifies its left, right, and bottom edges, so each position receives the required star or dot. The separator rule places exactly one space between symbols.

Complexity: O(n²) time and O(1) extra space.

Watch out. The entire bottom row is boundary. Add spaces between symbols, not after the last symbol.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int row = 1; row <= n; row++) {
        for (int column = 1; column <= row; column++) {
            if (column > 1) {
                cout << ' ';
            }
            // The boundary consists of both sloping edges and the bottom row.
            bool boundary = column == 1 || column == row || row == n;
            cout << (boundary ? '*' : '.');
        }
        cout << '\n';
    }
}

Source: Arena Olimpiici ↗