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