samedi 4 septembre 2021

C++ Pattern with a loop [closed]

How can I print this pattern in sections like this Here is my code

int numberOfSections = 0;
    cout << "Number of sections -> ";
    cin>> numberOfSections;
    int p, k, mid;
    int x= 2 * numberOfSections+2;
    mid = (x/2);

for(p = 1; p<= mid; p++) {
  for(k = 1; k<=(mid-p); k++) {
     cout << " ";
  }
  if(p == 1) {
     cout << "---";
  }else{
     cout << "("; 
     for(k = 1; k<=2*p-1; k++) { 
        cout << " ";
     }
     cout << ")";
  }
  cout << endl;
  }
  for(p = mid+1; p<x; p++) {
  for(k = 1; k<=p-mid; k++) {
     cout << " ";
  }
  if(p == x-1) {
     cout << "";
  }else{
     cout << "(";
     for(k = 1; k<=2*(x - p)-1; k++) { 
        cout << " ";
     }
     cout << ")";
  }
  cout << endl;
}

Thats my output I want that to be printed like this in to sections incrementing by 2

Aucun commentaire:

Enregistrer un commentaire