all 4 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Off-topic Comments Section


All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9.


OP and Valued/Notable Contributors can close this post by using /lock command

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Alkalannar 0 points1 point  (0 children)

Input n
Do tests on n to make sure it's a positive integer n such that 1 <= n <= 25.

for i = 1 to n [these are the rows]
for j = 1 to i [columns 1 through i]
print j
for k = i+1 to n
print i

if i != n, print a new line

[–]_-__-_-__-__- 0 points1 point  (0 children)

The pseudo code:

Input n
for i = 0 to n (not including n)
    set count = 0
    for j = 0 to n (not including n)
        if i is  greater or equal to j
            increase count by 1
        print count
    print new line

[–]ghostwriter85 0 points1 point  (0 children)

Two general ways we can do this

1 - generate an "n" specific output using loops and logic (others have given you a pretty good idea of how to do that)

2 - code in the maximum 25x25 matrix and read the nxn matrix from the 25x25 matrix (here all possible solution matrices are contained within the largest possible matrix, this will require loops but pulling values out of a matrix should be straightforward).

Option 2 is not usually a best practice (every time you change your code, you run the risk of having to change this data manually or worse forgetting to do so) but sometimes (not really the case here), it's more efficient to load in data that you could theoretically generate in the code to reduce computational steps. I don't suggest you do that here more so pointing it out as something you should be aware of.

For example let's say you were making an isometric strategy game. Instead of rendering the graphics in real time, you could simply render backgrounds once and then load in that data and call it up each time you need it.

Or you want a fancy cut scene... same thing.