I'm trying to write a code that outputs a number spiral, but it outputs very large numbers, and I can't find the problem.

The number spiral is a square with a odd-numbered side length, starts with 0 in the middle, and spirals out counterclockwise until there are a total of [the odd number squared] numbers.
Like this:

16 15 14 13 12

17 4 3 2 11

18 5 0 1 10

19 6 7 8 9

20 21 22 23 24

I'm imagining the first value of array a to be its vertical coordinate, horizontal with the second.
Up is positive [first value of a], right is positive [second value of a].
v and h is the vertical and horizontal coordinate of where the "next number goes
here pointer thingy" (NNGHPT) goes, respectively.

My approach is to fill the slot of where NNHGPT is with i, which decreases every time a number is filled, and decide where NNGHPT goes next.
To decide where NNGHPT goes next, it will first decide if the next slot in the direction it is going (dir 1=left, dir 2=up, dir 3=right, dir4= down) is already filled or not in the array, if so, it will turn right(in its perspective), and then move in that direction. If the next slot in the direction it is going is valid, it goes there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n][n]={ },i=n*n-1,v=n-1,h=n-1,dir=1;
        while(i>=0)
        {
            a[v][h]==i;
            i--;
            if(dir==1)
            {
                if(h-1<0||a[v][h-1]!=0)
                {
                    dir=2;
                    v--;
                }
                else h--;
            }
            if(dir==2)
            {
                if(v-1<0||a[v-1][h]!=0)
                {
                    dir=3;
                    h++;
                }
                else v--;
            }
            if(dir==3)
            {
                if(h+1>n-1||a[v][h+1]!=0)
                {
                    dir=4;
                    v++;
                }
                else h++;
            }
            if(dir==4)
            {
                if(v+1>n-1||a[v+1][h]!=0)
                {
                    dir=1;
                    h--;
                }
                else v++;
            }
        }
        for(int j=0;j<n;j++)
        {
            for(int k=0;j<n;k++)
                cout<<a[j][k];
            cout<<endl;
        }
    }
}

Am I over-complicating things again? If so, where and how?
(Programs outputting huge numbers will be the bane of my sanity, ugh.)
Last edited on
> a[v][h]==i;
Perhaps you should try assignment, instead of comparison.

Using the tools properly to tell you when you're doing dump stuff.
1
2
3
4
5
$ g++ -Wall -Wextra proj.cpp
proj.cpp: In function ‘int main()’:
proj.cpp:11:20: warning: statement has no effect [-Wunused-value]
             a[v][h]==i;
                    ^
> for(int k=0;j<n;k++)
Also, watch out for mis-edits when copy/pasting for loops!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
    struct {
        const char *dir;
        int dh, dv;
    } dirns[] = {
        { "L", -1,  0 },  // left
        { "U",  0, -1 },  // up
        { "R", +1,  0 },  // right
        { "D",  0, +1 },  // down
    };
    int n;
    while(cin>>n)
    {
        int a[n][n]={0},i=n*n-1,v=n-1,h=n-1,dir=0;
        while(i>=0)
        {
            cout << "Update: a[" << v << "][" << h << "]\n";
            a[v][h]=i;
            i--;
            int nexth = h + dirns[dir].dh;
            int nextv = v + dirns[dir].dv;
            if ( nexth < 0 || nexth >= n ||
                 nextv < 0 || nextv >= n ||
                 a[nextv][nexth] != 0 ) {
                dir = (dir + 1) % 4;
                cout << "Now going " << dirns[dir].dir << endl;
                h += dirns[dir].dh;
                v += dirns[dir].dv;
            } else {
                h = nexth;
                v = nextv;
            }
        }
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
                cout<<a[j][k] << " ";
            cout<<endl;
        }
    }
}
And VLA watch!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

#define SIDE for ( int s = side; s; s-- )

int main()
{
   int n;
   cout << "Enter n: ";   cin >> n;
   vector< vector<int> > a( n, vector<int>( n, 0 ) );
   for ( int side = n - 1, i = side, j = side, value = n * n; side > 0; side -= 2, i--, j-- )
   {
      SIDE a[i][j--] = --value;
      SIDE a[i--][j] = --value;
      SIDE a[i][j++] = --value;
      SIDE a[i++][j] = --value;
   }
   
   int width = 1;
   for ( int m = n * n - 1; m; m /= 10 ) width++;
   for ( auto row : a )
   {
      for ( int e : row ) cout << setw( width ) << e;
      cout << '\n';
   }
}


Enter n: 5
 16 15 14 13 12
 17  4  3  2 11
 18  5  0  1 10
 19  6  7  8  9
 20 21 22 23 24

Enter n: 10
 81 80 79 78 77 76 75 74 73 72
 82 49 48 47 46 45 44 43 42 71
 83 50 25 24 23 22 21 20 41 70
 84 51 26  9  8  7  6 19 40 69
 85 52 27 10  1  0  5 18 39 68
 86 53 28 11  2  3  4 17 38 67
 87 54 29 12 13 14 15 16 37 66
 88 55 30 31 32 33 34 35 36 65
 89 56 57 58 59 60 61 62 63 64
 90 91 92 93 94 95 96 97 98 99
Last edited on
Topic archived. No new replies allowed.