Home > OS >  How to properly increment this 'half-diamond' shape?
How to properly increment this 'half-diamond' shape?

Time:01-21

I am trying to get the slashes to form a half-diamond type of shape, but I cannot seem to get the incrementation correct. I currently have:

else if (menuOption == 2) {
    
    
    int numberOfDolls = 0;
    cout << "Number of dolls -> ";
    cin >> numberOfDolls;

      
    
    for (int i = 1; i <= numberOfDolls; i  ) {
      for (int j = 1, n = numberOfDolls; j <= i; j  ) 
        cout << setw(n--) << '/' << endl;
      
      for (int k = 1, s = numberOfDolls; k <= i; k  )
        cout << setw(s  ) << '\\' << endl;
        
        
        cout << setw(numberOfDolls   1) << "-" << endl;
      
    
    } 


}

This yields the following if the user, let's say, inputs 3:

  /
  \
   -
  /
 /
  \
   \
   -
  /
 /
/
  \
   \
    \
   -

It should look like:

  /
  \
   -
  /
 /
 \
  \
   -
  /
 /
/
\
 \ 
  \
   -

I would greatly appreciate any help as I am new to C .

CodePudding user response:

    int numberOfDolls = 0;
    int deltaIndent = 0;
    cout << "Number of dolls -> ";
    cin >> numberOfDolls;



    for (int i = 1; i <= numberOfDolls; i  ) {

        deltaIndent = numberOfDolls - i;
        for (int j = i; j > 0 ;j--)
            cout << setw(j   deltaIndent) << '/' << endl;

        for (int j = 1; j <= i ; j  )
            cout << setw(j   deltaIndent) << '\\' << endl;

        cout << setw(numberOfDolls   1) << "-" << endl;
    }

output

Number of dolls -> 4
   /
   \
    -
   /
  /
  \
   \
    -
   /
  /
 /
 \
  \
   \
    -
   /
  /
 /
/
\
 \
  \
   \
    -
  •  Tags:  
  • Related