How to Create a Spiralling Algorithm in C++ for Printing Patterns

In summary, the pattern starts near the center and spirals out. To generate one line-by-line for an arbitrary size, you need to think about the loop counters a bit. This one is tricky because the numbering starts from the center. However, using recursion, you can set the up, down, left and right freedom of movement.
  • #1
ron_jay
81
0
Hey, I got an assignment to write and algorithm in C++ to print this pattern:

35 34 33 32 31 30
16 15 14 13 12 29
17 04 03 02 11 28
18 05 00 01 10 27
19 06 07 08 09 26
20 21 22 23 24 25

As you can see, the number starts near the center and spirals out... I am stumped. Any help?
 
Technology news on Phys.org
  • #2
Simple way for small size is to create a 2d array and fill it in starting from the centre (just need a couple of loops) and then print it.
Generating one line-by-line for an arbitrary size isn't too hard but requires thinking about the loop counters a bit!
 
  • #3
This one is a little tricky because the numbering starts from the center. I have already figured out how to do this when the numbering starts from the top left hand corner (recursion) using
"virtual motion analogy" (that's what I like to call it).However,looping with for or while is clumsy. Recursion should solve it elegantly. One can assume the array matrix as a 2D "field" and set them to 0. By, recursion, we can then set the up, down, left and right freedom of movement. However, this is the part which is slippery...how do the axes change to suit the situation? Its like a virtual loop that keeps on depositing a solid layer around it as it rotates on its outer periphery being pulled by the central force - just an analogy...if this could be interpreted in the terms of the matrix...
 
  • #4
Use an offset value for the center, in this case it's 3,3 (using y,x as index order)

Then the starting relative index will be -1,-1, followed by -1,+1, +1,+1, +1,-1.

Each time the index pattern is +n, -n, your at end of the current box, and ready to start the next box.

Don't forget to handle the case where the size of the array is an odd number of columns and rows.
You start at the middle, go right, fill in the 8 elements counter clockwise, then step out to the right
again.
 
Last edited:

1. What is a spiralling algorithm in C++?

A spiralling algorithm in C++ is a type of algorithm that is used to create a spiral pattern in a 2-dimensional array. It is commonly used in computer graphics and image processing to create visually appealing designs.

2. How does a spiralling algorithm work?

A spiralling algorithm in C++ works by starting at the center of the array and moving in a spiral pattern, filling in each element with a specific value. The direction of the spiral can be clockwise or counterclockwise, and the size of the spiral can vary depending on the desired pattern.

3. What are the advantages of using a spiralling algorithm in C++?

One advantage of using a spiralling algorithm in C++ is its efficiency in creating visually interesting patterns. It also allows for easy customization of the spiral size and direction, making it a versatile tool for various applications.

4. Are there any limitations to using a spiralling algorithm in C++?

One limitation of using a spiralling algorithm in C++ is that it is primarily used for creating spiral patterns in 2-dimensional arrays. It may not be suitable for more complex data structures or algorithms.

5. Can a spiralling algorithm be used for other purposes besides creating patterns?

Yes, a spiralling algorithm in C++ can also be used for other purposes, such as searching or sorting data in a spiral pattern. It can also be adapted for use in other programming languages, depending on the specific application.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Replies
58
Views
3K
  • Programming and Computer Science
Replies
2
Views
304
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
19
Views
972
Replies
2
Views
999
  • Nuclear Engineering
Replies
2
Views
1K
Replies
2
Views
1K
Back
Top