small fix
This commit is contained in:
parent
40a6501fde
commit
9a8a346a1e
@ -1,4 +1,4 @@
|
|||||||
*createNumbers:
|
*createNumbers: -> numbers array gets filled with random entry with only one dublicate
|
||||||
|
|
||||||
-> check if len is greater than 2
|
-> check if len is greater than 2
|
||||||
-> create new array numbers, use malloc and check if correct
|
-> create new array numbers, use malloc and check if correct
|
||||||
@ -7,7 +7,10 @@
|
|||||||
-> add new value to tree using addToTree and rand()
|
-> add new value to tree using addToTree and rand()
|
||||||
-> addToTree sets isDup to 1, if value already exists
|
-> addToTree sets isDup to 1, if value already exists
|
||||||
-> if value does not already exist -> add to numbers[]
|
-> if value does not already exist -> add to numbers[]
|
||||||
->
|
-> stops at numbers[len -2]
|
||||||
|
static dublicateRandomEntry:
|
||||||
|
-> numbers[len-1] is filled with random already existing number
|
||||||
|
-> numbers[len -1] gets switched with other random entry -> dublicate is placed randwom in entry; without the switch, dublicate would always be at the end
|
||||||
|
|
||||||
|
|
||||||
getDublicate:
|
getDublicate:
|
||||||
@ -15,7 +18,7 @@ getDublicate:
|
|||||||
-> numbers (Zeiger != NULL) und len (min. 2) check
|
-> numbers (Zeiger != NULL) und len (min. 2) check
|
||||||
-> define new array numbersCopy
|
-> define new array numbersCopy
|
||||||
-> copy numbers to numbersCopy
|
-> copy numbers to numbersCopy
|
||||||
-> simple loop to copy each element number -> numbersCopy
|
-> simple loop to copy each element from numbers to numbersCopy
|
||||||
-> sort numbersCopy with qsort
|
-> sort numbersCopy with qsort
|
||||||
-> compare each element of numbersCopy with next element (if numbersCopy[i] == numbersCopy[i+1] -> dublicate was found, because same values are right next to each other)
|
-> compare each element of numbersCopy with next element (if numbersCopy[i] == numbersCopy[i+1] -> dublicate was found, because same values are right next to each other)
|
||||||
-> return found dublicate
|
-> return found dublicate
|
||||||
|
|||||||
@ -15,12 +15,10 @@ static void dublicateRandomEntry (unsigned int *numbers, unsigned int len)
|
|||||||
{
|
{
|
||||||
if (numbers && len > 2)
|
if (numbers && len > 2)
|
||||||
{
|
{
|
||||||
unsigned int dubIndx = rand() % len;
|
unsigned int dubIndx = rand() % (len - 1);
|
||||||
unsigned int copyIndx;
|
unsigned int copyIndx;
|
||||||
|
|
||||||
do {
|
copyIndx = rand() % len;
|
||||||
copyIndx = rand() % len;
|
|
||||||
} while (copyIndx == dubIndx);
|
|
||||||
|
|
||||||
numbers[len - 1] = numbers[dubIndx];
|
numbers[len - 1] = numbers[dubIndx];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user