Print a given matrix in reverse spiral form

Reverse spiral form of matrix

Here given code implementation process.

/*
  C Program 
+ Print a given matrix in reverse spiral form
*/
#include <stdio.h>
#define ROW 7
#define COL 6

void spiral(int data[ROW][COL],
  int result[],
  int s_row,
  int s_col,
  int e_row,
  int e_col,
  int element)
{
 
  //Left to right
  for (int i = s_col; i <=e_col && element > 0 ; ++i)
  {
    element--;
   result[element]=data[s_row][i];
  }
  //Top to down
  for (int i = s_row+1; i <=e_row && element > 0  ; ++i)
  {
    element--;
   result[element]=data[i][e_col];
  }
  //Bottom right to bottom-left
  for (int i = e_col-1; i >=s_col && element > 0 ; --i)
  {
    element--;
    result[element]=data[e_row][i];
  }

  //Bottom left to top
  for (int i =e_row-1 ; i > s_row && element > 0 ; --i)
  {
    element--;
    result[element]=data[i][s_row];
  }


  if(s_row+1 <= e_row-1 && element > 0)
  {
    //Recursive call
    spiral(data,result,s_row+1,s_col+1,e_row-1,e_col-1,element);
  }
    
}
void reverse_spiral(int arr[ROW][COL])
{

  int element=COL*(ROW);

  int result[element];

  spiral(arr,result,0,0,ROW-1,COL-1,element);

  for (int i = 0; i < element; ++i)
  {
    printf("%4d",result[i] );
  }
}
int main()
{

  int arr[ROW][COL] =  {
    {1, 2, 3, 4, 5, 6},
    {22,23,24,25,26,7},
    {21,36,37,38,27,8},
    {20,35,-2,39,28,9},
    {19,34,-1,40,29,10},
    {18,33,32,31,30,11},
    {17,16,15,14,13,12},
  };

  reverse_spiral(arr);
 

  return 0;
}

Output

  -2  -1  40  39  38  37  36  35  34  33  32  31  30  29  28  27  26  25  24  23  22  21  20  19  18  17  16  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1
//C++ Program 
//Print a given matrix in reverse spiral form
#include <iostream>
using namespace std;
#define ROW 7
#define COL 6

class MyArray
{

public:

  void spiral(int data[ROW][COL],
    int result[],
    int s_row,
    int s_col,
    int e_row,
    int e_col,
    int element)
  {

    //Left to right
    for (int i = s_col; i <=e_col && element > 0 ; ++i)
    {
      element--;
      result[element]=data[s_row][i];
    }
    //Top to down
    for (int i = s_row+1; i <=e_row && element > 0  ; ++i)
    {
      element--;
      result[element]=data[i][e_col];
    }
    //Bottom right to bottom-left
    for (int i = e_col-1; i >=s_col && element > 0 ; --i)
    {
      element--;
      result[element]=data[e_row][i];
    }
    //Bottom left to top
    for (int i =e_row-1 ; i > s_row && element > 0 ; --i)
    {
      element--;
      result[element]=data[i][s_row];
    }
    if (s_row+1 <= e_row-1 && element > 0)
    {
      //Recursive call
      spiral(data,result,s_row+1,s_col+1,e_row-1,e_col-1,element);
    }

  }
  void reverse_spiral(int arr[ROW][COL])
  {
    int element=COL*(ROW);
    int result[element];
    spiral(arr,result,0,0,ROW-1,COL-1,element);
    for (int i = 0; i < element; ++i)
    {
      cout<<"  "<<result[i];
    }
  }
};

int main()
{

  MyArray obj;

  int arr[ROW][COL] =  {
    {1,  2,  3,  4,  5, 6},
    {22, 23, 24, 25, 26, 7},
    {21, 36, 37, 38, 27, 8},
    {20, 35, -2, 39, 28, 9},
    {19, 34, -1, 40, 29,10},
    {18, 33, 32, 31, 30, 11},
    {17, 16, 15, 14, 13, 12},
  };
  
  obj.reverse_spiral(arr);


  return 0;
}

Output

  -2  -1  40  39  38  37  36  35  34  33  32  31  30  29  28  27  26  25  24  23  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  1
//Java program
//Print a given matrix in reverse spiral form
public class MyArray
{

  public void  spiral ( int [][]data , 
    int [] result,
    int s_row , 
    int s_col , 
    int e_row , 
    int e_col , 
    int element)
  {

   //Left to right
    for ( int i = s_col ;  i  <=  e_col  &&  element > 0  ;  ++i)
    {
      element-- ; 
      result[element]= data[s_row][i];
    }
    //Top to down
    for ( int i = s_row+1 ;  i  <=  e_row  &&  element > 0 ;  ++i)
    {
      element-- ; 
      result[element]=data[i][e_col];
     
    }
     //Bottom right to bottom-left
    for ( int i = e_col-1 ;  i >= s_col  &&  element > 0 ;  --i)
    {
      element-- ; 
      result[element]=data[e_row][i];
    
    }

    //Bottom left to top
    for ( int i  = e_row-1  ;  i > s_row  &&  element > 0 ;  --i)
    {
      element-- ; 
      result[element]=data[i][s_row] ;
     
    }


    if ( s_row+1 <=  e_row-1  &&  element > 0)
    {
      //Recursive call
      spiral ( data ,result, s_row+1 , s_col+1 , e_row-1 , e_col-1 , element) ; 
    }
    
  }
  public void reverse_spiral(int [][]matrix)
  {
    int row =  matrix.length;
    int col =  matrix[0].length;
    //Get the size of array elements
    int element=row*col;

    int []result = new int[element];
    spiral ( matrix ,result, 0 , 0 , row-1 , col-1 , element) ; 
    for (int i = 0; i < element; ++i)
    {
      System.out.print(" "+result[i]);
    }
  }
  public static void main(String[] args) 
  {
    MyArray obj = new MyArray();
    //array elements
    int [][]matrix ={
    {1,  2,  3,  4,  5, 6},
    {22, 23, 24, 25, 26, 7},
    {21, 36, 37, 38, 27, 8},
    {20, 35, -2, 39, 28, 9},
    {19, 34, -1, 40, 29,10},
    {18, 33, 32, 31, 30, 11},
    {17, 16, 15, 14, 13, 12},
  };
    
    
    obj.reverse_spiral(matrix);
  }
}

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
#Python Program 
#Print a given matrix in reverse spiral form
class MyArray:
  
  def spiral (self,
    data , 
    result,
    s_row , 
    s_col , 
    e_row , 
    e_col , 
    element) :

    #Left to right
    i=s_col
    while(i  <=  e_col   and   element > 0 ) :

      element -= 1 
      result[element] = data[s_row][i]
      
      i += 1 

    
    #Top to down

    i = s_row + 1

    while (i  <=  e_row   and   element > 0 ) :

      element -= 1 

      result[element] = data[i][e_col]
     
      i += 1 


    #Bottom right to bottom-left
    i = e_col-1
    while ( i >= s_col and element > 0   --i) :

      element -= 1
      result[element] = data[e_row][i]   

      i -= 1 

    #Bottom left to top
    i  = e_row-1
    while ( i > s_row  and element > 0   ) :

      element -= 1  
      result[element] = data[i][s_row]
    
      i -= 1

    if ( s_row+1 <=  e_row-1 and element > 0) :

      #Recursive call
      self.spiral ( data ,result, s_row+1 , s_col+1 , e_row-1 , e_col-1 , element)  


def main():

  obj = MyArray()
  matrix = [
  [1,  2,  3,  4,  5, 6],
  [22, 23, 24, 25, 26, 7],
  [21, 36, 37, 38, 27, 8],
  [20, 35, -2, 39, 28, 9],
  [19, 34, -1, 40, 29,10],
  [18, 33, 32, 31, 30, 11],
  [17, 16, 15, 14, 13, 12]
]
  row = len(matrix)
  col = len(matrix[0])  
  element = row * col
  result=[0]*element
  obj.spiral ( matrix,result, 0 , 0 , row-1 , col-1 , element)
  i=0
  while(i<element):
    print(result[i],end=" ")
    i+=1 
 
if __name__ =="__main__":
  main()

Output

0 -2 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
//C# program
//Print a given matrix in reverse spiral form
using System;
public class MyArray
{

  public void  spiral ( int [,]data , 
    int [] result,
    int s_row , 
    int s_col , 
    int e_row , 
    int e_col , 
    int element)
  {

    //Left to right
    for ( int i = s_col ;  i  <=  e_col  &&  element > 0  ;  ++i)
    {
      element-- ; 
      result[element]= data[s_row,i];
    }
    //Top to down
    for ( int i = s_row+1 ;  i  <=  e_row  &&  element > 0 ;  ++i)
    {
      element-- ; 
      result[element]=data[i,e_col];

    }
    //Bottom right to bottom-left
    for ( int i = e_col-1 ;  i >= s_col  &&  element > 0 ;  --i)
    {
      element-- ; 
      result[element]=data[e_row,i];

    }

    //Bottom left to top
    for ( int i  = e_row-1  ;  i > s_row  &&  element > 0 ;  --i)
    {
      element-- ; 
      result[element]=data[i,s_row] ;

    }


    if ( s_row+1 <=  e_row-1  &&  element > 0)
    {
      //Recursive call
      spiral ( data ,result, s_row+1 , s_col+1 , e_row-1 , e_col-1 , element) ; 
    }

  }
  public void reverse_spiral(int [,]matrix)
  {
    int row =  matrix.GetLength(0);
    int col =  matrix.GetLength(1);
    //Get the size of array elements
    int element=row*col;

    int []result = new int[element];
    spiral ( matrix ,result, 0 , 0 , row-1 , col-1 , element) ; 
    for (int i = 0; i < element; ++i)
    {
      Console.Write(" "+result[i]);
    }
  }
  public static void Main(String[] args) 
  {
    MyArray obj = new MyArray();
    //array elements
    int [,]matrix ={
      {1,  2,  3,  4,  5, 6},
      {22, 23, 24, 25, 26, 7},
      {21, 36, 37, 38, 27, 8},
      {20, 35, -2, 39, 28, 9},
      {19, 34, -1, 40, 29,10},
      {18, 33, 32, 31, 30, 11},
      {17, 16, 15, 14, 13, 12},
    };


    obj.reverse_spiral(matrix);
  }
}

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
<?php
//Php program 
//Print a given matrix in reverse spiral form

class MyArray
{


  function  spiral($data, 
  &$result,
  $s_row, 
  $s_col, 
  $e_row, 
  $e_col, 
  $element)
  {

   //Left to right
    for  ($i= $s_col;  $i <=  $e_col &&  $element>0 ; ++ $i)
    {
      $element--; 
      $result[$element] = $data[$s_row][$i]; 
    }


    //Top to down
    for  ($i= $s_row+1;  $i <=  $e_row &&  $element>0; ++ $i)
    {
      $element--; 
      $result[$element] = $data[$i][$e_col];
   
    }

     //Bottom right to bottom-left
    for($i= $e_col-1;  $i>= $s_col &&  $element>0; --$i)
    {
      $element--; 
      $result[$element] = $data[$e_row][$i]; 
    }



    //Bottom left to top
    for ($i = $e_row-1 ; $i> $s_row &&  $element>0; --$i)
    {
      $element--; 
      $result[$element] = $data[$i][$s_row]; 
    }


    if ( $s_row+1<=  $e_row-1 &&  $element>0)
    {


      //Recursive call
      $this->spiral( $data,$result, $s_row+1, $s_col+1, $e_row-1, $e_col-1, $element); 
    }

  }
  public function  reverse_spiral( $matrix)
  {
    $row = count($matrix);
    $col =count($matrix[0]);
    $element=$row*$col;
    $this->spiral($matrix,$result,0,0,$row-1,$col-1,$element);
    for ( $i = 0; $i < $element; ++$i)
    {
      echo "  ".$result[$i];
    }
  }
}
function main()
{

  $obj= new MyArray();
  $matrix = array(
  array(1,  2,  3,  4,  5, 6),
  array(22, 23, 24, 25, 26, 7),
  array(21, 36, 37, 38, 27, 8),
  array(20, 35, -2, 39, 28, 9),
  array(19, 34, -1, 40, 29,10),
  array(18, 33, 32, 31, 30, 11),
  array(17, 16, 15, 14, 13, 12)
); 


  $obj->reverse_spiral ( $matrix) ; 
}
main();
?>

Output

  -2  -1  40  39  38  37  36  35  34  33  32  31  30  29  28  27  26  25  24  23  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  1
# Ruby Program 
# Print a given matrix in reverse spiral form

class MyArray 
  def spiral(data, result, s_row, s_col, e_row, e_col, element) 
    # Left to right
    i = s_col
    while (i <= e_col and element > 0) 
      element -= 1
      result[element] = data[s_row][i]
      i += 1
    end
    # Top to down
    i = s_row + 1
    while (i <= e_row and element > 0) 
      element -= 1
      result[element] = data[i][e_col]
      i += 1
    end
    # Bottom right to bottom-left
    i = e_col - 1
    while (i >= s_col and element > 0) 
      element -= 1
      result[element] = data[e_row][i]
      i -= 1
    end
    # Bottom left to top
    i = e_row - 1
    while (i > s_row and element > 0) 
      element -= 1
      result[element] = data[i][s_row]
      i -= 1
    end
    if (s_row + 1 <= e_row - 1 and element > 0) 
      self.spiral(data, result, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element)
    end
  end
  def reverse_spiral(matrix) 
    row = matrix.length
    col = matrix[0].length
    element = row * col
    result = Array.new(element, 0)
    self.spiral(matrix, result, 0, 0, row - 1, col - 1, element)
    i = 0
    while (i < element) 
      print(" ", result[i])
      i += 1
    end
  end
end
def main() 
  obj = MyArray.new()
  matrix = [
    [1, 2, 3, 4, 5, 6],
    [22, 23, 24, 25, 26, 7],
    [21, 36, 37, 38, 27, 8],
    [20, 35, -2, 39, 28, 9],
    [19, 34, -1, 40, 29, 10],
    [18, 33, 32, 31, 30, 11],
    [17, 16, 15, 14, 13, 12]
  ]
  obj.reverse_spiral(matrix)
end
main()

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
/*
 Swift 4 Program
 Print a given matrix in reverse spiral form
*/

class MyArray {
  func spiral(_ data: [[Int]], _ result: inout [Int], _ s_row: Int, _ s_col: Int, _ e_row: Int, _ e_col: Int, _ size: Int) {
    //Left to right
    var i: Int = s_col;
    var element : Int = size;
    while (i <= e_col && element > 0) {
      element -= 1;
      result[element] = data[s_row][i];
      i += 1;
    }
    //Top to down
    i = s_row + 1;
    while (i <= e_row && element > 0) {
      element -= 1;
      result[element] = data[i][e_col];
      i += 1;
    }
    //Bottom right to bottom-left
    i = e_col - 1;
    while (i >= s_col && element > 0) {
      element -= 1;
      result[element] = data[e_row][i];
      i -= 1;
    }
    //Bottom left to top
    i = e_row - 1;
    while (i > s_row && element > 0) {
      element -= 1;
      result[element] = data[i][s_row];
      i -= 1;
    }
    if (s_row + 1 <= e_row - 1 && element > 0) {
      self.spiral(data, &result, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
    }
  }
  func reverse_spiral(_ matrix: [[Int]]) {
    let row: Int = matrix.count;
    let col: Int = matrix[0].count;
    let element: Int = row * col;
    var result: [Int] = Array(repeating: 0, count: element);
    self.spiral(matrix, &result, 0, 0, row - 1, col - 1, element);
    var i: Int = 0;
    while (i < element) {
      print(" ", result[i],terminator:"");
      i += 1;
    }
  }
}
func main() {
  let obj: MyArray = MyArray();
  let matrix: [[Int]] = [
    [1, 2, 3, 4, 5, 6],
    [22, 23, 24, 25, 26, 7],
    [21, 36, 37, 38, 27, 8],
    [20, 35, -2, 39, 28, 9],
    [19, 34, -1, 40, 29, 10],
    [18, 33, 32, 31, 30, 11],
    [17, 16, 15, 14, 13, 12]
  ];
  obj.reverse_spiral(matrix);
}
main();

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
/*
 Scala Program
 Print a given matrix in reverse spiral form
*/


class MyArray {
  def spiral(data: Array[Array[Int]], result: Array[Int], s_row: Int, s_col: Int, e_row: Int, e_col: Int, size: Int) {
    //Left to right
    var i: Int = s_col;
    var element: Int = size;
    while (i <= e_col && element > 0) {
      element -= 1;
      result(element) = data(s_row)(i);
      i += 1;
    }
    //Top to down
    i = s_row + 1;
    while (i <= e_row && element > 0) {
      element -= 1;
      result(element) = data(i)(e_col);
      i += 1;
    }
    //Bottom right to bottom-left
    i = e_col - 1;
    while (i >= s_col && element > 0) {
      element -= 1;
      result(element) = data(e_row)(i);
      i -= 1;
    }
    //Bottom left to top
    i = e_row - 1;
    while (i > s_row && element > 0) {
      element -= 1;
      result(element) = data(i)(s_row);
      i -= 1;
    }
    if (s_row + 1 <= e_row - 1 && element > 0) {
      this.spiral(data, result, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
    }
  }
  def reverse_spiral(matrix: Array[Array[Int]]) {
    var row: Int = matrix.length;
    var col: Int = matrix(0).length;
    var element: Int = row * col;
    var result: Array[Int] = Array.fill[Int](element)(0);
    this.spiral(matrix, result, 0, 0, row - 1, col - 1, element);
    var i: Int = 0;
    while (i < element) {
      print(" " + result(i));
      i += 1;
    }
  }
}
object Main {
  def main(args: Array[String]) {
    var obj: MyArray = new MyArray();
    var matrix: Array[Array[Int]] = Array(
      Array(1, 2, 3, 4, 5, 6),
      Array(22, 23, 24, 25, 26, 7),
      Array(21, 36, 37, 38, 27, 8),
      Array(20, 35, -2, 39, 28, 9),
      Array(19, 34, -1, 40, 29, 10),
      Array(18, 33, 32, 31, 30, 11),
      Array(17, 16, 15, 14, 13, 12));
    obj.reverse_spiral(matrix);
  }
}

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
/*
 Node Js Program
 Print a given matrix in reverse spiral form
*/


class MyArray {

  spiral(data, result, s_row, s_col, e_row, e_col, element) {
    //Left to right

    for (var i = s_col; i <= e_col && element > 0; ++i) {
      element--;
      result[element] = data[s_row][i];
    }
    //Top to down
    for (var i = s_row + 1; i <= e_row && element > 0; ++i) {
      element--;
      result[element] = data[i][e_col];
    }
    //Bottom right to bottom-left
    for (var i = e_col - 1; i >= s_col && element > 0; --i) {
      element--;
      result[element] = data[e_row][i];
    }
    //Bottom left to top
    for (var i = e_row - 1; i > s_row && element > 0; --i) {
      element--;
      result[element] = data[i][s_row];
    }
    if (s_row + 1 <= e_row - 1 && element > 0) {
      //Recursive call
      this.spiral(data, result, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
    }
  }
  reverse_spiral(matrix) {
    var row = matrix.length;
    var col = matrix[0].length;
    //Get the size of array elements
    var element = row *col;
    var result = Array(element).fill(0);
    this.spiral(matrix, result, 0, 0, row - 1, col - 1, element);
    for (var i = 0; i < element; ++i) {
      process.stdout.write(" " + result[i]);
    }
  }
}

function main() {
  var obj = new MyArray();
  //array elements
  var matrix = [
    [1, 2, 3, 4, 5, 6],
    [22, 23, 24, 25, 26, 7],
    [21, 36, 37, 38, 27, 8],
    [20, 35, -2, 39, 28, 9],
    [19, 34, -1, 40, 29, 10],
    [18, 33, 32, 31, 30, 11],
    [17, 16, 15, 14, 13, 12]
  ];
  obj.reverse_spiral(matrix);
}

main();

Output

 -2 -1 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1


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