# I do not understand the use of std :: setw

5

I'm doing an exercise in a C ++ book that I use to learn the language, and I can not fully understand the use of std :: setw and its use in conjunction with std :: left (default) and std :: right . In fact, I do not know how, I've found the key to do what I wanted to do, but I do not understand why it works ..

Tell me if I'm wrong, please, but according to what I think:

• The for j loop causes the first triangle to unfold *.
• The cout < < right < < setw (16 - i); causes the b's to be separated from * in each line to the right of each *, in 16 -i spaces, starting from the immediate character to each *.
• The for z loop adds the triangle consisting of letters b.
• The cout < < right < < setw (4 + i * 2); is where I already get lost, because I did not get that result that is what I want, and do not tell me why, if it was intuition or what, but I thought put the "* 2" and so if it came out xD

Please, I know it's a lot to ask, but if someone could explain to me why it works and why I messed with that instruction so much, I would greatly appreciate it.

``````    #include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;
using std::right;
using std::left;

int main() {
for(int i = 1; i <= 10; i++) {
for(int j = i; j >=1; j--)
cout << '*';

cout << right << setw(16 - i);

for(int z = 11 - i; z >= 1; z--)
cout  << 'b';

cout << right << setw(4 + i * 2);

for(int y = 11 - i; y >= 1; y--)
cout << 'a';

cout << endl;
}

return 0;
}
``````

I also put the output that gives me, in case it was not portable.

``````*              bbbbbbbbbb     aaaaaaaaaa
**             bbbbbbbbb       aaaaaaaaa
***            bbbbbbbb         aaaaaaaa
****           bbbbbbb           aaaaaaa
*****          bbbbbb             aaaaaa
******         bbbbb               aaaaa
*******        bbbb                 aaaa
********       bbb                   aaa
*********      bb                     aa
**********     b                       a
``````

asked by Alejandro 02.05.2016 в 19:56
source

5

If I understood your question `cout << right << setw(4 + i * 2);` form the pyramid of spaces in the middle because the space you increase to `4` by the sum of `i` is doubled, think that if you had this instruction `cout << right << setw(4 + i);` your result would be something as:

``````*              bbbbbbbbbb    aaaaaaaaaa
**             bbbbbbbbb     aaaaaaaaa
***            bbbbbbbb      aaaaaaaa
****           bbbbbbb       aaaaaaa
*****          bbbbbb        aaaaaa
******         bbbbb         aaaaa
*******        bbbb          aaaa
********       bbb           aaa
*********      bb            aa
**********     b             a
``````

By multiplying `i` by `2` the space you leave for each line between the `b` and the `a` is doubled, when `i = 1` will leave 6 spaces, on the next line it will leave `8` and so on progressively.

source
1

You have the `i` loop that is used to print the rows. For each row you will print an asterisk number equal to the position of the row (row 1 -> 1 asterisk, row 2 -> 2 asterisks, ...).

The next item to print is the `b` sequence. Since you want this new sequence to start always in the same column you have to add after the asterisk sequence a number of spaces in such a way that `número de asteriscos + número de espacios = x` or, put another way, `número de espacios = x - número de asteriscos` , being in your case `x=16` . Since the sequence of asterisks is directly proportional to the row, the number of asterisks must be inversely proportional. From here you get the first `setw` : `setw(16 - i);` :

``````Fila i = i asteriscos + (16 - i) espacios

Fila 01: 1 asterisco  + (16-1) espacios = 16
Fila 02: 2 asteriscos + (16-2) espacios = 16
Fila 03: 3 asteriscos + (16-3) espacios = 16
...
``````

Well, now the sequence of `b` and the sequence of `a` is printed in such a way that the gap between them forms a pyramid of spaces, the total sequence occupying 24 characters. Both sequences, `a` and `b` , evolve inversely proportional to the row number. So we have:

``````24 = longitud_secuencia_b + espacios + longitud_secuencia_a
24 = (10-i) + espacios + (10-i)
24 = 2 * (10-i) + espacios
``````

And now we try to calculate the number of spaces to insert according to the number of the row:

``````espacios = 24 - 2*(10 - i)
espacios = 24 - 20 + 2*i
espacios = 4 + 2*i
``````

If you compare this last result with the second `setw` you will see that the equation is exactly the same: `setw(4 + i * 2);`

Why is that `x2` ? Because the number of elements in the sequence `a` is reduced by one in each row and the same for the sequence `b` , consequently, the number of spaces will have to grow by two units between one row and the next if you intend to the total sequence occupies the same number of characters.