Advertisement
Guest User

SoftUni Chalange | RotateMatrix | Procedural

a guest
Apr 13th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Procedural
  5. */
  6.  
  7. $n = 3;
  8. $matrix = [
  9.     [1, 2, 3],
  10.     [4, 5, 6],
  11.     [7, 8, 9],
  12. ];
  13.  
  14. function rotate(&$matrix, $n)
  15. {
  16.     $result = [];
  17.  
  18.     for ($col = 0; $col < $n; $col++) {
  19.         $currRow = [];
  20.         for ($row = $n - 1; $row >= 0; $row--) {
  21.             $currRow[] = $matrix[$row][$col];
  22.         }
  23.         $result[] = $currRow;
  24.     }
  25.  
  26.     $matrix = $result;
  27. }
  28.  
  29. function renderMatrix($matrix, $n)
  30. {
  31.     for ($row = 0; $row < $n; $row++) {
  32.         for ($col = 0; $col < $n; $col++) {
  33.             echo $matrix[$row][$col] . ' ';
  34.         }
  35.         echo '<br />';
  36.     }
  37. }
  38.  
  39. // Show initial matrix
  40. renderMatrix($matrix, $n);
  41. echo '<br />';
  42.  
  43. // One rotation
  44. rotate($matrix, $n);
  45. renderMatrix($matrix, $n);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement