Array - Diagonal

Hello all,

I have a home assignment to write a short C++ program that would display an array of n columns and lines (square array) whose values would be distributed as follows:

1 1 1 ... 1
1 2 2 ... 2
1 2 3 ... 3
..... ...
..... ...
1 2 3 ... n

I know it has to be two loops. What I don't know is the exact algorithm. I'm guessing it has something to do with the diagonal... But how do I program it to put the values as above?

Please advise. Any input much appreciated :)
What I don't know is the exact algorithm.

Then invent an algorithm ;)

i'll help you
1
2
3
4
5
6
for(int i = 0; i < n; ++i)
{
    for(int j = 0; j < n; ++j)
    {
    }
}
Thank you, Gamer2015. I know it has to be two loops, but what's inside of them?

1 1 1 1 1
1 2 2 2 2
1 2 3 3 3
1 2 3 4 4
1 2 3 4 5

I want it to look like the above...

So, the values in the first row would be (j+1). What about the second and following rows?
So, the values in the first row would be (j+1)
I think in your case you should loop from 1 to including n, so you don't have to care about the +1

looking at this I'd say it is the smaller one of row and column-number
Last edited on
I'd say it is the smaller one of row and column-number.


Thank you! This is what I was missing! I'll try to put it together in the evening and come back to you if I have any other problems.
Topic archived. No new replies allowed.