Repository navigation
Expand file tree
/
Copy pathgenerator.c
More file actions
90 lines (77 loc) · 2.17 KB
/
Copy pathgenerator.c
File metadata and controls
90 lines (77 loc) · 2.17 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
/**
* Swap two integer values
* as taken from the lecture
*/
static void swap(int* x, int* y) {
int tmp = *x;
*x = *y;
*y = tmp;
}
/**
* Fisher-Yates shuffle
* https://stackoverflow.com/questions/6127503/shuffle-array-in-c
*/
static void shuffle(int* xs, const unsigned int N) {
for (unsigned int i = 0; i < N - 1; i++)
swap(xs + i, xs + (i + rand() / (RAND_MAX / (N - i) + 1)));
}
/**
* Reverse an array
*/
static void reverse(int* xs, const unsigned int N) {
for (unsigned int i = 0; i < N / 2; i++)
swap(xs + i, xs + N - 1 - i);
}
/**
* Write array of integers to file
* as taken from the lecture
*/
static void write_file(const int* xs, const int N, const char* path) {
// unsafe string formatting
// https://stackoverflow.com/questions/804288/creating-c-formatted-strings-not-printing-them
char filename[64];
sprintf(filename, "%s/%d", path, N);
FILE* file = fopen(filename, "w");
fwrite(xs, sizeof(int), N, file);
fclose(file);
}
int main(int argc, char** argv) {
// parse input
if (argc != 5) {
printf("Usage: ./generator N path dist seed\n");
printf(" N - input size\n");
printf(" path - directory to store file in\n");
printf(" dist - 0 = random order, 1 = random ints, 2 = sorted, 3 = reversed\n");
printf(" seed - for the random number generator, 0 = current time\n");
exit(-1);
}
const int N = atoi(argv[1]);
const char* path = argv[2];
const char dist = atoi(argv[3]);
const int seed = atoi(argv[4]);
// seed RNG
if (seed <= 0) srand(time(NULL));
else srand(seed);
// generate data
int* xs = (int*) malloc(N * sizeof(int));
// random integers
if (dist == 1) {
for (int i = 0; i < N; i++)
xs[i] = rand();
// random order
} else
for (int i = 0; i < N; i++)
xs[i] = i;
if (dist == 0)
shuffle(xs, N);
// reversed order
else if (dist == 3)
reverse(xs, N);
// write output
write_file(xs, N, path);
return 0;
}