From 5532cec3ad880c13fb9dcfe4bfbbb7520d2012be Mon Sep 17 00:00:00 2001 From: Maximilian Ott Date: Sat, 13 Dec 2025 12:14:33 +0100 Subject: [PATCH] added compareFct and fillArray --- numbers.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/numbers.c b/numbers.c index fb4bf6a..63d4801 100644 --- a/numbers.c +++ b/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 NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // 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) { if (len < 2) @@ -43,10 +80,6 @@ unsigned int *createNumbers(unsigned int len) //TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max) - - - - position = rand() % len; duplicate_value = array[position];