I don't understand the use of std:: setw
I'm doing an exercise from a C++ book that I use to learn the language, and I can't quite understand the use of std::setw and its joint use with std::left (by default) and std::right. In fact, I do not know how, I have come up with the key to perform 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 unfold the first triangle of *.
- the Line cout causes the b's to be separated from the * in each line to the right of each *, in 16-I spaces, starting from the character immediately to each *.
- the for z Loop adds the Triangle composed of letters b.
- the instruction cout is where I get lost, because I could not get that result that is the one I want, and do not tell me why, if it was intuition or that, but gave me to put the "* 2 " and so that came out xd
Please, I know it's a lot to ask, but if anyone could explain to me why it works and why I struggle so much with that instruction 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
2 answers
If I understood your question cout << right << setw(4 + i * 2);
forms the pyramid of spaces in between because the space you increment to 4
by the sum of i
is doubled, think that if you had this statement cout << right << setw(4 + i);
Your result would be something like:
* 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 b
and a
is doubled, when i = 1
will leave 6 spaces, in the next line will leave 8
and so progressively.
You have the loop i
that serves to print the rows. For each row you will print a number of asterisks equal to the position of The Row (Row 1 -> 1 asterisk, Row 2 -> 2 asterisks, ...).
The next element to print is the sequence of b
. As you want this new sequence to start in always in the same column you have to add after the sequence of asterisks a number of spaces such that número de asteriscos + número de espacios = x
or, in other words, 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
are printed in such a way that the gap between them forms a pyramid of spaces, occupying the total sequence 24 characters. Both sequences, a
and b
, evolve inversely proportional to the row number. Thus 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 based on the row number:
espacios = 24 - 2*(10 - i)
espacios = 24 - 20 + 2*i
espacios = 4 + 2*i
If you compare the latter result with the second setw
you will see that the equation is exactly the same: setw(4 + i * 2);
Why 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
, accordingly, the number of spaces will have to grow by two units between a row and the next if you want the total sequence to occupy the same number of characters.