-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynNum.c
More file actions
51 lines (44 loc) · 755 Bytes
/
dynNum.c
File metadata and controls
51 lines (44 loc) · 755 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int N;
if (argc == 1)
{
printf("No argument, Using default size, 5\n");
N = 5;
}
else
{
N = atoi( argv[1] );
if ( N <= 0 )
{
printf("Bad argument %s, Using default size 5\n", argv[1]);
N = 5;
}
}
printf("Finally %d\n", N);
// dyanically allocate array of size N
int *A;
int *used;
A = calloc(N, sizeof(int));
used = calloc(N+1, sizeof(int));
// populate the array
int k = 0, x, i, dup, try;
for(k = 0; k < N; k++)
{
try = 0;
do
{
x = rand()% N + 1;
}while(used[x]);
printf("%5d: %d %d\n", k, x, try)
A[k] = x;
used[x] = 1;
}
// print the array
for(k = 0; k < N; k++)
printf("%2d", A[k]);
printf("\n");
return 0;
}