6 hours later, I have written a program that will generate Ulam's Spiral.
I am still testing it, since after all I wrote it today, but you can find the source here. Also, you'll need the headers for includes and stepping functions.
Or I mean I guess just download the executable. Using this will always produce the exact same thing, since currently the only way to change anything is by editing the code.

I feel like that merits some explanation. Well, two things.
1. maxlength is defined as whatever you want, but eventually used to set the values of Rows and Columns, both of which are declared as type const int. This means that once they are initialized, they are set. No, this would not work:
Well, actually, I guess it would. Except for the fact that to initialize an array, you need a constant value. Otherwise you'd have to dynamically allocate, clear, and reallocate memory blocks and blah, who wants to do that when you can just use a vector?
So neither this:
nor this:
unsigned long int SpiralArray[maxlength][maxlength];
would work. Both will simply give you compiler errors.
2. Formatting! It's a pain!
Consider this snippet, which is actually just a comment from my code...
/*
For formatting below, bear this in mind: Each number should have 6 "spaces" allocated to it. This is to keep things even. So, a number with one digit is printed "____n_" which means that 2 next to each other gives us "____n_____n_"  notice the even amount of space between them (5). Therefore, a number with 2 digits should have one fewer space in front of it, and so on. ALL NUMBERS must have ONE space AFTER them. So:
1 digit: ____n_ = 4 spaces, n, space
2 digits: ___nn_ = 3 spaces, n, space
3 digits: __nnn_ = 2 spaces, n, space
4 digits: _nnnn_ = 1 space , n, space
5 digits: nnnnn_ = no space, n, space
For numbers with more than 6 digits, you would need to edit the code so that numbers take up 7 spaces, and
add a new space before each n (so a 1 digit number has 5 instead of 4, etc).
This is scalable, it just needs editing. The whole idea of the spiral is for each number to take up the
exact same amount of space, or else there is no pattern to observe!!
*/
The last part is the most important. Think about it  why did the Excel spreadsheet work? Because it's a really powerful program that's somehow innately good for graphing? Nope. Although it is, I'll admit...
No, the reason Excel was good for the spiral is because each cell is the same size no matter what it contains. A cell where you put 1 is the exact same size as a cell where you put 1,000,000. You can even put numbers in that are too big for the cell, and they won't change size.
So, imagine that this arbitrary "6 spaces" (or 7, or however many you choose to expand it to include) is our working definition of a "cell," only instead of an XLSX, it's a TXT.
The height is luckily predetermined by the operating system's default plaintext font and character encoding, so we just need to set the width.
I chose 6 because I wanted a space after each number  at least one. Obviously some have much more. It's Ulam's Spiral, after all. There are supposed to be huge blank spaces!
A 1digit number needs 4 spaces in front of it because the last two characters in its "cell" must be itself and the ending space. A 2digit number gets 3 spaces because it takes up one more space...and so on, and so forth.
So to expand to a format where we'd be able to represent bigger numbers, we increase the amount of space in a "cell." For instance, to allow 8 digit numbers to be represented cleanly, we need 9 spaces per "cell." So let's do this:
1 digit: _______n_ = 7 spaces, n, space
2 digits: ______nn_ = 6 spaces, n, space
3 digits: _____nnn_ = 5 spaces, n, space
4 digits: ____nnnn_ = 4 spaces, n, space
5 digits: ___nnnnn_ = 3 spaces, n, space
6 digits: __nnnnnn_ = 2 spaces, n, space
7 digits: _nnnnnnn_ = 1 space , n, space
8 digits: nnnnnnnn_ = no space, n, space
Easy enough. The hard part is writing it in properly!
As always, keeping you updated.
JB
I am still testing it, since after all I wrote it today, but you can find the source here. Also, you'll need the headers for includes and stepping functions.
Or I mean I guess just download the executable. Using this will always produce the exact same thing, since currently the only way to change anything is by editing the code.

I feel like that merits some explanation. Well, two things.
1. maxlength is defined as whatever you want, but eventually used to set the values of Rows and Columns, both of which are declared as type const int. This means that once they are initialized, they are set. No, this would not work:
unsigned long int n;
cout >> "enter maxlength";
cin << n;
#define maxlength n
Well, actually, I guess it would. Except for the fact that to initialize an array, you need a constant value. Otherwise you'd have to dynamically allocate, clear, and reallocate memory blocks and blah, who wants to do that when you can just use a vector?
So neither this:
unsigned long int SpiralArray[n][n];
nor this:
unsigned long int SpiralArray[maxlength][maxlength];
would work. Both will simply give you compiler errors.
2. Formatting! It's a pain!
Consider this snippet, which is actually just a comment from my code...
/*
For formatting below, bear this in mind: Each number should have 6 "spaces" allocated to it. This is to keep things even. So, a number with one digit is printed "____n_" which means that 2 next to each other gives us "____n_____n_"  notice the even amount of space between them (5). Therefore, a number with 2 digits should have one fewer space in front of it, and so on. ALL NUMBERS must have ONE space AFTER them. So:
1 digit: ____n_ = 4 spaces, n, space
2 digits: ___nn_ = 3 spaces, n, space
3 digits: __nnn_ = 2 spaces, n, space
4 digits: _nnnn_ = 1 space , n, space
5 digits: nnnnn_ = no space, n, space
For numbers with more than 6 digits, you would need to edit the code so that numbers take up 7 spaces, and
add a new space before each n (so a 1 digit number has 5 instead of 4, etc).
This is scalable, it just needs editing. The whole idea of the spiral is for each number to take up the
exact same amount of space, or else there is no pattern to observe!!
*/
The last part is the most important. Think about it  why did the Excel spreadsheet work? Because it's a really powerful program that's somehow innately good for graphing? Nope. Although it is, I'll admit...
No, the reason Excel was good for the spiral is because each cell is the same size no matter what it contains. A cell where you put 1 is the exact same size as a cell where you put 1,000,000. You can even put numbers in that are too big for the cell, and they won't change size.
So, imagine that this arbitrary "6 spaces" (or 7, or however many you choose to expand it to include) is our working definition of a "cell," only instead of an XLSX, it's a TXT.
The height is luckily predetermined by the operating system's default plaintext font and character encoding, so we just need to set the width.
I chose 6 because I wanted a space after each number  at least one. Obviously some have much more. It's Ulam's Spiral, after all. There are supposed to be huge blank spaces!
A 1digit number needs 4 spaces in front of it because the last two characters in its "cell" must be itself and the ending space. A 2digit number gets 3 spaces because it takes up one more space...and so on, and so forth.
So to expand to a format where we'd be able to represent bigger numbers, we increase the amount of space in a "cell." For instance, to allow 8 digit numbers to be represented cleanly, we need 9 spaces per "cell." So let's do this:
1 digit: _______n_ = 7 spaces, n, space
2 digits: ______nn_ = 6 spaces, n, space
3 digits: _____nnn_ = 5 spaces, n, space
4 digits: ____nnnn_ = 4 spaces, n, space
5 digits: ___nnnnn_ = 3 spaces, n, space
6 digits: __nnnnnn_ = 2 spaces, n, space
7 digits: _nnnnnnn_ = 1 space , n, space
8 digits: nnnnnnnn_ = no space, n, space
Easy enough. The hard part is writing it in properly!
As always, keeping you updated.
JB
No comments:
Post a Comment