diff --git a/doble b/doble index d07f80e..3827d3a 100755 Binary files a/doble and b/doble differ diff --git a/numbers.c b/numbers.c index 6fcd80c..dbdf078 100644 --- a/numbers.c +++ b/numbers.c @@ -11,23 +11,22 @@ * Duplizieren eines zufälligen Eintrags im Array. * in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */ -// Comparison function for unsigned integers used in the binary search tree +// Comparison function for unsigned integers used in BST and sorting operations. // Returns: <0 if a < b, 0 if a == b, >0 if a > b -// This function is passed to addToTree to determine ordering -int compareUnsignedInts(const void *a, const void *b) +// This is an internal function (static) since it's not part of the public API. +static int compareUnsignedInts(const void *a, const void *b) { unsigned int valA = *(const unsigned int *)a; unsigned int valB = *(const unsigned int *)b; - // Simple subtraction works for unsigned integers in this case - // (Note: in general subtraction can overflow, but our range is small) + // Safe comparison without overflow concerns for our range if (valA < valB) return -1; if (valA > valB) return 1; return 0; } // 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 the implementation of the binary search tree to check for possible duplicates while // creating random numbers. // // Algorithm: @@ -66,7 +65,7 @@ unsigned int *createNumbers(unsigned int len) int isDuplicate = 0; usedNumbers = addToTree(usedNumbers, &randomNumber, sizeof(unsigned int), compareUnsignedInts, &isDuplicate); - if (usedNumbers == NULL) + if (usedNumbers == NULL) // checks if addTOTree failed { free(numbers); return NULL; @@ -103,17 +102,7 @@ unsigned int *createNumbers(unsigned int len) return numbers; } -// Comparison function for qsort to sort unsigned integers in ascending order -// Returns: <0 if a < b, 0 if a == b, >0 if a > b -static int compareUnsigned(const void *a, const void *b) -{ - unsigned int valA = *(const unsigned int *)a; - unsigned int valB = *(const unsigned int *)b; - - if (valA < valB) return -1; - if (valA > valB) return 1; - return 0; -} + // Returns only the only number in numbers which is present twice. Returns zero on errors. // @@ -140,8 +129,8 @@ unsigned int getDuplicate(const unsigned int *numbers, unsigned int len) // Step 3: Copy the array memcpy(sorted, numbers, len * sizeof(unsigned int)); - // Step 4: Sort the copy - qsort(sorted, len, sizeof(unsigned int), compareUnsigned); + // Step 4: Sort the copy using the shared comparison function + qsort(sorted, len, sizeof(unsigned int), compareUnsignedInts); // Step 5: Compare adjacent elements to find the duplicate for (unsigned int i = 0; i < len - 1; i++) diff --git a/numbers.o b/numbers.o index b6079d0..22155d6 100644 Binary files a/numbers.o and b/numbers.o differ diff --git a/runTest_numbers b/runTest_numbers index 366d0f2..9d2da0d 100755 Binary files a/runTest_numbers and b/runTest_numbers differ