Asked By
stefen
0 points
N/A
Posted on - 07/22/2011
Â
Hello! I want to create a PYRAMID of digits using LOOPS(nested). But I don’t know how to use nested loops. As I always receive an ERROR. Anybody will please help in designing this code. And also tell me the rules how simple loops differ from nested loops
                           1
                          232
 34543
4567654
       567898765
      67890109876
                                        7890123210987
     890123454321098
    90123456765432109
   0123456789876543210
Create pyramid output using nested loops
Nested loops are basically a loop in a loop.  It is kind of like circuit training done by athletes.  They run to one station do a bunch of repeated actions go to the next station and do another bunch of repeated actions until they reach the last station and start all over again.
So in the case of nested loops, the outer loop triggers the inner loop to do a bunch of things. Â When the inner loop finishes, it exits, and the outer loop once again trigger it. Â This carries on until the outer loop's conditions is satisfied.
When coding nested statements, it is very important to practice good indentation. Â With good indentation, you will have a good idea in which nest you are working on.
For every programming problem, it is just a collection of small problems. Â The difficulty is identifying the small problems. Â In this case, you will have to build a pyramid of 10 levels (1st problem). Â In each level, the 1st and last number will be one up of the previous level's 1st number(2nd problem). Â And the level have to be populated by increasing number from both ends till the center of the line(3rd problem).
So in this case we are looking at a loop in a loop. Â The outer loop will handle the building of the levels, while the inner loop will handle the populating of the level.
If one will to notice, the size of the level is also 2 more then the previous level. Â Which will mean the center position of the level is always (Size of previous level + 2) / 2 + 1.
So here is the pseudo-code for this problem:
Â
Loop ( level > 10 ) {
current start = Previous level start + 1
Loop ( end of level? ) {
is more then center
current–;
if no
current++;
}
}
Â
Of course this is just an extremely simplified version. Â You will have to convert it into actual code.
Â