Concentric Rectangular Pattern In C++ - C++ Programming Tutorial
C++ Course / Design Patterns / Concentric Rectangular Pattern In C++

Concentric Rectangular Pattern In C++

BLUF: Mastering Concentric Rectangular Pattern In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Concentric Rectangular Pattern In C++

C++ is renowned for its efficiency. Learn how Concentric Rectangular Pattern In C++ enables low-level control and high-performance computing in the tutorial below.

The term "concentric" describes a configuration where multiple spheres, circles, or other forms are positioned around a central point, with each successive form encompassing the one before it. To maintain equilibrium and symmetry along a central axis, elements are typically arranged in a layered or hierarchical fashion, which is the primary focus of this term. Concentric configurations are commonly observed in natural settings, artistic expressions, and architectural designs, often symbolizing equilibrium, entirety, and unity. This principle finds practical uses in various fields such as biology, engineering, and architecture, where organizing structures around a central feature can lead to beneficial functional or visually appealing outcomes.

A concentric rectangular arrangement is a visual pattern frequently employed in software development, involving multiple rectangles of varying dimensions positioned around a central point. This design is commonly utilized in various fields such as graphical user interfaces (GUIs), data representation, and gaming development to enhance visual appeal and structural organization.

The idea involves arranging rectangles in a way that creates a series of nested structures similar to tree rings. At the heart of this pattern is the central rectangle, surrounded by subsequent rectangles that grow in size progressively. Calculations were made for the dimensions, positions, and distances from the center of each rectangle to achieve this specific pattern.

There are numerous methods to apply the pattern. One approach involves utilizing two inputs for specifying rows and columns, while another method entails receiving a single input to display the pattern of rows and columns, with the input digit serving as the outermost boundary for the rectangle.

1. The pattern which takes two inputs for rows and columns:

Example

#include <iostream>
int main() {
    int numRows, numCols;
    std::cout<< "Enter the number of rows: ";
    std::cin>>numRows;
    std::cout<< "Enter the number of columns: ";
    std::cin>>numCols;
    for (int i = 0; i<numRows; ++i) {
        for (int j = 0; j <numCols; ++j) {
            int distanceToTop = i;
            int distanceToBottom = numRows - 1 - i;
            int distanceToLeft = j;
            int distanceToRight = numCols - 1 - j;
            int minDistance = std::min(std::min(distanceToTop, distanceToBottom),std::min(distanceToLeft, distanceToRight));
            if (j != 0) {
                std::cout<< " ";
            }
            char character = '1' + minDistance;
            std::cout<< character;
        }
        std::cout<< std::endl;
    }
    return 0;
}

Output:

2. Print the pattern, which takes only one input for rows and columns:

Example

#include <iostream>
using namespace std;
int main() {
    int size;
cout<< "Enter the size of the rectangle: ";
cin>> size;
    for (int i = 0; i< size; i++) {
        for (int j = 0; j < size; j++) {
            int distanceToCenter = max(abs(i - size / 2), abs(j - size / 2));
cout<<distanceToCenter + 1 << " ";
        }
cout<<endl;
    }
    return 0;
}

Output:

3. Printing the pattern in a way that only the input will be the border:

Example

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int n;
cout<< "Enter a number: ";
cin>> n;
    int size = 2 * n - 1;
    for (int i = 0; i< size; i++) {
        for (int j = 0; j < size; j++) {
            int num;
            if (i<= size / 2) {
num = n - min(min(i, size - 1 - i), min(j, size - 1 - j));
            } else {
num = n - min(min(size - 1 - i, i), min(j, size - 1 - j));
            }
cout<<num<< " ";
        }
cout<<endl;
    }
    return 0;
}

Output:

Explanation:

  • To print the above pattern, we need to understand that the matrix has '2n - 1' rows and '2n - 1' columns , and it is split into two halves: the top half has rows from '0 to floor((2n - 1)/2)' , while the bottom half has rows from 'floor((2n - 1)/2) + 1' to '2*n - 2' . The pattern for each row is composed of a falling sequence, a constant sequence , and a rising sequence .
  • A sequence of loops and fundamental arithmetic operations are used to alter the numbers printed in each cell to carry out the reasoning. The symmetry of the top and bottom halves , with the exception of the central row, makes the procedure simpler.
  • The top-half approach entails managing the pattern by performing a loop from row 0 to 'floor((2*n - 1)/2)' and computing the numbers in accordance with the stated logic. While the second section employs the constant "n - i" , the first and third parts use decreasing and ascending sequences. The result of this is the upper portion of the design.
  • The bottom half solution employs a loop from "floor((2*n - 1)/2)" down to 0 to build the pattern in the lower half and produce a symmetrical and attractive design .

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience