Javatpoint Logo
Javatpoint Logo

Print a given matrix in spiral form

Introduction

In computer science and mathematics, matrices are fundamental structures that are used as the building blocks of different algorithms and computations. Different matrix manipulation techniques can produce intriguing patterns and effective solutions. Printing a matrix in spiral form is one such fascinating process.When we refer to a spiral order, we mean navigating the matrix in a clockwise direction, beginning in the top-left corner and working your way down to the center.The concept is to print the outermost elements more than once, progressively approaching the center and modifying the boundaries as you go.

Overview of the Algorithm

There are four steps in the algorithm for printing a matrix spiral form:

  • Starting from the left, print the first row.
  • Print the final column from left to right.
  • Copy the final row from the right to the left.
  • The first column should be printed from top to bottom.
  • Until every element in the matrix is printed, these procedures are repeated. We can use four variables to track the boundaries of the submatrix that needto be printed in each step of this algorithm's efficient implementation.

Code

Output:

Print a given matrix in spiral form

Code Explanation

PrintSpiral function

  • Parameters: Takes two integer rows and cols to indicate the matrix's dimensions and a 2D array matrix to represent the matrix to be printed in spiral form.
  • Variables: The boundaries of the submatrix to be printed in each step are tracked using four variables: top, bottom, left, and right.

Spiral Printing Algorithm

  • The while loop keeps going until the matrix's elements are all printed. Four steps are taken by the code inside the loop:
  1. Print Top Row: Prints each element by iterating along the top row from left to right.
  2. Print Rightmost Column: Prints each element as iterates along the rightmost column from top to bottom.
  3. Print Bottom Row: This code iterates along the bottom row from right to left, printing each element if there is a row below the top row (top less than or equal to the bottom).
  4. Print Leftmost Column: Iterating from bottom to top along the leftmost column, print each element if there is a column to the left of the rightmost column (left is less than or equal to right).

Function main

  • Declares the variable's rows and cols to hold the matrix's dimensions.
  • Ask the user to enter the number of columns and rows.
  • Creates a 2D array matrix to hold the elements of the matrix.
  • Asks the user to enter matrix elements through the use of nested loops.
  • To print the matrix in spiral form, call the printSpiral function.

Input and Output

  • The user is prompted by the application to enter the number of rows and columns before moving on to the matrix elements.
  • It prints the matrix in a spiral form to the console after input.

Preprocessor Directives

  • In order to define a constant MAX_SIZE that represents the maximum size of the matrix, the code contains the preprocessor directive #define MAX_SIZE 10.

Limitations

  • The code assumes a maximum matrix size of 10x10 (MAX_SIZE).
  • For the sake of simplicity, input validation, which verifies that the user enters valid integers, is not used.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA