added first function in numbers.c

This commit is contained in:
Thilo 2025-12-10 17:10:34 +01:00
parent 6dc857cc63
commit b75411fd99
4 changed files with 33 additions and 10 deletions

View File

@ -35,7 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
tets_sources = test_stack.c stack.c test_sources = test_stack.c stack.c
test_objects = $(test_sources:.c=.o) test_objects = $(test_sources:.c=.o)
test_runner: $(test_objects) test_runner: $(test_objects)

View File

@ -6,8 +6,7 @@
#include "bintree.h" #include "bintree.h"
//TODO: getDuplicate und createNumbers implementieren //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. * Duplizieren eines zufälligen Eintrags im Array.
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */ * in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
@ -16,6 +15,28 @@
// creating random numbers. // creating random numbers.
unsigned int *createNumbers(unsigned int len) 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;
} }

View File

@ -78,3 +78,4 @@ void clearStack(StackNode *stack)
current = next; current = next;
} }
} }

View File

@ -31,7 +31,7 @@ void test_on_empty_stack(void)
void test_push(void) void test_push(void)
{ {
int a[3] = 0; int a[3] = {0};
StackNode *stack = NULL; StackNode *stack = NULL;
for (int i = 1; i < 4; i++) 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]); 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) 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); else printf("Test pop: FAIL! - Stack is not empty! Head: %p\n", (void*)stack);
stack = clearStack(stack); clearStack(stack);
} }
void test_top(void) void test_top(void)
@ -99,7 +99,7 @@ void test_top(void)
else printf("Test top: FAIL! - Expected 1, was %p\n", b); else printf("Test top: FAIL! - Expected 1, was %p\n", b);
stack = clearStack(stack); clearStack(stack);
} }
void test_clearStack(void) void test_clearStack(void)
@ -120,7 +120,8 @@ void test_clearStack(void)
stack = push(stack, var_push); stack = push(stack, var_push);
} }
stack = clearStack(stack); clearStack(stack);
void *a = top(stack); void *a = top(stack);
if (a == NULL) printf("Test clear: SUCESS!\n"); if (a == NULL) printf("Test clear: SUCESS!\n");