How can I print this pattern [closed]

Problem statement:

You are given an integer n, representing the side length of a rhombus. Your task is to write a program to print the pattern of the sides and diagonals of the rhombus.

INPUT
The only line of the input contains a single integer n, representing the side length of a rhombus.
OUTPUT
Print the diagonals and sides of a rhombus.

Sample Input: 5
Sample Output:

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

I tried this code but I’m unable to print the vertical bar of Rhombus.

#include <iostream>
using namespace std;

int main() {
    int n;
    
    cin >> n;

    // Upper half of the rhombus
    for (int i = 1; i <= n; i++) {
        // Print spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print asterisks and spaces
        for (int j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == n) {
                cout << "*";
            } else {
                cout << " ";
            }
        }

        cout << endl;
    }

    // Lower half of the rhombus
    for (int i = n - 1; i >= 1; i--) {
        // Print spaces
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }

        // Print asterisks and spaces
        for (int j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == 1) {
                cout << "*";
            } else {
                cout << " ";
            }
        }

        cout << endl;
    }

    return 0;
}

5

A rhombus is a a quadrilateral all of whose sides have the same length.

The definition does not say anything about the angle sizes, so that may be varied, but I shall assume that its “top” will be in the middle and the sides open downward by increasing/decreasing their position by 1 each time. So you have three parts:

  • first part
  • middle section
  • second part

Input is N

So the first question is: how long will be the middle section?

Since we have N lines in the first part, that means that we have 1 (the middle) + N (left) + N (right). Therefore the answer is 2N + 1

Hence, if we start coordinates from 0, then the middle is N characters away.

Let’s therefore draw the first half:

for (int i = 0; i < N; i++) {
    int left = N - i;
    int right = N + i;
    for (int j = 0; j < 2 * N + 1; j++) {
        cout << ((i == left) || (i == right) || (i == N)) ? '*' : ' ';
    }
    cout << endl;
}

Now, let’s draw the middle (this will be easy):

for (int i = 0; i < 2 * N + 1; i++) {
    cout << '*';
}
cout << endl;

And let’s do it for the bottom too:

for (int i = 0; i < N; i++) {
    int left = i;
    int right = 2 * N - i;
    for (int j = 0; j < 2 * N + 1; j++) {
        cout << ((i == left) || (i == right) || (i == N)) ? '*' : ' ';
    }
    cout << endl;
}

Basically we are consistently displaying on the specified lines. Code was untested, please let me know if anything does not click.

If you index the rows and columns from -n+1 to n-1, then the stars appear when either coordinate is 0, or the sum or difference of the two coordinates is n-1 or -n+1. Yielding this relatively concise code:

#include <iostream>
using namespace std;

int main() {
    int n = 8;
    --n;
    for (int i = -n; i <= n; i++) {
        for (int j = -n; j <= n; j++) {
            cout << ((i == 0 || j == 0 || abs(i - j) == n || abs(i + j) == n) ? "*" : " ");
        }
        cout << "n";
    }
    cout << "n";
}

The code uses a fixed n, but you can obviously change it to respond to input.

Using meaningful names for your variables would help. We can think of left, mid and right, each representing a position for an asterisk on a line. The middle line will be exception to this, having a solid line.

A recursive solution comes to mind, where before and after each recursive call you print the same line. The base case is when left is 1, and then a solid line is printed:

void line(int left, int mid, int right) {
    for (int i = 1; i <= right; i++) {
        std::cout << (left == 1 || i == left || i == mid || i == right ? '*' : ' '); 
    }
    std::cout << "n";
}

void rhombus_slice(int left, int mid, int right) {
    line(left, mid, right);
    if (left <= 1) return;
    rhombus_slice(left - 1, mid, right + 1);
    line(left, mid, right);
}

int main() {
    int n = 5;
    
    rhombus_slice(n, n, n);
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật