0

The following are some examples that you may see in an intro to programming course:

Print the elements of an nxn array...

  • Spiraling in from the outside
  • Spiraling out from the center
  • Zig zagging from the top left corner
  • Going right, then left, then right, then left(like a printer)
  • Etc.

These are fairly standard to whip up. However, the end result is typically ugly. It usually comes down to a bunch of nested for loops, with if statements sewn throughout, tailored to that specific transversal pattern. Heaven forbid you need to modify that, and account for a "width of the brush" or something.

Is there a cleaner pattern for this?

I've considered using some sort of automata, with directions as states. That seems plausible. However, I was wondering if there was something else, possibly more elegant/canonical, and less homebrew. We do talk about these examples all the time, after all!

Thoughts?

Kyle
  • 37

0 Answers0