Have a look at my post over here:
http://www.cplusplus.com/forum/general/200987/
----0
---1-2
--3-4-5
-6-7-8-9
0-1-2-3-4
|
To get the spaces before the first number on each row, do
spaces = total_rows - n - 1
, where n is the current row number, with the first row being 0. For example, total_rows = 5, n = 0, therefore spaces = 5 - 0 - 1, which is 4. If we look above, we can see that there are 4 spaces on the first row.
Note that if n > 0, that is from the second line onward, there are spaces in between each number. Second row = 1, third row = 2, fourth row = 3 etc... The number of spaces between each number is actually equal to n, the row number(zero indexed).
We can see that as the number reaches 9, it is reset to 0. Note that
s = s-10;
won't work for numbers greater than 19. A simple way to fix this is to do
s %= 10
.