Spiral view of diamond element in square matrix

Spiral view

Here given code implementation process.

/*
  C Program 
+ Spiral view of diamond element in square matrix
*/
#include <stdio.h>

#define ROW 7
#define COL 7

void spiral_view(int data[ROW][COL],
  int s_row,
  int s_col,
  int e_row,
  int e_col,
  int k) {

  int mid_col = s_col + ((e_col - s_col) / 2);

  int mid_row = mid_col;

  for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++) {
    printf("%4d", data[s_row + j][i]);
  }

  for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++) {
    printf("%4d", data[(mid_row) + j][i]);
  }

  for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++) {
    printf("%4d", data[(e_col) - j][i]);
  }

  for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++) {
    printf("%4d", data[(mid_row) - j][i]);
  }



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


}
void diamond_spiral(int arr[ROW][COL]) {
  if (ROW != COL || COL % 2 == 0) {
    printf("\nNot a valid perfect Odd square matrix");
    return;
  }

  spiral_view(arr, 0, 0, ROW - 1, COL - 1, (ROW *COL) - ((COL + 1) / 2) *4);

}
int main() {

  int arr[ROW][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,  26, 27, 28},
    {29, 30, 31, 32,  33, 34, 35},
    {36, 37, 38, 39,  40, 41, 42},
    {43, 44, 45, 46,  47, 48, 49}
  }; 
  diamond_spiral(arr);
  return 0;
}

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
/*
  Java Program 
+ Spiral view of diamond element in square matrix
*/
public class Matrix
{

  public void spiral_view(int [][]data,
    int s_row,
    int s_col,
    int e_row,
    int e_col,
    int k) {

    int mid_col = s_col + ((e_col - s_col) / 2);

    int mid_row = mid_col;

    for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++) {
      System.out.print("  "+ data[s_row + j][i]);
    }

    for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++) {
      System.out.print("  "+ data[(mid_row) + j][i]);
    }

    for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++) {
      System.out.print("  "+ data[(e_col) - j][i]);
    }

    for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++) {
      System.out.print("  "+ data[(mid_row) - j][i]);
    }

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


  }
  public void diamond_spiral(int [][]arr) {
    int row = arr.length;
    int col = arr[0].length;
    if (row != col || col % 2 == 0) 
    {
      System.out.print("\nNot a valid perfect Odd square matrix");
      return;
    }

    spiral_view(arr, 0, 0, row - 1, col - 1, (row * col) - ((col + 1) / 2) * 4);

  }
  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,  26, 27, 28},
      {29, 30, 31, 32,  33, 34, 35},
      {36, 37, 38, 39,  40, 41, 42},
      {43, 44, 45, 46,  47, 48, 49}
    }; 
    obj.diamond_spiral(arr);

  }
}



Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
/*
C++ Program 
Spiral view of diamond element in square matrix
*/
#include <iostream>

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

class Matrix {
public:
  void spiral_view(int data[ROW][COL], int s_row, int s_col, int e_row, int e_col, int k) {
    int mid_col = s_col + ((e_col - s_col) / 2);
    int mid_row = mid_col;
    int i = mid_col, j = 0;
    while (i < e_col && k > 0) {
      cout <<"  "<< data[s_row + j][i];
      i++;
      k--;
      j++;
    }
    i = e_col;
    j = 0;
    while (i >= mid_col && k > 0) {
      cout << "  "<< data[(mid_row) + j][i];
      j++;
      k--;
      i--;
    }
    i = mid_col - 1;
    j = 1;
    while (i >= s_col && k > 0) {
      cout <<"  " << data[(e_col) - j][i];
      j++;
      k--;
      i--;
    }
    i = s_col + 1;
    j = 1;
    while (i < mid_col && k > 0) {
      cout <<"  "<< data[(mid_row) - j][i];
      i++;
      k--;
      j++;
    }
    if (s_row + 1 <= e_row - 1 && k > 0) {
      this->spiral_view(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
    }
  }
 void diamond_spiral(int arr[ROW][COL]) {
    int row = ROW;
    int col = COL;
    if (row != col || col % 2 == 0) {
      cout << "\nNot a valid perfect Odd square matrix";
      return;
    }
    this->spiral_view(arr, 0, 0, row - 1, col - 1, (row *col) - ((col + 1) / 2) *4);
  }
};

int main() {
  Matrix obj;
  int arr[ROW][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,  26, 27, 28},
    {29, 30, 31, 32,  33, 34, 35},
    {36, 37, 38, 39,  40, 41, 42},
    {43, 44, 45, 46,  47, 48, 49}
  }; 
  obj.diamond_spiral(arr);
  return 0;
}

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
# Python 3 Program
# Spiral view of diamond element in square matrix
class Matrix :

  def spiral_view(self, data, s_row, s_col, e_row, e_col, k) :
    mid_col = s_col + int((e_col - s_col) / 2)
    mid_row = mid_col
    i = mid_col
    j = 0
    while (i < e_col and k > 0) :
      print(data[s_row + j][i],end=" ")
      i += 1
      k -= 1
      j += 1
    
    i = e_col
    j = 0
    while (i >= mid_col and k > 0) :
      print(data[(mid_row) + j][i],end=" ")
      j += 1
      k -= 1
      i -= 1
    
    i = mid_col - 1
    j = 1
    while (i >= s_col and k > 0) :
      print( data[(e_col) - j][i],end=" ")
      j += 1
      k -= 1
      i -= 1
    
    i = s_col + 1
    j = 1
    while (i < mid_col and k > 0) :
      print(data[(mid_row) - j][i],end=" ")
      i += 1
      k -= 1
      j += 1
    
    if (s_row + 1 <= e_row - 1 and k > 0) :
      self.spiral_view(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
    
  
  def diamond_spiral(self, arr) :
    row = len(arr)
    col = len(arr[0])
    if (row != col or col % 2 == 0) :
      print("\nNot a valid perfect Odd square matrix")
      return
    
    self.spiral_view(arr, 0, 0, row - 1, col - 1, (row * col) - int((col + 1) / 2) * 4)
  
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, 26, 27, 28],
    [29, 30, 31, 32, 33, 34, 35],
    [36, 37, 38, 39, 40, 41, 42],
    [43, 44, 45, 46, 47, 48, 49]
  ]
  obj.diamond_spiral(arr)


if __name__ == "__main__":
  main()

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
/*
  C# Program 
  Spiral view of diamond element in square matrix
*/
using System;
public class Matrix
{

	public void spiral_view(int [,]data,
		int s_row,
		int s_col,
		int e_row,
		int e_col,
		int k) {

		int mid_col = s_col + ((e_col - s_col) / 2);

		int mid_row = mid_col;

		for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++) {
			Console.Write("  "+ data[s_row + j,i]);
		}

		for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++) {
			Console.Write("  "+ data[(mid_row) + j,i]);
		}

		for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++) {
			Console.Write("  "+ data[(e_col) - j,i]);
		}

		for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++) {
			Console.Write("  "+ data[(mid_row) - j,i]);
		}

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


	}
	public void diamond_spiral(int [,]arr) {
		int row = arr.GetLength(0);
		int col = arr.GetLength(1);
		if (row != col || col % 2 == 0) 
		{
			Console.Write("\nNot a valid perfect Odd square matrix");
			return;
		}

		spiral_view(arr, 0, 0, row - 1, col - 1, (row * col) - ((col + 1) / 2) * 4);

	}
	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,  26, 27, 28},
			{29, 30, 31, 32,  33, 34, 35},
			{36, 37, 38, 39,  40, 41, 42},
			{43, 44, 45, 46,  47, 48, 49}
		}; 
		obj.diamond_spiral(arr);

	}
}



Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
# Ruby program
# Spiral view of diamond element in square matrix
class Matrix 
	def spiral_view(data, s_row, s_col, e_row, e_col, k) 
		mid_col = s_col + ((e_col - s_col) / 2)
		mid_row = mid_col
		i = mid_col
		j = 0
		while (i < e_col and k > 0) 
			print("  ", data[s_row + j][i])
			i += 1
			k -= 1
			j += 1
		end
		i = e_col
		j = 0
		while (i >= mid_col and k > 0) 
			print("  ", data[(mid_row) + j][i])
			j += 1
			k -= 1
			i -= 1
		end
		i = mid_col - 1
		j = 1
		while (i >= s_col and k > 0) 
			print("  ", data[(e_col) - j][i])
			j += 1
			k -= 1
			i -= 1
		end
		i = s_col + 1
		j = 1
		while (i < mid_col and k > 0) 
			print("  ", data[(mid_row) - j][i])
			i += 1
			k -= 1
			j += 1
		end
		if (s_row + 1 <= e_row - 1 and k > 0) 
			self.spiral_view(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
		end
	end
	def diamond_spiral(arr) 
		row = arr.length
		col = arr[0].length
		if (row != col or col % 2 == 0) 
			print("\nNot a valid perfect Odd square matrix")
			return
		end
		self.spiral_view(arr, 0, 0, row - 1, col - 1, (row * col) - ((col + 1) / 2) * 4)
	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, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49] 
	]
	
	obj.diamond_spiral(arr)
end
main()

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
/*
  Node Js Program
  Spiral view of diamond element in square matrix
*/
class Matrix {
	spiral_view(data, s_row, s_col, e_row, e_col, k) {
		var mid_col = s_col + ((e_col - s_col) / 2);
		var mid_row = mid_col;
		var i = mid_col;
		var j = 0;
		while (i < e_col && k > 0) {
			process.stdout.write("  " + data[s_row + j][i]);
			i++;
			k--;
			j++;
		}
		i = e_col;
		j = 0;
		while (i >= mid_col && k > 0) {
			process.stdout.write("  " + data[(mid_row) + j][i]);
			j++;
			k--;
			i--;
		}
		i = mid_col - 1;
		j = 1;
		while (i >= s_col && k > 0) {
			process.stdout.write("  " + data[(e_col) - j][i]);
			j++;
			k--;
			i--;
		}
		i = s_col + 1;
		j = 1;
		while (i < mid_col && k > 0) {
			process.stdout.write("  " + data[(mid_row) - j][i]);
			i++;
			k--;
			j++;
		}
		if (s_row + 1 <= e_row - 1 && k > 0) {
			this.spiral_view(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
		}
	}
	diamond_spiral(arr) {
		var row = arr.length;
		var col = arr[0].length;
		if (row != col || col % 2 == 0) {
			process.stdout.write("\nNot a valid perfect Odd square matrix");
			return;
		}
		this.spiral_view(arr, 0, 0, row - 1, col - 1, (row *col) - ((col + 1) / 2) *4);
	}


}
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, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49]
	];
	obj.diamond_spiral(arr);
}
main();

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25
<?php

/*
  Php Program 
  Spiral view of diamond element in square matrix
*/
class Matrix {
  public
  function spiral_view($data, $s_row, $s_col, $e_row, $e_col, $k) {
    $mid_col = $s_col + intval(($e_col - $s_col) / 2);
    $mid_row = $mid_col;
    $i = $mid_col;
    $j = 0;
    while ($i < $e_col && $k > 0) {
      echo ("  ". $data[$s_row + $j][$i]);
      $i++;
      $k--;
      $j++;
    }
    $i = $e_col;
    $j = 0;
    while ($i >= $mid_col && $k > 0) {
      echo ("  ". $data[($mid_row) + $j][$i]);
      $j++;
      $k--;
      $i--;
    }
    $i = $mid_col - 1;
    $j = 1;
    while ($i >= $s_col && $k > 0) {
      echo ("  ". $data[($e_col) - $j][$i]);
      $j++;
      $k--;
      $i--;
    }
    $i = $s_col + 1;
    $j = 1;
    while ($i < $mid_col && $k > 0) {
      echo ("  ". $data[($mid_row) - $j][$i]);
      $i++;
      $k--;
      $j++;
    }
    if ($s_row + 1 <= $e_row - 1 && $k > 0) {
      $this->spiral_view($data, $s_row + 1, $s_col + 1, $e_row - 1, $e_col - 1, $k);
    }
  }
  public
  function diamond_spiral($arr) {
    $row = count($arr);
    $col = count($arr[0]);
    if ($row != $col || $col % 2 == 0) {
      echo("\nNot a valid perfect Odd square matrix");
      return;
    }
    $this->spiral_view($arr, 0, 0, $row - 1, $col - 1, ($row *$col) - intval(($col + 1) / 2) *4);
  }

}
function main() {
  $obj = new Matrix();
  $arr = array(
          array(1, 2, 3, 4, 5, 6, 7), 
          array(8, 9, 10, 11, 12, 13, 14), 
          array(15, 16, 17, 18, 19, 20, 21), 
          array(22, 23, 24, 25, 26, 27, 28), 
          array(29, 30, 31, 32, 33, 34, 35), 
          array(36, 37, 38, 39, 40, 41, 42), 
          array(43, 44, 45, 46, 47, 48, 49)
  );
  $obj->diamond_spiral($arr);
}
main();

Output

   4  12  20  28  34  40  46  38  30  22  16  10  11  19  27  33  39  31  23  17  18  26  32  24  25


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