Rotate matrix elements in given size

Rotate matrix elements

Observe that in above example has rotate matrix of size 5 elements. So before rotate [0][0] location are element 1. After rotate this element are shift to [1][4] location.

Here given code implementation process.

//C Program 
//Rotate matrix elements in given size
#include <stdio.h>

#define ROW 5
#define COL 5

void change(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 size)
{
  int row=ROW, col=COL,occurrence=COL;

  if(ROW < COL)
  {
    //When not a perfect square matrix
    //Get small size
    occurrence=ROW;
  }
  for (int i = 0; i < occurrence/2; ++i)
  {
    row--;
    col--;
    change(arr,row,col,i);
  }
  if(size>1)
  {
    rotate(arr,size-1);
  }
}
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}  
  }; 


  int size=5;

  printf(" Before Rotate \n\n");
  
  show_data(arr);

  printf(" After Rotate %d elements \n\n",size);
  //rotate matrix element of size 
  rotate(arr,size);

  show_data(arr);

  return 0;
}

Output

 Before Rotate 

  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

 After Rotate 5 elements 

 22 21 16 11  6
 23 14 19 18  1
 24  9 13 17  2
 25  8  7 12  3
 20 15 10  5  4
//C++ Program 
//Rotate matrix elements in given size
#include <iostream>
using namespace std;
#define ROW 5
#define COL 5


class MyArray
{

public:

  void change(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 size)
  {
    int row=ROW, col=COL,occurrence=COL;
    if (ROW < COL)
    {
      //When not a perfect square matrix
      //Get small size
      occurrence=ROW;
    }
    for (int i = 0; i < occurrence/2; ++i)
    {
      row--;
      col--;
      change(arr,row,col,i);
    }
    if (size>1)
    {
      rotate(arr,size-1);
    }
  }
  void show_data(int arr[][COL])
  {
    for (int i = 0; i < ROW; ++i)
    {
      for (int j = 0; j < COL; ++j)
      {
        cout<<"  "<<arr[i][j];
      }
      cout<<"\n";
    }
    cout<<"\n";
  }


};

int main()
{

  MyArray 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}  
  }; 
  int size=5;
  cout<<" Before Rotate \n\n";
  
  obj.show_data(arr);
  cout<<" After Rotate "<<size<<" elements \n\n";
  //rotate matrix element of size 
  obj.rotate(arr,size);
  obj.show_data(arr);



  return 0;
}

Output

 Before Rotate 

  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

 After Rotate 5 elements 

  22  21  16  11  6
  23  14  19  18  1
  24  9  13  17  2
  25  8  7  12  3
  20  15  10  5  4
//Java program
//Rotate matrix elements in given size
public class MyArray
{
  public void  change(int [][]arr,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;
    }

  }
  public void  rotate(int [][]arr,int size)
  {
    int row=arr.length, col =arr[0].length;
    int occurrence=col ;
    if (row < col )
    {
      //When not a perfect square matrix
      //Get small size
      occurrence=row;
    }
    for (int i = 0; i < occurrence/2; ++i)
    {
      row--;
      col --;
      change(arr,row,col ,i);
    }
    if (size>1)
    {
      rotate(arr,size-1);
    }
  }
  public void  show_data(int [][]arr)
  {
    int row=arr.length, col =arr[0].length;
    for (int i = 0; i < row; ++i)
    {
      for (int j = 0; j < col ; ++j)
      {
        System.out.print("  "+arr[i][j]);
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  
  public static void main(String[] args) 
  {
    MyArray obj = new MyArray();
    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}  
    }; 
    int size=5;
    System.out.print(" Before Rotate \n");

    obj.show_data(arr);
    System.out.print(" After Rotate "+size+" elements \n\n");
    //rotate matrix element of size 
    obj.rotate(arr,size);
    obj.show_data(arr);

  }
}

Output

 Before Rotate 
  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

 After Rotate 5 elements 

  22  21  16  11  6
  23  14  19  18  1
  24  9  13  17  2
  25  8  7  12  3
  20  15  10  5  4
#Python Program 
#Rotate matrix elements in given size
class MyArray:
  

  def change(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, size) :

    row=len(arr)
    col=len(arr[0])
    occurrence=col
    if (row < col) :

      #When not a perfect square matrix
      #Get small size
      occurrence=row

    i = 0
    while ( i < occurrence/2 ) :

      row -=1 
      col -=1 
      self.change(arr,row,col,i)
      i+=1
    if (size>1) :

      self.rotate(arr,size-1)

  def show_data(self, arr) :

    row=len(arr)
    col=len(arr[0])
    i = 0 
    while (i < row) :
      j = 0
      while ( j < col ) :

        print(arr[i][j],end="  ")
        j+=1

      print(end="\n")

      i+=1

    print(end="\n")


  
def main():

  obj = MyArray()
  matrix = [
    [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]
 
  ]

  size=5
  print(" Before Rotate \n")

  obj.show_data(matrix)
  print(" After Rotate ",size," elements \n")

  #rotate matrix element of size
  obj.rotate(matrix,size)
  obj.show_data(matrix)
 
if __name__ =="__main__":
  main()

Output

 Before Rotate 

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  

 After Rotate  5  elements 

22  21  16  11  6  
23  14  19  18  1  
24  9  13  17  2  
25  8  7  12  3  
20  15  10  5  4  
//C# program
//Rotate matrix elements in given size
using System;
public class MyArray
{
  public void  change(int [,]arr,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;
    }

  }
  public void  rotate(int [,]arr,int size)
  {
    int row=arr.GetLength(0), col =arr.GetLength(1);
    int occurrence=col ;
    if (row < col )
    {
      //When not a perfect square matrix
      //Get small size
      occurrence=row;
    }
    for (int i = 0; i < occurrence/2; ++i)
    {
      row--;
      col --;
      change(arr,row,col ,i);
    }
    if (size>1)
    {
      rotate(arr,size-1);
    }
  }
  public void  show_data(int [,]arr)
  {
    int row=arr.GetLength(0), col =arr.GetLength(1);
    for (int i = 0; i < row; ++i)
    {
      for (int j = 0; j < col ; ++j)
      {
        Console.Write("  "+arr[i,j]);
      }
      Console.Write("\n");
    }
    Console.Write("\n");
  }

  public static void Main(String[] args) 
  {
    MyArray obj = new MyArray();
    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}  
    }; 
    int size=5;
    Console.Write(" Before Rotate \n");

    obj.show_data(arr);
    Console.Write(" After Rotate "+size+" elements \n\n");
    //rotate matrix element of size 
    obj.rotate(arr,size);
    obj.show_data(arr);

  }
}

Output

 Before Rotate 
  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

 After Rotate 5 elements 

  22  21  16  11  6
  23  14  19  18  1
  24  9  13  17  2
  25  8  7  12  3
  20  15  10  5  4
<?php
//Php program 
//Rotate matrix elements in given size

class MyArray
{

  public function change( &$arr, $row, $col, $index)
  {

    $data = $arr[$row][$col];
    $temp = 0;
    for ( $i = $col-1; $i >=$index ; --$i)
    {
      $temp=$arr[$row][$i];
      $arr[$row][$i]=$data;
      $data=$temp;
    }

    for ( $i = $row-1; $i >=$index ; --$i)
    {
      $temp=$arr[$i][$index];
      $arr[$i][$index]=$data;
      $data=$temp;
    }

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

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

  }
  public function  rotate( &$arr, $size)
  {
    $row=count($arr);
    $col=count($arr[0]);
    $occurrence=$col;
    if ($row < $col)
    {
      //When not a perfect square matrix
      //Get small size
      $occurrence=$row;
    }
    for ( $i = 0; $i < $occurrence/2; ++$i)
    {
      $row--;
      $col--;
      $this->change($arr,$row,$col,$i);
    }
    if ($size>1)
    {
      $this->rotate($arr,$size-1);
    }
  }
  public function  show_data( $arr)
  {
    $row=count($arr);
    $col=count($arr[0]);
    for ( $i = 0; $i < $row; ++$i)
    {
      for ( $j = 0; $j < $col; ++$j)
      {
        echo "  ".$arr[$i][$j];
      }
      echo "\n";
    }
    echo "\n";
  }
}

function main()
{

  $obj= new MyArray();
  $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)
    ); 
  
  $size=5;
  echo " Before Rotate \n\n";
  
  $obj->show_data($arr);
  echo " After Rotate ".$size." elements \n\n";
  //rotate matrix element of size 
  $obj->rotate($arr,$size);
  $obj->show_data($arr);
}
main();
?>

Output

 Before Rotate 

  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

 After Rotate 5 elements 

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

Time complexity of above approach O(size * n) here n number of element in matrix. And size is indicate number of rotation.



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