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

  • Thread starter Thread starter ron_jay
  • Start date Start date
  • Tags Tags
    Algorithm C++
AI Thread Summary
To create a spiraling algorithm in C++ for printing a specific pattern, start by using a 2D array initialized to zero. Begin filling the array from the center, employing loops to manage the direction of movement as you spiral outward. The algorithm requires careful handling of loop counters to ensure the correct placement of numbers, especially when transitioning between layers. Recursion can be utilized to simplify the process, allowing for dynamic adjustment of axes as the pattern expands. It's important to account for both odd and even dimensions of the array to maintain the correct structure.
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:
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...

Similar threads

Replies
19
Views
2K
Replies
2
Views
3K
Replies
3
Views
4K
Replies
7
Views
2K
Replies
6
Views
2K
Back
Top