catfish666 can you help me with the last part? |
If you keep asking
me specifically for help, what you do is alienate
everybody else. This means they won't bother to help you, which is detrimental
to you.
Now back to the subject, I have a small observation: in your code (which you deleted) I noticed that you mixed up the columns and lines.
Your code works fine though, because you used the same
N for both width and height. Otherwise nasty things may have happened.
Here is the "traditional" way to go through a 2D array (notice which
for() loop iterates what):
1 2 3 4 5
|
int arr[3][10];
for (int i=0; i < 3; ++i)
for (int j=0; j < 10; ++j)
arr[i][j] = 0;
|
So you have a 2D array and you need to do a weird diagonal sorting kind of thing.
I'd suggest simplifying things: copy all the contents of the array into a vector, sort the vector, then copy the now sorted contents of the vector back into the array.
Copying to a vector and sorting it is the easy bit.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <algorithm>
#include <vector>
// ...
std::vector<int> arr_contents;
for (int i=0; i < N; ++i)
for (int j=0; j < N; ++j)
arr_contents.push_back(arr[i][j]);
std::sort(arr_contents.begin(), arr_contents.end());
|
Now we need to copy from
arr_contents back to
arr in a certain way.
|0 1 2
-+-----
0|a b d
1|c e g
2|f h i
a[0][0]
b[0][1]
c[1][0]
d[0][2]
e[1][1]
f[2][0]
g[1][2]
h[2][1]
i[2][2] |
Do you see some patterns? As in, some kind of shift happening between the coordinates?
a[0][0] no shift
b[0][1] shift: i+1, j-1
c[1][0] stop
d[0][2] shift: i+1, j-1
e[1][1] shift: i+1, j-1
f[2][0] stop
g[1][2] shift: i+1, j-1
h[2][1] stop
i[2][2] no shift
|
Figure out why the shifting stops (should be obvious).
Then figure out how you move to the next starting element from where the shifting begins. Starting elements are: a, b, d, g, i.
Hint: check if in current starting element
arr[i][j],
j equals N-1.