+ 1

Need an explanation to this program??

https://code.sololearn.com/c009g6kuu6yI/?ref=app

21st Apr 2021, 2:09 AM
Sindhuja Selvakumar
Sindhuja Selvakumar - avatar
2 odpowiedzi
+ 2
So basically you can see that the codes consist of three parts, one for each loop The first for i loop is for every layer, I guess you understand that The second for j loop is the main thing, let's loop it manually here Let's say x = 8, i = 0 (top layer) First loop: x - i - 1= 7, so it prints 7 spaces followed by 1 * Second loop: x - i - 1= 6, so it prints 6 spaces followed by 2 * As you can see, the number of spaces printed decreases by 1 as it goes down, that's exactly what we want! The pyramid should extend one space to the left everytime it goes down So up to this point, you may wonder why is it x - i - 1 instead of other things, so the 1 is not that important, if you remove it, it'll just print 1 space to right extra (so pyramid kinda shifts to the right a bit) You should first understand this pyramid * *** ***** ******* ********* *********** The pyramid in your codes is a variant of this pyramid, this pyramid has a height of x = 6, and a width of 13 (2x +1)
21st Apr 2021, 4:01 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
So in the first layer, the spaces printed must be (13 - 1) /2 = 6, which is exactly x, that's why you print x spaces for first layer, and decrease it every layer after
21st Apr 2021, 4:02 AM
Hoh Shen Yien
Hoh Shen Yien - avatar