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

  • Context: C/C++ 
  • Thread starter Thread starter ron_jay
  • Start date Start date
  • Tags Tags
    Algorithm C++
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ algorithm to print a spiraling pattern of numbers starting from the center of a 2D array. Participants explore various approaches to implement this algorithm, including recursion and iterative methods, while addressing challenges related to indexing and filling the array correctly.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using a 2D array and filling it from the center with loops, noting that generating the pattern line-by-line is manageable but requires careful consideration of loop counters.
  • Another participant discusses the complexity of starting the numbering from the center, proposing a recursive approach that utilizes a "virtual motion analogy" to navigate the 2D matrix, although they express uncertainty about how to adapt the axes during recursion.
  • A different participant recommends using an offset value for the center and outlines a specific indexing pattern to fill the array, emphasizing the need to handle cases where the array dimensions are odd.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with no consensus on a single method. The discussion includes differing opinions on the effectiveness of recursion versus iterative methods and how best to manage the indexing for filling the array.

Contextual Notes

Participants mention challenges related to the indexing of the array and the implications of odd versus even dimensions, but these issues remain unresolved within the discussion.

Who May Find This Useful

Readers interested in algorithm design, C++ programming, and pattern generation may find this discussion relevant.

ron_jay
Messages
81
Reaction score
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
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!
 
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...
 
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:

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 58 ·
2
Replies
58
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K