Template
Compare commits
2
Commits
main
..
a816abfe04
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a816abfe04 | ||
|
|
74b541a1a0 |
-62
@@ -1,62 +0,0 @@
|
|||||||
# ---> C
|
|
||||||
# Prerequisites
|
|
||||||
*.d
|
|
||||||
|
|
||||||
# Object files
|
|
||||||
*.o
|
|
||||||
*.ko
|
|
||||||
*.obj
|
|
||||||
*.elf
|
|
||||||
|
|
||||||
# Linker output
|
|
||||||
*.ilk
|
|
||||||
*.map
|
|
||||||
*.exp
|
|
||||||
|
|
||||||
# Precompiled Headers
|
|
||||||
*.gch
|
|
||||||
*.pch
|
|
||||||
|
|
||||||
# Libraries
|
|
||||||
*.lib
|
|
||||||
*.la
|
|
||||||
*.lo
|
|
||||||
|
|
||||||
# Shared objects (inc. Windows DLLs)
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.so.*
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Executables
|
|
||||||
*.exe
|
|
||||||
*.out
|
|
||||||
*.app
|
|
||||||
*.i*86
|
|
||||||
*.x86_64
|
|
||||||
*.hex
|
|
||||||
Startcode/doble_initial
|
|
||||||
Startcode/doble
|
|
||||||
Startcode/unitTests
|
|
||||||
|
|
||||||
# Debug files
|
|
||||||
*.dSYM/
|
|
||||||
*.su
|
|
||||||
*.idb
|
|
||||||
*.pdb
|
|
||||||
|
|
||||||
# Kernel Module Compile Results
|
|
||||||
*.mod*
|
|
||||||
*.cmd
|
|
||||||
.tmp_versions/
|
|
||||||
modules.order
|
|
||||||
Module.symvers
|
|
||||||
Mkfile.old
|
|
||||||
dkms.conf
|
|
||||||
|
|
||||||
# IDE folders
|
|
||||||
.vscode/
|
|
||||||
.idea/
|
|
||||||
|
|
||||||
# macOS
|
|
||||||
.DS_Store
|
|
||||||
Binary file not shown.
@@ -1,26 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "numbers.h"
|
|
||||||
#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.
|
|
||||||
* 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)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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 $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
#include "bintree.h"
|
||||||
|
|
||||||
|
//Speicher für Array erstellen, zufällige Zahlen von 1-2xlen erzeugen, mittels Binärbaum checken, ob Zahlen einzigartig sind
|
||||||
|
//Eine Zahl duplizieren, an zufälliger Stelle einfügen und die Zahl an der Stelle ans Ende schieben
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user