Rotate Matrix Elements

Rotate Matrix Element

Here given code implementation process.

//C Program 
//Rotate Matrix Elements
#include <stdio.h>

#define ROW 5
#define COL 5

void update(int arr[][COL],int row,int col,int index)
{
  int data=arr[row][col],temp=0;
  for (int i = col-1; i >=index ; --i)
  {
    temp=arr[row][i];
    arr[row][i]=data;
    data=temp;
  }
  
  for (int i = row-1; i >=index ; --i)
  {
    temp=arr[i][index];
    arr[i][index]=data;
    data=temp;
  }
  
  for (int i = index+1; i <=col ; ++i)
  {
    temp=arr[index][i];
    arr[index][i]=data;
    data=temp;
  }
  
  for (int i = index+1; i <=row ; ++i)
  {
    temp=arr[i][col];
    arr[i][col]=data;
    data=temp;
  }
  
}
void rotate(int arr[][COL])
{
   int row=ROW, col=COL,size=COL;

  if(ROW < COL)
  {
    //When not a perfect square matrix
    //Get small size
    size=ROW;
  }
 
  for (int i = 0; i < size/2; ++i)
  {
    row--;
    col--;
    update(arr,row,col,i);
  }
}
void show_data(int arr[][COL])
{
  for (int i = 0; i < ROW; ++i)
  {
    for (int j = 0; j < COL; ++j)
    {
      printf("%3d",arr[i][j] );
    }
    printf("\n");
  }
  printf("\n");
}
int main()
{

  int arr[][COL]={ {1,  2,  3,  4 , 5}, 
    {6,  7,  8,  9 ,10},
    {11, 12, 13, 14, 15} ,
    {16, 17, 18, 19, 20},  
    {21, 22, 23, 24, 25}  
  }; 

  show_data(arr);
  rotate(arr);
  show_data(arr);
  return 0;
}


Output

  1  2  3  4  5
  6  7  8  9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

  6  1  2  3  4
 11 12  7  8  5
 16 17 13  9 10
 21 18 19 14 15
 22 23 24 25 20
/*
C++ Program
Rotate Matrix Elements
*/
#include <iostream>

#define ROW 5
#define COL 5
using namespace std;

class Matrix {
public:
  void update(int arr[][COL], int row, int col, int index) {
    int data = arr[row][col], temp = 0;
    int i = col - 1;
    while (i >= index) {
      temp = arr[row][i];
      arr[row][i] = data;
      data = temp;
      i--;
    }
    i = row - 1;
    while (i >= index) {
      temp = arr[i][index];
      arr[i][index] = data;
      data = temp;
      i--;
    }
    i = index + 1;
    while (i <= col) {
      temp = arr[index][i];
      arr[index][i] = data;
      data = temp;
      i++;
    }
    i = index + 1;
    while (i <= row) {
      temp = arr[i][col];
      arr[i][col] = data;
      data = temp;
      i++;
    }
  }
  void rotate(int arr[][COL]) {
    int row = ROW;
    int col = COL;
    int size = col, i = 0;
    if (row < col) {
      size = row;
    }
    while (i < size / 2) {
      row--;
      col--;
      this->update(arr, row, col, i);
      i++;
    }
  }
  void show_data(int arr[][COL]) {
    int i = 0;
    int j = 0;
    int row = ROW;
    int col = COL;
    while (i < row) {
      j = 0;
      while (j < col) {
        cout << "  " << arr[i][j];
        j++;
      }
      cout << "\n";
      i++;
    }
    cout << "\n";
  }
};

int main() {
  Matrix obj;
  int arr[][COL] = {
    {
      1,
      2,
      3,
      4,
      5
    },
    {
      6,
      7,
      8,
      9,
      10
    },
    {
      11,
      12,
      13,
      14,
      15
    },
    {
      16,
      17,
      18,
      19,
      20
    },
    {
      21,
      22,
      23,
      24,
      25
    }
  };
  obj.show_data(arr);
  obj.rotate(arr);
  obj.show_data(arr);

  return 0;
}

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20
/*
  Java Program
  Rotate Matrix Elements
*/
public class Matrix {

  public void update(int[][] arr, int row, int col, int index) {

    int data = arr[row][col], temp = 0;
    int i = col - 1;

    while (i >= index) {
      temp = arr[row][i];
      arr[row][i] = data;
      data = temp;
      i--;
    }
    i = row - 1;
    while (i >= index) {
      temp = arr[i][index];
      arr[i][index] = data;
      data = temp;
      i--;
    }
    i = index + 1;
    while (i <= col) {
      temp = arr[index][i];
      arr[index][i] = data;
      data = temp;
      i++;

    }
    i = index + 1;
    while (i <= row) {
      temp = arr[i][col];
      arr[i][col] = data;
      data = temp;
      i++;
    }

  }
  public void rotate(int[][] arr) {
    int row = arr.length;
    //get the size of number of column
    int col = arr[0].length;
    int size = col, i = 0;

    if (row < col) {
      //When not a perfect square matrix
      //Get small size
      size = row;
    }

    while (i < size / 2) {
      row--;
      col--;
      update(arr, row, col, i);
      i++;
    }
  }
  public void show_data(int[][] arr) {
    int i = 0;
    int j = 0;
    int row = arr.length;
    int col = arr[0].length;
    while (i < row) {
      j = 0;
      while ( j < col) {
        System.out.print("  " + arr[i][j]);
        j++;
      }
      System.out.print("\n");
      i++;
    }
    System.out.print("\n");
  }

  public static void main(String[] args) {
    Matrix obj = new Matrix();
    int [][]arr={ {1,  2,  3,  4 , 5}, 
        {6,  7,  8,  9 ,10},
      {11, 12, 13, 14, 15} ,
      {16, 17, 18, 19, 20},  
      {21, 22, 23, 24, 25}  
    }; 

    obj.show_data(arr);
    obj.rotate(arr);
    obj.show_data(arr);
  }
}

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20
/*
  C# Program
  Rotate Matrix Elements
*/
using System;
public class Matrix {

  public void update(int[,] arr, int row, int col, int index) {

    int data = arr[row,col], temp = 0;
    int i = col - 1;

    while (i >= index) {
      temp = arr[row,i];
      arr[row,i] = data;
      data = temp;
      i--;
    }
    i = row - 1;
    while (i >= index) {
      temp = arr[i,index];
      arr[i,index] = data;
      data = temp;
      i--;
    }
    i = index + 1;
    while (i <= col) {
      temp = arr[index,i];
      arr[index,i] = data;
      data = temp;
      i++;

    }
    i = index + 1;
    while (i <= row) {
      temp = arr[i,col];
      arr[i,col] = data;
      data = temp;
      i++;
    }

  }
  public void rotate(int[,] arr) {
    int row = arr.GetLength(0);
    int col = arr.GetLength(1);
    int size = col, i = 0;

    if (row < col) {
      //When not a perfect square matrix
      //Get small size
      size = row;
    }

    while (i < size / 2) {
      row--;
      col--;
      update(arr, row, col, i);
      i++;
    }
  }
  public void show_data(int[,] arr) {
    int i = 0;
    int j = 0;
    int row = arr.GetLength(0);
    int col = arr.GetLength(1);
    while (i < row) {
      j = 0;
      while ( j < col) {
        Console.Write("  " + arr[i,j]);
        j++;
      }
      Console.Write("\n");
      i++;
    }
    Console.Write("\n");
  }

  public static void Main(String[] args) {
    Matrix obj = new Matrix();
    int [,]arr={ {1,  2,  3,  4 , 5}, 
      {6,  7,  8,  9 ,10},
      {11, 12, 13, 14, 15} ,
      {16, 17, 18, 19, 20},  
      {21, 22, 23, 24, 25}  
    }; 

    obj.show_data(arr);
    obj.rotate(arr);
    obj.show_data(arr);
  }
}

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20

<?php
/*
  Php Program
  Rotate Matrix Elements
*/
class Matrix {

  public  function update(&$arr, $row, $col, $index) {
    $data = $arr[$row][$col];
    $temp = 0;
    $i = $col - 1;
    while ($i >= $index) {
      $temp = $arr[$row][$i];
      $arr[$row][$i] = $data;
      $data = $temp;
      $i--;
    }
    $i = $row - 1;
    while ($i >= $index) {
      $temp = $arr[$i][$index];
      $arr[$i][$index] = $data;
      $data = $temp;
      $i--;
    }
    $i = $index + 1;
    while ($i <= $col) {
      $temp = $arr[$index][$i];
      $arr[$index][$i] = $data;
      $data = $temp;
      $i++;
    }
    $i = $index + 1;
    while ($i <= $row) {
      $temp = $arr[$i][$col];
      $arr[$i][$col] = $data;
      $data = $temp;
      $i++;
    }
  }
  public  function rotate(&$arr) {
    $row = count($arr);
    $col = count($arr[0]);
    $size = $col;
    $i = 0;
    if ($row < $col) {
      $size = $row;
    }
    while ($i < $size / 2) {
      $row--;
      $col--;
      $this->update($arr, $row, $col, $i);
      $i++;
    }
  }
  public  function show_data(&$arr) {
    $i = 0;
    $j = 0;
    $row = count($arr);
    $col = count($arr[0]);
    while ($i < $row) {
      $j = 0;
      while ($j < $col) {
        echo("  ". $arr[$i][$j]);
        $j++;
      }
      echo("\n");
      $i++;
    }
    echo("\n");
  } 
}
function main() {
  $obj = new Matrix();
  $arr = array(array(1, 2, 3, 4, 5), 
    array(6, 7, 8, 9, 10),
    array(11, 12, 13, 14, 15), 
    array(16, 17, 18, 19, 20), 
    array(21, 22, 23, 24, 25)
  );
  $obj->show_data($arr);
  $obj->rotate($arr);
  $obj->show_data($arr);
}
main();

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20
#Ruby Program
#Rotate Matrix Elements
class Matrix 
  def update(arr, row, col, location) 
    data = arr[row][col]
    temp = 0
    i = col - 1
    while (i >= location) 
      temp = arr[row][i]
      arr[row][i] = data
      data = temp
      i -= 1
    end
    i = row - 1
    while (i >= location) 
      temp = arr[i][location]
      arr[i][location] = data
      data = temp
      i -= 1
    end
    i = location + 1
    while (i <= col) 
      temp = arr[location][i]
      arr[location][i] = data
      data = temp
      i += 1
    end
    i = location + 1
    while (i <= row) 
      temp = arr[i][col]
      arr[i][col] = data
      data = temp
      i += 1
    end
  end
  def rotate(arr) 
    row = arr.length
    col = arr[0].length
    size = col
    i = 0
    if (row < col) 
      size = row
    end
    while (i < size / 2) 
      row -= 1
      col -= 1
      self.update(arr, row, col, i)
      i += 1
    end
  end
  def show_data(arr) 
    i = 0
    j = 0
    row = arr.length
    col = arr[0].length
    while (i < row) 
      j = 0
      while (j < col) 
        print("  ", arr[i][j])
        j += 1
      end
      print("\n")
      i += 1
    end
    print("\n")
  end

end
def main() 
  obj = Matrix.new()
  arr = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
      ]
  
  obj.show_data(arr)
  obj.rotate(arr)
  obj.show_data(arr)
end
main()

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20
/*
  Node Js Program

  Rotate Matrix Elements

*/
class Matrix {
  update(arr, row, col, index) {
    var data = arr[row][col];
    var temp = 0;
    var i = col - 1;
    while (i >= index) {
      temp = arr[row][i];
      arr[row][i] = data;
      data = temp;
      i--;
    }
    i = row - 1;
    while (i >= index) {
      temp = arr[i][index];
      arr[i][index] = data;
      data = temp;
      i--;
    }
    i = index + 1;
    while (i <= col) {
      temp = arr[index][i];
      arr[index][i] = data;
      data = temp;
      i++;
    }
    i = index + 1;
    while (i <= row) {
      temp = arr[i][col];
      arr[i][col] = data;
      data = temp;
      i++;
    }
  }
  rotate(arr) {
    var row = arr.length;
    var col = arr[0].length;
    var size = col;
    var i = 0;
    if (row < col) {
      size = row;
    }
    while (i < size / 2) {
      row--;
      col--;
      this.update(arr, row, col, i);
      i++;
    }
  }
  show_data(arr) {
    var i = 0;
    var j = 0;
    var row = arr.length;
    var col = arr[0].length;
    while (i < row) {
      j = 0;
      while (j < col) {
        process.stdout.write("  " + arr[i][j]);
        j++;
      }
      process.stdout.write("\n");
      i++;
    }
    process.stdout.write("\n");
  }
}
function main() {
  var obj = new Matrix();
  var arr = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
  ];
  obj.show_data(arr);
  obj.rotate(arr);
  obj.show_data(arr);
}
main();

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  6  1  2  3  4
  11  12  7  8  5
  16  17  13  9  10
  21  18  19  14  15
  22  23  24  25  20
/*
  Swift Program

  Rotate Matrix Elements

*/
class Matrix {
  func update(_ arr: inout [[Int]] , _ row : Int, _ col: Int, _ index: Int) {
    var data: Int = arr[row][col];
    var temp = 0;
    var i: Int = col - 1;
    while (i >= index) {
      temp = arr[row][i];
      arr[row][i] = data;
      data = temp;
      i -= 1;
    }
    i = row - 1;
    while (i >= index) {
      temp = arr[i][index];
      arr[i][index] = data;
      data = temp;
      i -= 1;
    }
    i = index + 1;
    while (i <= col) {
      temp = arr[index][i];
      arr[index][i] = data;
      data = temp;
      i += 1;
    }
    i = index + 1;
    while (i <= row) {
      temp = arr[i][col];
      arr[i][col] = data;
      data = temp;
      i += 1;
    }
  }
  func rotate(_ arr: inout [[Int]] ) {
    var row: Int = arr.count;
    var col: Int = arr[0].count;
    var size: Int = col;
    var i = 0;
    if (row < col) {
      size = row;
    }
    while (i < size / 2) {
      row -= 1;
      col -= 1;
      self.update(&arr, row, col, i);
      i += 1;
    }
  }
  func show_data(_ arr: [[Int]] ) {
    var i: Int = 0;
    var j: Int = 0;
    let row: Int = arr.count;
    let col: Int = arr[0].count;
    while (i < row) {
      j = 0;
      while (j < col) {
        print(arr[i][j],terminator : " ");
        j += 1;
      }
      print();
      i += 1;
    }
    print();
  }

}
func main() {
  let obj: Matrix = Matrix();
  var arr: [[Int]] = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
  ];
  obj.show_data(arr);
  obj.rotate(&arr);
  obj.show_data(arr);
}
main();

Output

1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25 

6 1 2 3 4 
11 12 7 8 5 
16 17 13 9 10 
21 18 19 14 15 
22 23 24 25 20 
#  Php Program
#  Rotate Matrix Elements

class Matrix :
  def update(self, arr, row, col, index) :
    data = arr[row][col];
    temp = 0;
    i = col - 1;
    while (i >= index) :
      temp = arr[row][i];
      arr[row][i] = data;
      data = temp;
      i -= 1;
    
    i = row - 1;
    while (i >= index) :
      temp = arr[i][index];
      arr[i][index] = data;
      data = temp;
      i -= 1;
    
    i = index + 1;
    while (i <= col) :
      temp = arr[index][i];
      arr[index][i] = data;
      data = temp;
      i += 1;
    
    i = index + 1;
    while (i <= row) :
      temp = arr[i][col];
      arr[i][col] = data;
      data = temp;
      i += 1;
    
  
  def rotate(self, arr) :
    row = len(arr);
    col = len(arr[0]);
    size = col;
    i = 0;
    if (row < col) :
      size = row;
    
    while (i < size / 2) :
      row -= 1;
      col -= 1;
      self.update(arr, row, col, i);
      i += 1;
    
  
  def show_data(self, arr) :
    i = 0;
    j = 0;
    row = len(arr);
    col = len(arr[0]);
    while (i < row) :
      j = 0;
      while (j < col) :
        print(arr[i][j],end=" ");
        j += 1;
      
      print();
      i += 1;
    
    print();
  
def main() :
  obj = Matrix();
  arr = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25]
  ];
  obj.show_data(arr);
  obj.rotate(arr);
  obj.show_data(arr);
  

if __name__ == "__main__":
  main()

Output

1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25 

6 1 2 3 4 
11 12 7 8 5 
16 17 13 9 10 
21 18 19 14 15 
22 23 24 25 20 


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved