redbarron23 / matrix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rotate by 90 degrees

Synopsis is to rotate a N x N Matrix using swap function

this was heavily borrowed from youtube video

https://www.youtube.com/watch?v=Jtu6dJ0Cb94

key is to move values in array clockwise until rotated 90 degrees

i.e. move top left 0, 0 to top right 0, 1 etc until values are arranged in column format

2 X 2 matrix before

01
23

2 X 2 matrix after

20
31

3 X 3 matrix before

012
345
678

3 X 3 matrix after

012
345

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

directions to compile use latest version of gcc

$ 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

Directions to compile source into executable

g++ -o mattrix2 mattrix2.cpp

Directions to run

./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

To change number of rows/cols

Note the default is 4

grep const mattrix2.cpp
const int MATRIX_SIZE = 4;

About


Languages

Language:C++ 100.0%