generated from freudenreichan/info2Praktikum-DobleSpiel
added compareFct and fillArray
This commit is contained in:
parent
c47a735954
commit
5532cec3ad
41
numbers.c
41
numbers.c
@ -13,6 +13,43 @@
|
|||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
static int compareFct(const void *argument1, const void *argument2)
|
||||||
|
{
|
||||||
|
unsigned int x = *(const unsigned int *)argument1;
|
||||||
|
unsigned int y = *(const unsigned int *)argument2;
|
||||||
|
|
||||||
|
if (x < y) return -1;
|
||||||
|
if (x > y) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fillArray(unsigned int *array, unsigned int len)
|
||||||
|
{
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int isDuplicate;
|
||||||
|
unsigned int value;
|
||||||
|
unsigned int i = 0;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
value = rand() % (2 * len) + 1;
|
||||||
|
|
||||||
|
root = addToTree(root, &value, sizeof(unsigned int), compareFct, &isDuplicate);
|
||||||
|
|
||||||
|
if (isDuplicate == 0)
|
||||||
|
{
|
||||||
|
array[i] = value;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
// Bei Duplikat: nochmal versuchen, kein Inkrement
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
if (len < 2)
|
if (len < 2)
|
||||||
@ -43,10 +80,6 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
|
|
||||||
//TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max)
|
//TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
position = rand() % len;
|
position = rand() % len;
|
||||||
duplicate_value = array[position];
|
duplicate_value = array[position];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user