I have some code:
This code does not work but I think it gets the idea across. I am trying to get an output in a monospaced text field to look like this.
This is apparently not a trivial output. Any help is appreciated.
I think you can't build the formatted string untils you're done generating the whole triangle.
1 2 1
Does anyone have any suggestions for making it more succinct?
Campbell Ritchie wrote:Is it difficult? If you have numbers every 5 spaces, then the nth row will require (n - 1) * 5 spaces across.
So your padding for the 1st row would be (n - 1) * 5 / 2 spaces.
I don't think it's that simple. Eventually, you will get to rows where the numbers are bit long ..i.e. 283774. you need to take into account how wide the numbers you are displaying will be as well, and that is not a simple to compute.
I take your point, Fred. Maybe the %d tag with a width? But that won't put the numbers neatly in the middle of their columns. One way to compute width is with the log of the number.
fred rosenberger wrote: . . . to take into account how wide the numbers you are displaying will be . . .