Display array content in GDB: P (INT [10]) *
I:
1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <time. h>
4
5 # define num 10
6 const int shuffle_initial [num] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
7 int shuffle_flags [num] = {0 };
8 int shuffle_final [num] = {0 };
9
10 int generate (void)
11 {
12 int I, K, J, L, temp;
13 srand (unsigned INT) Time (null ));
14 For (I = num; I --)
15 {
16 k = 0;
17 J = rand () % I + 1;
18 do
19 {
20 if (shuffle_flags [k ++] = 0)
21 -- J;
22} while (j );
23
24 -- K;
25 shuffle_final [k] = shuffle_initial [k];
26 shuffle_flags [k] = 1;
27 printf ("% d \ t", shuffle_final [k]);
28}
29 printf ("\ n ");
30 return 0;
31}
II,
1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <time. h>
4
5 # define num 10
6 int shuffle [num] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
7
8 int generate (void)
9 {
10 int I, K, temp;
11 srand (unsigned) Time (null ));
12 For (I = num; I! = 1 ;)
13 {
/* It should be noted that the random function is not the ansic standard, so the random function should not be used in programs compiled by GCC,
If you only use rand (), a random number is generated. The problem is that the random number generated each time is the same !!!
In the GCC environment, you need to use srand () to plant a seed. The seed should be different every time,
The random number sequence is the same every time when fixed seeds are planted.
*/
14 temp = rand () % I;
15 I --;
16 k = shuffle [temp];
17 shuffle [temp] = shuffle [I];
18 shuffle [I] = K;
19
20 printf ("% d \ t", k );
21}
22 printf ("% d \ n", shuffle [0]);
23
24 return 0;
25}