1 Attachment(s)
Please help with displaying 2D array
I got assigment at my school to display 2D array like this:
This is default array:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Now I have to print the default array like this:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
I have tried some codes to do it myself but i had no succes.
Please can you give information what algorithm to use, to make this program work:
Thank you very much!!!
Re: Please help with displaying 2D array
Well, every zig-zag in the picture is basically the same proceedure, just from a different starting point. That indicates how the outer loop should be defined, eg, the first iteration of the outer loop sets 1-3, the second sets 4-10, the third sets 11-21, etc.
Within the outer loop, it will probably be easiest to use two separate inner loops, one for traversal in each direction.
Within each inner loop, it's just a matter of incrementing the indexes appropriately and writing the next number.