-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.c
More file actions
60 lines (48 loc) · 1.21 KB
/
Array.c
File metadata and controls
60 lines (48 loc) · 1.21 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
/* ========================================================================= *
* Array generator
* Implementation of the Array generator interface
* ========================================================================= */
#include <stdlib.h>
#include <stddef.h>
#include "Array.h"
static const int MAX_START = 1001;
static const int UPPER_BOUND = 1001;
int* createSortedArray(size_t length)
{
int* array = (int*) malloc(length * sizeof(int));
if (!array)
return NULL;
int shift = rand() % MAX_START;
for (size_t i = 0; i < length; i++)
{
array[i] = shift + i;
}
return array;
}
int* createDecreasingArray(size_t length)
{
int* array = createSortedArray(length);
if (!array)
return NULL;
int tmp;
size_t i = 0, j = length-1;
while (i < j)
{
// Swap array[i] and array[j]
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
return array;
}
int* createRandomArray(size_t length)
{
int* array = (int*) malloc(length * sizeof(int));
if (!array)
return NULL;
for (size_t i = 0; i < length; i++)
array[i] = rand() % UPPER_BOUND;
return array;
}