mercredi 3 mars 2021

(C++) Input specific pattern accoring to user input

I have a line of code that when inputted 3, the result will print out a series of dashes and asterisks to form a diamond:

Expected Input:

3

Expected Output:

--*--
-***-
*****
-***-
--*--

what i have so far is the triangle but I can' seem to get rid of the middle line to make it a full diamond shape. Also "-" is not printing on the right side of the bottom half

this is the code I have made

int n;
cin >> n;
for (int left_stars = 0; left_stars < n; left_stars++) {
    for (int column = 0; column < 2 * n - 1; column++) {
        int first_star = n - 1 - left_stars;
        int last_star = n - 1 + left_stars;
        if (column < first_star || column > last_star) {
            cout << "-";
        } else {
            cout << "*";
        }
    }
    cout << endl;
}

for(int i = n; i >= 1; --i) {
    for(int space = 0; space < n-i; ++space) {
        cout << "-";
    }
    for(int j = i; j <= 2*i-1; ++j) {
        cout << "*";
    }
    for(int j = 0; j < i-1; ++j) {
        cout << "*";
    }
    cout << endl;
}
return 0;

Aucun commentaire:

Enregistrer un commentaire