Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a816abfe04 | ||
|
|
74b541a1a0 |
Binary file not shown.
@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
|
|||||||
userInput = inputNumber("Welche Zahl kommt doppelt vor: ");
|
userInput = inputNumber("Welche Zahl kommt doppelt vor: ");
|
||||||
measuredSeconds = stopTimer();
|
measuredSeconds = stopTimer();
|
||||||
|
|
||||||
duplicate = getDuplicate(numbers, numberOfElements);
|
duplicate = getDuplicate(numbers, numberOfElements+1);
|
||||||
|
|
||||||
// check result and update highscores
|
// check result and update highscores
|
||||||
if(userInput == duplicate)
|
if(userInput == duplicate)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
|||||||
doble : main.o $(program_obj_files)
|
doble : main.o $(program_obj_files)
|
||||||
$(CC) $(FLAGS) $^ -o doble
|
$(CC) $(FLAGS) $^ -o doble
|
||||||
|
|
||||||
$(program_obj_files): %.o: %.c
|
$(program_obj_filesobj_files): %.o: %.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -5,22 +5,59 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: getDuplicate und createNumbers implementieren
|
//Speicher für Array erstellen, zufällige Zahlen von 1-2xlen erzeugen, mittels Binärbaum checken, ob Zahlen einzigartig sind
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
//Eine Zahl duplizieren, an zufälliger Stelle einfügen und die Zahl an der Stelle ans Ende schieben
|
||||||
* 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. */
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// creating random numbers.
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
unsigned int *numbers = malloc (sizeof(unsigned int) * len);
|
||||||
|
unsigned int upperLimit = len * 2;
|
||||||
|
int isDuplicate = 0;
|
||||||
|
TreeNode *binTree = NULL;
|
||||||
|
|
||||||
}
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
do
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
|
numbers[i] = rand () % upperLimit + 1;
|
||||||
|
binTree = addToTree(binTree, &numbers[i], sizeof(unsigned int), compare, &isDuplicate);
|
||||||
|
} while (isDuplicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int duplicate = numbers[rand () % len];
|
||||||
|
int indexDuplicate = rand () % len;
|
||||||
|
numbers[len] = numbers[indexDuplicate];
|
||||||
|
numbers[indexDuplicate] = duplicate;
|
||||||
|
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Vergleichsfunktion von qsort
|
||||||
|
const int compare (const void *a, const void *b) {
|
||||||
|
unsigned int *x = a;
|
||||||
|
unsigned int *y = b;
|
||||||
|
if (*x < *y) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if (*x > *y) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sortiert Zahlen mit qsort, vergleicht dann benachbarte Elemente und gibt bei Erfolg die doppelte Zahl zurück
|
||||||
|
unsigned int getDuplicate(unsigned int numbers[], unsigned int len)
|
||||||
|
{
|
||||||
|
if (len < 2) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(numbers, len, sizeof(unsigned int), compare);
|
||||||
|
|
||||||
|
for (int i = 0; i < len-1; i++) {
|
||||||
|
if (numbers[i] == numbers [i+1]) {
|
||||||
|
return numbers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,6 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
|
||||||
#ifdef __linux__
|
#if __APPLE__
|
||||||
// Defines strict posix compliance for CLOCK_MONOTONIC
|
|
||||||
#define _POSIX_C_SOURCE 199309L
|
|
||||||
#include <time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __APPLE__ || __linux__
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
static struct timespec start = {0, 0};
|
static struct timespec start = {0, 0};
|
||||||
|
|
||||||
@@ -27,8 +21,7 @@ double stopTimer()
|
|||||||
|
|
||||||
double measuredSeconds = (double)delta_us / 1000000.;
|
double measuredSeconds = (double)delta_us / 1000000.;
|
||||||
|
|
||||||
if (start.tv_nsec > 0)
|
if(start.tv_nsec > 0) {
|
||||||
{
|
|
||||||
start.tv_nsec = 0;
|
start.tv_nsec = 0;
|
||||||
start.tv_sec = 0;
|
start.tv_sec = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user