Spiral pattern in CIn C, a 2D spiral pattern is the arrangement of components in a two-dimensional array in a spiral shape. This pattern is frequently found in programming tasks such as matrix traversal, where items must be accessed in a specified order. If we want to make a 2D spiral design, apply nested loops and four variables to maintain track of the spiral's top, bottom, left, and right borders. Example: Input: 4 Output: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 Approach- Create an n-dimensional 2D array.
- In the boundary variable, store the array's boundaries. It will start with n-1 and will change after each revolution.
- In variable sizeLeft, save the size left for spiral printing. It will start at n-1 and decrease by 1 after every two rotations.
- Create a flag to identify two revolutions, as the sizeLeft will drop after every two rotations.
- Create a char attribute move for storing the spiral pattern's present movement. It may contain the letters 'r' for right, 'l' for left, 'd' for down, and 'u' for up.
- Repeat the following steps until 'i' is in the range [1, n2]:
- Assign an i value to the spiral pattern.
- Determine the next pattern's movement.
- Check to see if the pattern has reached the boundary. Change the sizes and spin the spiral design if you reach this point.
- Print the pattern as a 2D array.
Program to spiral pattern in CLet's take an example to illustrate the spiral pattern in C. Filename: Pattern. c Output: Enter the size of the square matrix: 3
1 2 3
8 9 4
7 6 5
Enter the size of the square matrix: 9
1 2 3 4 5 6 7 8 9
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
Explanation - The generateSpiral function accepts an integer n as an input, representing the square matrix size.
- A 2D integer array matrix of size n x n is declared inside the method to store the spiral pattern.
- The borders of the matrix are represented by four variables: top, bottom, left, and right. The top is initially set to 0, the bottom to n - 1, the left to 0, and the right to n - 1.
- Another integer variable, num, has been set to 1. This variable records the value to be filled in the matrix, beginning with 1.
- The program enters in a while loop, repeated until the top boundary is less than or equal to the bottom boundary and the leftmost boundary is less than or equal to the right boundary. This loop ensures that the spiral pattern is accurately created.
- Four loops are used within the loop to fill the matrix in four directions: right, down, left, and up. Each of the directions takes up one side of the spiral.
- Move Right: The first loop traverses the row from left to right, filling it with values ranging from num to num + (right - left).
- Move Down: The second loop proceeds from top to bottom, filling the right-hand column with values ranging from num to num + (bottom - top).
- Move Left: The third loop proceeds from right to left, filling the row at the bottom border in reverse order with values ranging from num to num + (right - left).
- Move Up: The fourth loop advances from bottom to top, filling every column at the left border in reverse order with values ranging from num to num + (bottom - top).
- After filling each direction, the relevant boundary (top, bottom, left, or right) is modified to show that one side of the spiral is complete.
- After each value in the matrix is filled, the num variable is incremented.
- When the loop is finished, the created spiral pattern is printed through iterating through the matrix array, and each element is formatted properly.
- The main function accepts the size of the square matrix (n) from user input and runs the generate spiral function to produce and print the spiral pattern.
Time Complexity (O(n^2)):The generateSpiral function's main iteration iterates until the top is less than or equal to the bottom and the left is lower than or equal to the right. This loop will run for n/2 iterations in the worst-case scenario as the highest, bottom, left, and right boundaries move closer to the matrix's centre. We have four interconnected loops that traverse the rows and columns of the matrix within each iteration of the loop. These nested loops iterate some times in each direction. As a result, the total number of repetitions in the worst scenario is approximately 4 * n/2 * n, which compresses to O(n^2). Space Complexity(O(n^2)):The size of the matrices array, which is stated as int matrix[n][n], determines the space complexity. This array contains n rows and n columns for n2 entries. Each matrix array element stores an integer value. As a result, the space complexity is O(n^2) since it grows substantially with input size n.
|