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 E2026 · EasyNested loops & patterns

Draw a Rectangle

National Olympiad — National round (2026 season)

Submission name: paintrectangle.cpp

Problem summary

Print an N-by-N square with the given border character. Inside it, draw the main diagonal with a backslash and the other diagonal with a slash. If both diagonals share an interior cell, print uppercase X there. Other interior cells are spaces.

Input

An integer N and a visible, non-whitespace border character.

Output

Exactly N rows of N characters.

Constraints

1 ≤ N < 80; the border character is any visible keyboard character.

Worked example

An independently prepared example for this explanation.

Input

5 #

Output

#####
#\ /#
# X #
#/ \#
#####

Solution analysis

  1. Number rows and columns from 0 to N−1. A cell is on the border when its row or column is 0 or N−1. Border cells take priority over diagonal cells.
  2. An interior cell belongs to the main diagonal when row = column, and to the other diagonal when row + column = N−1. Test whether both conditions hold before testing either separately.
  3. For N = 5, the two diagonals share cell (2,2), which becomes X. For even N they cross between cells and no X is printed. For N = 1 or 2 there is no interior to fill.

Why it works

Every printed cell is classified by the exact equations defining the border and diagonals. Checking the border first prevents the diagonal symbols from replacing its corners. The nested loops visit every cell once in output order.

Complexity: O(N²) time, which is necessary to output N² characters; O(1) extra space when characters are printed directly.

Watch out. A C++ backslash character must be written as a double backslash inside the character literal. Preserve interior spaces. Print X before either individual diagonal case. Do not draw diagonals over the border.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main() {
    int size;
    char border;
    cin >> size >> border;
    for (int row = 0; row < size; row++) {
        for (int column = 0; column < size; column++) {
            bool onBorder = row == 0 || column == 0 || row == size - 1 || column == size - 1;
            bool mainDiagonal = row == column;
            bool otherDiagonal = row + column == size - 1;
            // The border wins even where a diagonal reaches a corner.
            if (onBorder) {
                cout << border;
            } else if (mainDiagonal && otherDiagonal) {
                cout << 'X';
            } else if (mainDiagonal) {
                cout << '\\';
            } else if (otherDiagonal) {
                cout << '/';
            } else {
                cout << ' ';
            }
        }
        cout << '\n';
    }
}

Source: Arena Olimpiici ↗