From d264a7513e5d8fda5650c8639de71338244fb81a Mon Sep 17 00:00:00 2001 From: fonkou Date: Tue, 2 Dec 2025 15:23:22 +0100 Subject: [PATCH] add some files --- bintree.c | 6 +-- highscores.txt | 1 + numbers.c | 122 +++++++++++++++++++++++++++++-------------------- numbers.h | 1 + stack.h | 2 +- 5 files changed, 76 insertions(+), 56 deletions(-) diff --git a/bintree.c b/bintree.c index a9c8211..a94f2ef 100644 --- a/bintree.c +++ b/bintree.c @@ -10,11 +10,7 @@ // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). -TreeNode *addToTree(TreeNode *root, - const void *data, - size_t dataSize, - CompareFctType compareFct, - int *isDuplicate) { +TreeNode *addToTree(TreeNode *root,const void *data,size_t dataSize,CompareFctType compareFct,int *isDuplicate) { if (!root) { // allocate new node TreeNode *node = malloc(sizeof(TreeNode)); diff --git a/highscores.txt b/highscores.txt index e69de29..f73290f 100644 --- a/highscores.txt +++ b/highscores.txt @@ -0,0 +1 @@ +kamte;2990 diff --git a/numbers.c b/numbers.c index 66bc744..3e9ee17 100644 --- a/numbers.c +++ b/numbers.c @@ -1,9 +1,8 @@ #include -#include #include #include #include "numbers.h" -#include "bintree.h" + //TODO: getDuplicate und createNumbers implementieren /* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen. @@ -14,77 +13,100 @@ // 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. + + +// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. +// Returns NULL on errors. unsigned int *createNumbers(unsigned int len) { - if (len < 2) return NULL; + if (len < 2) return NULL; - unsigned int *numbers = malloc(len * sizeof(unsigned int)); - if (!numbers) return NULL; + // Allocate memory for the array + unsigned int *numbers = malloc(len * sizeof(unsigned int)); + if (!numbers) return NULL; - srand((unsigned int)time(NULL)); + // Initialize random number generator + srand((unsigned int)time(NULL)); - TreeNode *root = NULL; - unsigned int maxValue = 2 * len; + // We need to ensure len-1 unique numbers in range [1, 2*len] + unsigned int maxValue = 2 * len; + unsigned int uniqueCount = len - 1; // We'll generate len-1 unique values - unsigned int uniqueCount = len - 1; // we generate len-1 unique values - unsigned int i = 0; + // Generate unique numbers using a simple linear search approach + for (unsigned int i = 0; i < uniqueCount; i++) { + int isUnique; + unsigned int candidate; - // Fill len - 1 unique values using BST - while (i < uniqueCount) { - unsigned int r = (rand() % maxValue) + 1; - int isDup = 0; + // Keep generating until we find a unique number + do { + candidate = (rand() % maxValue) + 1; // Random number in [1, 2*len] + isUnique = 1; - root = addToTree(root, &r, sizeof(unsigned int), compareUnsigned, &isDup); + // Check if candidate already exists in our array so far + for (unsigned int j = 0; j < i; j++) { + if (numbers[j] == candidate) { + isUnique = 0; + break; + } + } + } while (!isUnique); - if (!isDup) { - numbers[i] = r; - i++; + numbers[i] = candidate; } - } - // Pick one number already in the list → duplicate it - unsigned int duplicateIndex = rand() % uniqueCount; - unsigned int duplicateValue = numbers[duplicateIndex]; + // Now we have len-1 unique numbers. Duplicate one of them. + // Choose a random index from the unique numbers + unsigned int duplicateIndex = rand() % uniqueCount; + unsigned int duplicateValue = numbers[duplicateIndex]; - numbers[len - 1] = duplicateValue; + // Add the duplicate at the last position + numbers[len - 1] = duplicateValue; - // Cleanup tree - clearTree(root); + // Shuffle the entire array to randomize the position of the duplicate + for (unsigned int i = 0; i < len; i++) { + unsigned int swapIndex = rand() % len; - // Shuffle array for randomness - for (unsigned int j = 0; j < len; j++) { - unsigned int k = rand() % len; - unsigned int tmp = numbers[j]; - numbers[j] = numbers[k]; - numbers[k] = tmp; - } + // Swap numbers[i] and numbers[swapIndex] + unsigned int temp = numbers[i]; + numbers[i] = numbers[swapIndex]; + numbers[swapIndex] = temp; + } - return numbers; + return numbers; } - // Returns only the only number in numbers which is present twice. Returns zero on errors. unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) { - if (!numbers || len < 2) return 0; + if (!numbers || len < 2) return 0; - // Make a copy because qsort modifies the array - unsigned int *copy = malloc(len * sizeof(unsigned int)); - if (!copy) return 0; + // Create a copy of the array since we need to sort it + unsigned int *copy = malloc(len * sizeof(unsigned int)); + if (!copy) return 0; - memcpy(copy, numbers, len * sizeof(unsigned int)); + memcpy(copy, numbers, len * sizeof(unsigned int)); - qsort(copy, len, sizeof(unsigned int), compareUnsigned); - - // Check adjacent elements for equality - for (unsigned int i = 0; i < len - 1; i++) { - if (copy[i] == copy[i + 1]) { - unsigned int result = copy[i]; - free(copy); - return result; + // Simple bubble sort implementation (no external function dependencies) + for (unsigned int i = 0; i < len - 1; i++) { + for (unsigned int j = 0; j < len - i - 1; j++) { + if (copy[j] > copy[j + 1]) { + // Swap if out of order + unsigned int temp = copy[j]; + copy[j] = copy[j + 1]; + copy[j + 1] = temp; + } + } } - } - free(copy); - return 0; // no duplicate found (should not happen) + // Now find the duplicate by checking adjacent elements + unsigned int duplicate = 0; + for (unsigned int i = 0; i < len - 1; i++) { + if (copy[i] == copy[i + 1]) { + duplicate = copy[i]; + break; + } + } + + free(copy); + return duplicate; } diff --git a/numbers.h b/numbers.h index 2315581..1a13749 100644 --- a/numbers.h +++ b/numbers.h @@ -4,6 +4,7 @@ // 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. + unsigned int *createNumbers(unsigned int len); // Returns only the only number in numbers which is present twice. Returns zero on errors. diff --git a/stack.h b/stack.h index f7d542d..c7fd5a5 100644 --- a/stack.h +++ b/stack.h @@ -6,7 +6,7 @@ This means that with each new element all other elements are pushed deeper into The latest element is taken from the stack. */ #include - +typedef struct Stack_node StackNode; //TODO: passenden Datentyp als struct anlegen // Pushes data as pointer onto the stack.