5 |
|
* cnt.c - simple counting program. |
6 |
|
* |
7 |
|
* 2/1/88 |
8 |
+ |
* |
9 |
+ |
* Added -s (shuffle) option April 2022 |
10 |
|
*/ |
11 |
|
|
12 |
|
#include <stdio.h> |
13 |
+ |
#include <time.h> |
14 |
+ |
#include "random.h" |
15 |
|
|
16 |
+ |
#define MAXDIM 50 |
17 |
|
|
13 |
– |
int n[50]; |
18 |
|
char buf[256]; |
19 |
|
|
20 |
|
|
21 |
< |
main(argc, argv) |
22 |
< |
int argc; |
23 |
< |
char *argv[]; |
21 |
> |
/* Encode integer in string and return pointer to end */ |
22 |
> |
static char * |
23 |
> |
tack( |
24 |
> |
char *b, |
25 |
> |
int i |
26 |
> |
) |
27 |
|
{ |
28 |
< |
int a; |
22 |
< |
|
23 |
< |
argv++; argc--; |
24 |
< |
for (a = 0; a < argc; a++) |
25 |
< |
n[a] = atoi(argv[a]); |
26 |
< |
n[a] = 0; |
27 |
< |
loop(n, buf); |
28 |
< |
|
29 |
< |
exit(0); |
30 |
< |
} |
31 |
< |
|
32 |
< |
|
33 |
< |
char * |
34 |
< |
tack(b, i) |
35 |
< |
register char *b; |
36 |
< |
register int i; |
37 |
< |
{ |
38 |
< |
register char *cp; |
28 |
> |
char *cp; |
29 |
|
char *res; |
30 |
|
|
31 |
|
*b++ = '\t'; |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
< |
loop(n, b) |
53 |
< |
int *n; |
54 |
< |
char *b; |
52 |
> |
/* Loop over dimensions, spitting out buffer after each increment */ |
53 |
> |
static void |
54 |
> |
loop( |
55 |
> |
int *n, |
56 |
> |
char *b |
57 |
> |
) |
58 |
|
{ |
59 |
|
int i; |
60 |
|
|
65 |
|
} |
66 |
|
for (i = 0; i < n[0]; i++) |
67 |
|
loop(n+1, tack(b, i)); |
68 |
+ |
} |
69 |
+ |
|
70 |
+ |
|
71 |
+ |
/* Shuffle all possible output strings and spit out randomly */ |
72 |
+ |
static void |
73 |
+ |
shuffle( |
74 |
+ |
int *n |
75 |
+ |
) |
76 |
+ |
{ |
77 |
+ |
int sub[MAXDIM]; |
78 |
+ |
int ndim; |
79 |
+ |
int alen; |
80 |
+ |
int *myshuf; |
81 |
+ |
int i, j; |
82 |
+ |
|
83 |
+ |
alen = 1; /* allocate shuffle index array */ |
84 |
+ |
for (j = 0; n[j]; j++) |
85 |
+ |
if ((alen *= n[j]) < 0) |
86 |
+ |
exit(1); |
87 |
+ |
|
88 |
+ |
myshuf = (int *)malloc(alen*sizeof(int)); |
89 |
+ |
if (myshuf == NULL) { |
90 |
+ |
fputs("Insufficient memory for shuffle\n", stderr); |
91 |
+ |
exit(1); |
92 |
+ |
} |
93 |
+ |
for (i = alen; i--; ) /* initialize in any order */ |
94 |
+ |
myshuf[i] = i; |
95 |
+ |
/* get unique starting point */ |
96 |
+ |
srandom((long)time(0)); |
97 |
+ |
/* perform Fisher-Yates shuffle */ |
98 |
+ |
for (i = 0; i < alen-1; i++) { |
99 |
+ |
int ix = random()%(alen-i) + i; |
100 |
+ |
int ndx = myshuf[i]; |
101 |
+ |
myshuf[i] = myshuf[ix]; |
102 |
+ |
myshuf[ix] = ndx; |
103 |
+ |
} |
104 |
+ |
/* put randomly indexed output */ |
105 |
+ |
for (i = alen; i--; ) { |
106 |
+ |
int aval = myshuf[i]; |
107 |
+ |
for (j = 0; n[j+1]; j++) { |
108 |
+ |
printf("\t%d", aval % n[j]); |
109 |
+ |
aval /= n[j]; |
110 |
+ |
} |
111 |
+ |
printf("\t%d\n", aval); |
112 |
+ |
} |
113 |
+ |
free(myshuf); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
|
117 |
+ |
int |
118 |
+ |
main( |
119 |
+ |
int argc, |
120 |
+ |
char *argv[] |
121 |
+ |
) |
122 |
+ |
{ |
123 |
+ |
char *prog = argv[0]; |
124 |
+ |
int doshuffle = 0; |
125 |
+ |
int n[MAXDIM]; |
126 |
+ |
int a; |
127 |
+ |
|
128 |
+ |
argv++; argc--; |
129 |
+ |
if (argc <= 0) |
130 |
+ |
goto userr; |
131 |
+ |
if (argv[0][0] == '-' && argv[0][1] == 's') { |
132 |
+ |
doshuffle = 1; |
133 |
+ |
argv++; argc--; |
134 |
+ |
} |
135 |
+ |
for (a = 0; a < argc; a++) |
136 |
+ |
if ((n[a] = atoi(argv[a])) <= 1) |
137 |
+ |
goto userr; |
138 |
+ |
n[a] = 0; |
139 |
+ |
if (!a) |
140 |
+ |
goto userr; |
141 |
+ |
|
142 |
+ |
if (doshuffle) |
143 |
+ |
shuffle(n); |
144 |
+ |
else |
145 |
+ |
loop(n, buf); |
146 |
+ |
|
147 |
+ |
return(0); |
148 |
+ |
userr: |
149 |
+ |
fputs("Usage: ", stderr); |
150 |
+ |
fputs(prog, stderr); |
151 |
+ |
fputs(" [-s] N0 [N1 ..]\n", stderr); |
152 |
+ |
return(1); |
153 |
|
} |