From b75411fd9994a034f6705eefb41c8c50265bf2a8 Mon Sep 17 00:00:00 2001 From: Thilo Date: Wed, 10 Dec 2025 17:10:34 +0100 Subject: [PATCH] added first function in numbers.c --- makefile | 2 +- numbers.c | 25 +++++++++++++++++++++++-- stack.c | 3 ++- test_stack.c | 13 +++++++------ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/makefile b/makefile index 425a192..40c4667 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- -tets_sources = test_stack.c stack.c +test_sources = test_stack.c stack.c test_objects = $(test_sources:.c=.o) test_runner: $(test_objects) diff --git a/numbers.c b/numbers.c index f59d9a2..776a47f 100644 --- a/numbers.c +++ b/numbers.c @@ -6,8 +6,7 @@ #include "bintree.h" //TODO: getDuplicate und createNumbers implementieren -/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen. - * Sicherstellen, dass beim Befüllen keine Duplikate entstehen. +/* Sicherstellen, dass beim Befüllen keine Duplikate entstehen. * Duplizieren eines zufälligen Eintrags im Array. * in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */ @@ -16,7 +15,29 @@ // creating random numbers. unsigned int *createNumbers(unsigned int len) { + unsigned int *array[len -1]; + unsigned int position; + unsigned int duplicate; + unsigned int pos_dup; + srand(time(NULL)); + + for (unsigned int i = 1; i <= len; i++) + { + array[i-1] = rand() % (2*len) - 1; + } + + //TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max) + + position = rand() % len - 1; + duplicate = array[position]; + pos_dup = rand() % len - 1; + + if (pos_dup != position) + array[pos_dup] = array[position]; + + return array; + } // Returns only the only number in numbers which is present twice. Returns zero on errors. diff --git a/stack.c b/stack.c index 8e7076e..ab75e6a 100644 --- a/stack.c +++ b/stack.c @@ -77,4 +77,5 @@ void clearStack(StackNode *stack) current = next; } -} \ No newline at end of file +} + diff --git a/test_stack.c b/test_stack.c index 7acb6e9..89cde13 100644 --- a/test_stack.c +++ b/test_stack.c @@ -31,7 +31,7 @@ void test_on_empty_stack(void) void test_push(void) { - int a[3] = 0; + int a[3] = {0}; StackNode *stack = NULL; for (int i = 1; i < 4; i++) @@ -62,7 +62,7 @@ void test_push(void) else printf("Test pus: FAILED - Expected: all 1, Was: %d, %d, %d\n", a[0], a[1], a[2]); - stack = clearStack(stack); + clearStack(stack); } void test_pop(void) @@ -84,7 +84,7 @@ void test_pop(void) else printf("Test pop: FAIL! - Stack is not empty! Head: %p\n", (void*)stack); - stack = clearStack(stack); + clearStack(stack); } void test_top(void) @@ -99,7 +99,7 @@ void test_top(void) else printf("Test top: FAIL! - Expected 1, was %p\n", b); - stack = clearStack(stack); + clearStack(stack); } void test_clearStack(void) @@ -120,11 +120,12 @@ void test_clearStack(void) stack = push(stack, var_push); } - stack = clearStack(stack); + clearStack(stack); + void *a = top(stack); if (a == NULL) printf("Test clear: SUCESS!\n"); - else printf("Test clear: FAILED! - Expected: NULL, Was:%p\n", a); + else printf("Test clear: FAILED! - Expected: NULL, Was: %p\n", a); } int main()