https://www.youtube.com/watch?v=Jtu6dJ0Cb94
i.e. move top left 0, 0 to top right 0, 1 etc until values are arranged in column format
0 | 1 |
---|---|
2 | 3 |
2 | 0 |
---|---|
3 | 1 |
0 | 1 | 2 |
---|---|---|
3 | 4 | 5 |
6 | 7 | 8 |
0 | 1 | 2 |
---|---|---|
3 | 4 | 5 |
However things become tricky when asked to complete a matrix that is > 3 X 3 since there are more then 3 edges which does not evenly correspond to the number of swap values therefore layers are introduced to break up the cartesian plan of subsets using an array arithemtic variable totNumofLevels this evenly divides MATRIX_SIZE / 2 ensuring that sets of contingous values will be divided into subsets to match swap values with outer edges
note: I slightly extended this work by populating 2D array inside of a function to modularise a bit. also for C++ I used a static array that was fixed in size by using the const MATRIX_SIZE
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.37)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
g++ -o mattrix2 mattrix2.cpp
./mattrix2
------------------- Original Matrix --------------
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
-------------- Rotated Matrix ----------------
12 8 4 0
13 9 5 1
14 10 6 2
15 11 7 3
grep const mattrix2.cpp
const int MATRIX_SIZE = 4;