Professional Documents
Culture Documents
be/8S603d65WXw
Visual aids used:
To tackle this problem, this method makes use of 2 for loops, one nested.
The formula
n*(n + 1)/2
gives the total spaces needed
n = 1 n = 2 n = 3 n = 4
Knowing how, we’ll use the initial for loop to calculate our limit and the nested
one to write in the array. Thus, the algorithm works as next:
0) Initializes required local variables
1) Sets the value of our variable that needs to be assigned to 1.
2) Calculates the index where we must stop the sequence (limit)
3) Assigns to the array the value according to the sequence in the position
referenced by the super for loop index
4) Increases the value that will be stored (since the sequence has
consecutive numbers)
5) Increases the subindex reference by one to move forward through the
array.
If the actual referencing index (index + subindex) is minor than the
index were the sequence must repeat, repeats 3 and 4.
If not, follows next step.
6) Increases the index modifier to calculate the next limit.
7) Assigns to the index the value of the limit to write in the array from that
index reference.
8) Repeats 1 - 7 until the modifier is greater than the parameter (the
sequence is completed).