numbers
This commit is contained in:
parent
f46e9b06d9
commit
8449734941
36
numbers.c
36
numbers.c
@ -32,10 +32,11 @@ unsigned int* createNumbers(unsigned int len)
|
||||
|
||||
int isDuplicate = 0;
|
||||
int number = rand() % (2 * len) + 1;
|
||||
int* array = malloc(len*sizeof(int));
|
||||
|
||||
numbers = addToTree(NULL, &number, sizeof(int), compareFct, &isDuplicate);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
array[0] = number;
|
||||
for (int i = 1; i < len; i++)
|
||||
{
|
||||
number = rand() % (2 * len) + 1;
|
||||
addToTree(numbers, &number, sizeof(int), compareFct, &isDuplicate);
|
||||
@ -43,28 +44,19 @@ unsigned int* createNumbers(unsigned int len)
|
||||
{
|
||||
isDuplicate = 0;
|
||||
i--;
|
||||
}else{
|
||||
array[i] = number;
|
||||
}
|
||||
}
|
||||
|
||||
int duplicatePosition = rand() % len;
|
||||
TreeNode *duplicate = nextTreeData(numbers);
|
||||
int duplicatePosition1 = rand() % len;
|
||||
int duplicatePosition2 = rand() % len;
|
||||
|
||||
for (int i = 0; i < duplicatePosition; i++)
|
||||
{
|
||||
duplicate = nextTreeData(NULL);
|
||||
while(duplicatePosition2 == duplicatePosition1){
|
||||
duplicatePosition2 = rand() % len;
|
||||
}
|
||||
|
||||
addToTree(numbers, duplicate->data, sizeof(int), compareFct, NULL);
|
||||
|
||||
TreeNode* treeData = nextTreeData(numbers);
|
||||
int* array = malloc(len*sizeof(int));
|
||||
array[0] = treeData->data;
|
||||
|
||||
for (int i = 1; i < len; i++)
|
||||
{
|
||||
treeData = nextTreeData(NULL);
|
||||
array[i] = treeData->data;
|
||||
}
|
||||
array[duplicatePosition2] = array[duplicatePosition1];
|
||||
|
||||
return array;
|
||||
}
|
||||
@ -72,5 +64,13 @@ unsigned int* createNumbers(unsigned int len)
|
||||
// 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)
|
||||
{
|
||||
qsort(numbers, len, sizeof(int), compareFct);
|
||||
|
||||
for(int i = 0; i < len-1; i++){
|
||||
if(numbers[i] == numbers[i+1]){
|
||||
return numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user