[ create a new paste ] login | about

Link: http://codepad.org/hlLi0hnj    [ raw code | output | fork ]

novatech - C++, pasted on Jan 15:
/* print list of numbers in spiral direction */
#include <iostream>
using namespace std;
int i=0,x=25,y=10,spiral[100][100];
char direction = 'r';
void change_direction() {
     switch(direction) {
     case 'r':direction='d';break;
     case 'd':direction='l';break;
     case 'l':direction='u';break;
     case 'u':direction='r';break;
     }
}
int check_cell(int a,int b) {
 if (b >= y || b <0) { return 0; }
 else if (a >= x) { return 0; }
 else if (spiral[a][b] != 0) { return 0; }
 else { return 1; }
}
void move_cell(int &a,int &b) {
     int next_a = a, next_b = b;
     if (i == x*y-1) { return; }
     switch(direction) {
     case 'r': next_b = b + 1;break;
     case 'd': next_a = a + 1;break;
     case 'l': next_b = b - 1;break;
     case 'u': next_a = a - 1;break;
     }   
     if (check_cell(next_a,next_b)) {
         a = next_a;b = next_b;                              
     }
     else { 
          change_direction(); 
          move_cell(a,b);
          }
    return; 
}

int main() {
int j,a=0,b=0;
printf("%dx%d spiral number\n",x,y);
for (i = 0;i<x*y;i++) {
     spiral[a][b]=i+1;
     move_cell(a,b);
     }
for (i = 0;i < x;i++) {
         for (j = 0;j < y;j++) {
             printf("%d\t",spiral[i][j]);
         }
         printf("\n");
     }
return 0;
}


Output:
25x10 spiral number
1	2	3	4	5	6	7	8	9	10	
66	67	68	69	70	71	72	73	74	11	
65	124	125	126	127	128	129	130	75	12	
64	123	174	175	176	177	178	131	76	13	
63	122	173	216	217	218	179	132	77	14	
62	121	172	215	250	219	180	133	78	15	
61	120	171	214	249	220	181	134	79	16	
60	119	170	213	248	221	182	135	80	17	
59	118	169	212	247	222	183	136	81	18	
58	117	168	211	246	223	184	137	82	19	
57	116	167	210	245	224	185	138	83	20	
56	115	166	209	244	225	186	139	84	21	
55	114	165	208	243	226	187	140	85	22	
54	113	164	207	242	227	188	141	86	23	
53	112	163	206	241	228	189	142	87	24	
52	111	162	205	240	229	190	143	88	25	
51	110	161	204	239	230	191	144	89	26	
50	109	160	203	238	231	192	145	90	27	
49	108	159	202	237	232	193	146	91	28	
48	107	158	201	236	233	194	147	92	29	
47	106	157	200	235	234	195	148	93	30	
46	105	156	199	198	197	196	149	94	31	
45	104	155	154	153	152	151	150	95	32	
44	103	102	101	100	99	98	97	96	33	
43	42	41	40	39	38	37	36	35	34	


Create a new paste based on this one


Comments: