[Solved] pattern drawing programming logic [closed]


A C++ implementation :

Without any formatting

void print(int n) {
    for(int i=n, cl=1, cr=n*n+1; i>0; cl+=i, --i, cr-=i) {
        for(int j=0; j<i; ++j) cout << cl+j;
        for(int j=0; j<i; ++j) cout << cr+j;
        cout << endl;
    }
}

With dashes and stars

void print(int n) {
    for(int i=n, cl=1, cr=n*n+1; i>0; cl+=i, --i, cr-=i) {
        for(int j=0; j<n-i; ++j) cout << "--";
        for(int j=0; j<i; ++j) cout << cl+j << "*";
        for(int j=0; j<i; ++j) cout << cr+j << (j-i+1?"*":"");
        for(int j=0; j<n-i; ++j) cout << "--";
        cout << endl;
    }
}

1

solved pattern drawing programming logic [closed]