lundi 9 avril 2018

Drawing an arrow pattern using nested loops

So i am trying to draw this pattern using nested loops The pattern

i managed to do it with a static width using this code

for (int i = 1; i <=7; i++) {
    for (int j = 1; j <=12; j++) {
        if (i == 1 || i == 7) {
            if (j == 1 || j == 2 || j == 3 || j == 9) {
                cout << "* ";
            }
            else cout << "  ";
        }
        if (i == 2 || i == 6) {
            if (j == 1 || j == 2 || j == 3 || j == 9 || j == 10) {
                cout << "* ";
            }
            else cout << "  ";
        }
        if (i == 3 || i == 5) {
            if (j == 12) {
                cout << "  ";
            }
            else cout << "* ";
        }
        if (i == 4) {
            cout << "* ";
        }
    }
    cout << endl;
}

but now i am asked to let the user enter the arrow width he wants and i can't think of it at all how can i manage to do it ?

Aucun commentaire:

Enregistrer un commentaire