Why do i have to add to the counter when using for loop to perfectly get result?

estew estewh at gmail.com
Thu May 30 22:44:41 PDT 2013


Well, after a quick glance at the code you're iterating N times 
but only printing N-1 times. When counter == peak the loop does 
nothing so you'd get something like:

chosen=5
for(int counter = 0; counter < chosen ; ++counter){ // note +1 
removed }

counter = 0, 1, 2, [3]NO_PRINT, 4
   *0
  ***1
*****2
  ***4

(note: my ascii art, I assume this is what the code would 
produce...)
Now add the +1
for(int counter = 0; counter < chosen +1 ; ++counter){
0, 1, 2, [3]skip, 4, 5
   *0
  ***1
*****2
  ***4
   *5

With your implementation as it stands you need to skip iteration 
N/2+1 to get the correct output. But you still need to write N 
lines.

Hope that makes sense...!! :D
Stewart


More information about the Digitalmars-d-learn mailing list