From 487f103de40ff781117a89714f36df06ead63726 Mon Sep 17 00:00:00 2001 From: paulusja Date: Thu, 18 Jun 2026 09:03:11 +0200 Subject: [PATCH] Add exercises 09. --- 09/sortierung/makefile | 9 ++++ 09/sortierung/sortierung.c | 8 +++ 09/sortierung/sortierung.h | 9 ++++ 09/sortierung/sortierungTest.c | 57 ++++++++++++++++++++ 09/sortierung/sortierungsvergleich.c | 53 +++++++++++++++++++ 09/suche/makefile | 7 +++ 09/suche/suche.c | 14 +++++ 09/suche/suche.h | 6 +++ 09/suche/sucheTest.c | 78 ++++++++++++++++++++++++++++ 09/suche/zaehleVorkommen.c | 76 +++++++++++++++++++++++++++ 09/utils/stoppuhr.c | 14 +++++ 09/utils/stoppuhr.h | 7 +++ 09/utils/zahlenEingabe.c | 29 +++++++++++ 09/utils/zahlenEingabe.h | 8 +++ 09/utils/zufallsarray.c | 8 +++ 09/utils/zufallsarray.h | 6 +++ 16 files changed, 389 insertions(+) create mode 100644 09/sortierung/makefile create mode 100644 09/sortierung/sortierung.c create mode 100644 09/sortierung/sortierung.h create mode 100644 09/sortierung/sortierungTest.c create mode 100644 09/sortierung/sortierungsvergleich.c create mode 100644 09/suche/makefile create mode 100644 09/suche/suche.c create mode 100644 09/suche/suche.h create mode 100644 09/suche/sucheTest.c create mode 100644 09/suche/zaehleVorkommen.c create mode 100644 09/utils/stoppuhr.c create mode 100644 09/utils/stoppuhr.h create mode 100644 09/utils/zahlenEingabe.c create mode 100644 09/utils/zahlenEingabe.h create mode 100644 09/utils/zufallsarray.c create mode 100644 09/utils/zufallsarray.h diff --git a/09/sortierung/makefile b/09/sortierung/makefile new file mode 100644 index 0000000..13bef32 --- /dev/null +++ b/09/sortierung/makefile @@ -0,0 +1,9 @@ +SORTIERUNGLIBFILES = sortierung.c +SORTIERUNGTESTLIBFILES = $(SORTIERUNGLIBFILES) ../utils/zahlenEingabe.c +SORTIERUNGSVERGLEICHLIBFILES = $(SORTIERUNGLIBFILES) ../utils/stoppuhr.c ../utils/zufallsarray.c + +sortierungTest: + gcc -Wall -o sortierungTest sortierungTest.c $(SORTIERUNGTESTLIBFILES) + +sortierungsvergleich: + gcc -Wall -o sortierungsvergleich sortierungsvergleich.c $(SORTIERUNGSVERGLEICHLIBFILES) \ No newline at end of file diff --git a/09/sortierung/sortierung.c b/09/sortierung/sortierung.c new file mode 100644 index 0000000..dbd15c0 --- /dev/null +++ b/09/sortierung/sortierung.c @@ -0,0 +1,8 @@ +#include "sortierung.h" + +void tausche(int *zahl1, int *zahl2) +{ + int tmp = *zahl1; + *zahl1 = *zahl2; + *zahl2 = tmp; +} diff --git a/09/sortierung/sortierung.h b/09/sortierung/sortierung.h new file mode 100644 index 0000000..ba4600a --- /dev/null +++ b/09/sortierung/sortierung.h @@ -0,0 +1,9 @@ +#ifndef SORTIERUNG_H +#define SORTIERUNG_H + +void tausche(int *zahl1, int *zahl2); +void selectionsort(int array[], int anzahl); +void insertionsort(int array[], int anzahl); +void bubblesort(int array[], int anzahl); + +#endif \ No newline at end of file diff --git a/09/sortierung/sortierungTest.c b/09/sortierung/sortierungTest.c new file mode 100644 index 0000000..6ac0ce8 --- /dev/null +++ b/09/sortierung/sortierungTest.c @@ -0,0 +1,57 @@ +/***************************************************************** + * Verwenden Sie Ihre Lösung für Selection Sort, die Sie zur * + * Übung von eindimensionalen Arrays geschrieben haben. * + * Implementieren Sie nun die Sortierungsfunktion nur mit * + * Zeigern. Implementieren Sie Ihre Lösung in sortierung.h bzw. * + * sortierung.c * + * * + * Beispiel für Sortierung einer Liste 5 3 0 2: * + * Setze Minimum der Restliste an 1. Position: 0 3 5 2 * + * Setze Minimum der Restliste an 2. Position: 0 2 5 3 * + * Setze Minimum der Restliste an 3. Position: 0 2 3 5 * + * * + * Beispielhafte Ausgabe: * + * >> Wie viele Zahlen (max. 100)? fsdf * + * >> Fehler! Erneute Eingabe: -1 * + * >> Fehler! Erneute Eingabe: 101 * + * >> Fehler! Erneute Eingabe: 5 * + * >> Geben Sie die 1. Zahl ein: dsf * + * >> Fehler! Erneute Eingabe: 45 * + * >> Geben Sie die 2. Zahl ein: 1 * + * >> Geben Sie die 3. Zahl ein: 0 * + * >> Geben Sie die 4. Zahl ein: -4 * + * >> Geben Sie die 5. Zahl ein: 1423 * + * Sortierte Zahlen: -4 0 1 45 1423 * + * * + *****************************************************************/ + +#include +#include "../utils/zahlenEingabe.h" +#include "sortierung.h" + +#define MAX_ZAHLEN 100 + + +int main() +{ + int zahlen[MAX_ZAHLEN]; + int anzahl; + + printf("Wie viele Zahlen (max. %d)? ", MAX_ZAHLEN); + anzahl = gibMinMaxZahlEin(0, MAX_ZAHLEN); + + for(int i = 0; i < anzahl; i++) + { + printf("Geben Sie die %d. Zahl ein: ", i+1); + zahlen[i] = gibZahlEin(); + } + + selectionsort(zahlen, anzahl); + + printf("Sortierte Zahlen:\n"); + for(int i = 0; i < anzahl; i++) + printf("%3d ", zahlen[i]); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/09/sortierung/sortierungsvergleich.c b/09/sortierung/sortierungsvergleich.c new file mode 100644 index 0000000..1f7468c --- /dev/null +++ b/09/sortierung/sortierungsvergleich.c @@ -0,0 +1,53 @@ +/************************************************************************************************ + * Implementieren Sie die folgenden Sortieralgorithmen und vergleichen Sie deren Laufzeit mit + * untenstehendem Programm: + * 1. Nutzen Sie zunächst Ihre Lösung für SelectionSort. + * 2. Erweitern Sie nun sortierung.h bzw. sortierung.c um InsertionSort. + * 3. Erweitern Sie nun sortierung.h bzw. sortierung.c um BubbleSort. + * 4. Nutzen Sie abschließend die von der C-Standardbibliothek bereitgestellte qsort-Funktion. + * + * Hinweis: Sie können das Programm sortierungTest.c nutzen, um Ihre Implementierungen zu testen. + * + * Welche Laufzeit-Unterschiede ergeben sich? + *************************************************************************************************/ + +#include +#include +#include +#include "sortierung.h" +#include "../utils/zufallsarray.h" +#include "../utils/stoppuhr.h" + +#define ANZAHL_MAX 100000 +#define ANZAHL ANZAHL_MAX +#define ANZEIGE_MAX 100 + +void gibArrayAus(int array[], int anzahl) +{ + for(int i = 0; i < anzahl; i++) + { + printf(" %3d", array[i]); + } + printf("\n"); +} + +int main() +{ + int zufallszahlen[ANZAHL]; + srand(0); + + fuelleArray(zufallszahlen, ANZAHL, -ANZAHL, ANZAHL); + + if(ANZAHL <= ANZEIGE_MAX) + gibArrayAus(zufallszahlen, ANZAHL); + + starteUhr(); + selectionsort(zufallszahlen, ANZAHL); + + printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek()); + + if(ANZAHL <= ANZEIGE_MAX) + gibArrayAus(zufallszahlen, ANZAHL); + + return 0; +} diff --git a/09/suche/makefile b/09/suche/makefile new file mode 100644 index 0000000..0186f02 --- /dev/null +++ b/09/suche/makefile @@ -0,0 +1,7 @@ +LIBFILES = suche.c ../utils/zahlenEingabe.c ../utils/stoppuhr.c ../utils/zufallsarray.c + +sucheTest: + gcc -Wall -o sucheTest sucheTest.c $(LIBFILES) + +zaehleVorkommen: + gcc -Wall -o zaehleVorkommen zaehleVorkommen.c $(LIBFILES) \ No newline at end of file diff --git a/09/suche/suche.c b/09/suche/suche.c new file mode 100644 index 0000000..a6d1d89 --- /dev/null +++ b/09/suche/suche.c @@ -0,0 +1,14 @@ +// TODO: Beginn + +#include +#include "suche.h" + +int *binsucheZgr(int suchElem, int *startZgr, int *endZgr) +{ + // TODO +} + +int *binsuche(int suchElem, int array[], int anzahl) +{ + return binsucheZgr(suchElem, array, array + anzahl - 1); +} diff --git a/09/suche/suche.h b/09/suche/suche.h new file mode 100644 index 0000000..3a09283 --- /dev/null +++ b/09/suche/suche.h @@ -0,0 +1,6 @@ +#ifndef SUCHE_H +#define SUCHE_H + +int *binsuche(int suchElem, int array[], int anzahl); + +#endif \ No newline at end of file diff --git a/09/suche/sucheTest.c b/09/suche/sucheTest.c new file mode 100644 index 0000000..c881443 --- /dev/null +++ b/09/suche/sucheTest.c @@ -0,0 +1,78 @@ +/************************************************************************ + * Das untenstehende Programm ist ein Testlauf für eine Binärsuche. Bei + * einer Binärsuche geht man von (hier im Beispiel aufsteigend) + * sortierten Daten aus, die durchsucht werden sollen. Dabei wird für + * das in der Mitte befindliche Element geprüft, ob das zu suchende + * Element größer oder kleiner ist. Ist es kleiner, wird der Teil links des + * Mittelelements durchsucht, andernfalls der rechte Teil. Für den + * entsprechende Datenanteil beginnt die Suche wie eben beschrieben von + * vorne. Dies wiederholt man, bis man entweder das Element gefunden hat + * oder keine Daten mehr zu durchsuchen sind und das zu suchende Element + * folglich nicht enthalten ist. + * + * Vervollständigen Sie das Programm unter suche.h und suche.c, indem Sie + * die fehlenden Funktionen ergänzen. Implementieren Sie die Binärsuche + * einmal mit Array-Schreibweise und einmal mit Zeigerschreibweise. + * + ***************************************************************************/ + +#include +#include "suche.h" +#include "../utils/zahlenEingabe.h" + +#define MAX_ZAHLEN 100 + +void gibArrayEin(int array[], int anzahl); +void gibArrayAus(int array[], int anzahl); + +int main() +{ + int zahlen[MAX_ZAHLEN]; + int suchZahl, anzahl; + int *suchErg; + + printf("Wie viele Zahlen (max. %d)? ", MAX_ZAHLEN); + anzahl = gibMinMaxZahlEin(1, MAX_ZAHLEN); + + printf("Geben Sie %d Zahlen in aufsteigender Reihenfolge ein:\n", anzahl); + gibArrayEin(zahlen, anzahl); + + printf("\nDie Daten liegen folgendermassen im Speicher:\n"); + gibArrayAus(zahlen, anzahl); + + printf("Welche Zahl soll gesucht werden? "); + suchZahl = gibZahlEin(); + + suchErg = binsuche(suchZahl, zahlen, anzahl); + + if(suchErg == NULL) + printf("\nDie Zahl konnte nicht gefunden werden.\n"); + else + printf("\nDie Zahl %d befindet sich an Adresse %p, also an %d. Stelle im sortierten Array.\n", + *suchErg, suchErg, (int)(suchErg - zahlen + 1)); + + return 0; +} + +void gibArrayEin(int array[], int anzahl) +{ + printf("Geben Sie die 1. Zahl ein: "); + array[0] = gibZahlEin(); + + for(int i = 1; i < anzahl; i++) + { + printf("Geben Sie die %d. Zahl ein (min. %d): ", i+1, array[i-1]); + array[i] = gibMinZahlEin(array[i-1]); + } +} + +void gibArrayAus(int array[], int anzahl) +{ + printf(" Adresse | Wert\n"); + printf("-----------------------------------\n"); + for(int i = 0; i < anzahl; i++) + { + printf("%p | %16d\n", array+i, array[i]); + } + printf("\n"); +} diff --git a/09/suche/zaehleVorkommen.c b/09/suche/zaehleVorkommen.c new file mode 100644 index 0000000..d58ec54 --- /dev/null +++ b/09/suche/zaehleVorkommen.c @@ -0,0 +1,76 @@ +/************************************************************** + * Das untenstehende Programm soll ein Array mit Zufallswerten + * belegen, das Array aufsteigend sortieren und anschließend + * ermitteln, wie häufig ein einzugebender Wert im Array vorkommt. + * Die Häufigkeit soll einmal naiv ermittelt werden und einmal + * durch Verwendung Ihrer Implementierung für die binäre Suche. + * Das Programm vergleicht anschließend die benötigten Laufzeiten + * für beide Implementierungen. + * + * 1. Implementieren Sie zunächst die Sortierung des Zufallsarrays. + * (Die Funktionsaufrufe zur Ermittlung der Vorkommenszahlen müssen + * dafür auskommentiert werden.) + * 2. Implementieren Sie nun die naive Ermittlung der Anzahl an + * Vorkommen (ohne Binärsuche). + * 3. Implementieren Sie nun die Ermittlung der Anzahl an Vorkommen + * unter Verwendung Ihrer Implementierung der Binärsuche. + * + ***************************************************************/ +#include +#include +#include "../utils/zufallsarray.h" +#include "../utils/stoppuhr.h" +#include "../utils/zahlenEingabe.h" +#include "suche.h" + +#define MAX_ANZAHL 500000 +#define ANZAHL MAX_ANZAHL +#define MAX ((int)(ANZAHL * 0.2)) +#define MIN (-MAX) +#define MAX_100(n) ((n) < 100 ? n : 100) + +int zaehleVorkommenNaiv(int suchElem, int array[], int anzahl); +int zaehleVorkommenBs(int suchElem, int array[], int anzahl); +int vergleiche(const void *arg1, const void *arg2); +void gibArrayAus(int array[], int anzahl); + +int main() +{ + int suchZahl, anzahlVorkommen; + int zufallszahlen[ANZAHL]; + + srand(0); + + fuelleArray(zufallszahlen, ANZAHL, MIN, MAX); + // TODO: Sortierung mit qsort + + gibArrayAus(zufallszahlen, MAX_100(ANZAHL)); + + printf("Welche Zahl soll gesucht werden? "); + suchZahl = gibZahlEin(); + + printf("Zaehle naiv:\n"); + starteUhr(); + anzahlVorkommen = zaehleVorkommenNaiv(suchZahl, zufallszahlen, ANZAHL); + + printf("Die Zahl %d ist %dx enthalten. (%lf Sekunden)\n", suchZahl, anzahlVorkommen, messeZeitInSek()); + + printf("Zaehle mit Binaersuche:\n"); + starteUhr(); + anzahlVorkommen = zaehleVorkommenBs(suchZahl, zufallszahlen, ANZAHL); + + printf("Die Zahl %d ist %dx enthalten. (%lf Sekunden)\n", suchZahl, anzahlVorkommen, messeZeitInSek()); + + return 0; +} + +void gibArrayAus(int array[], int anzahl) +{ + printf("Die ersten %d Zufallszahlen: ", anzahl); + for(int i = 0; i < anzahl; i++) + { + printf(" %4d", array[i]); + } + printf("\n"); +} + diff --git a/09/utils/stoppuhr.c b/09/utils/stoppuhr.c new file mode 100644 index 0000000..826af31 --- /dev/null +++ b/09/utils/stoppuhr.c @@ -0,0 +1,14 @@ +#include +#include "stoppuhr.h" + +static clock_t zeitStempel = 0; + +void starteUhr() +{ + zeitStempel = clock(); +} + +double messeZeitInSek() +{ + return (double)(clock() - zeitStempel) / CLOCKS_PER_SEC; +} \ No newline at end of file diff --git a/09/utils/stoppuhr.h b/09/utils/stoppuhr.h new file mode 100644 index 0000000..9def335 --- /dev/null +++ b/09/utils/stoppuhr.h @@ -0,0 +1,7 @@ +#ifndef STOPPUHR_H +#define STOPPUHR_H + +void starteUhr(); +double messeZeitInSek(); + +#endif \ No newline at end of file diff --git a/09/utils/zahlenEingabe.c b/09/utils/zahlenEingabe.c new file mode 100644 index 0000000..f1391cf --- /dev/null +++ b/09/utils/zahlenEingabe.c @@ -0,0 +1,29 @@ +#include +#include "zahlenEingabe.h" + +int gibZahlEin() +{ + return gibMinMaxZahlEin(0x80000000, 0x7fffffff); +} + +int gibMinZahlEin(int min) +{ + return gibMinMaxZahlEin(min, 0x7fffffff); +} + +int gibMinMaxZahlEin(int min, int max) +{ + int zahl, geleseneWerte; + + do + { + geleseneWerte = scanf("%d", &zahl); + while(getchar() != '\n') {} + + if(geleseneWerte != 1 || zahl < min || zahl > max) + printf("Fehler! Erneute Eingabe: "); + + } while(geleseneWerte != 1 || zahl < min || zahl > max); + + return zahl; +} diff --git a/09/utils/zahlenEingabe.h b/09/utils/zahlenEingabe.h new file mode 100644 index 0000000..c4177d0 --- /dev/null +++ b/09/utils/zahlenEingabe.h @@ -0,0 +1,8 @@ +#ifndef ZAHLEN_EINGABE_H +#define ZAHLEN_EINGABE_H + +int gibZahlEin(); +int gibMinZahlEin(int min); +int gibMinMaxZahlEin(int min, int max); + +#endif diff --git a/09/utils/zufallsarray.c b/09/utils/zufallsarray.c new file mode 100644 index 0000000..da16e01 --- /dev/null +++ b/09/utils/zufallsarray.c @@ -0,0 +1,8 @@ +#include +#include "zufallsarray.h" + +void fuelleArray(int array[], int anzahl, int min, int max) +{ + for(int i = 0; i < anzahl; i++) + array[i] = rand() % (max - min + 1) + min; +} \ No newline at end of file diff --git a/09/utils/zufallsarray.h b/09/utils/zufallsarray.h new file mode 100644 index 0000000..3e71819 --- /dev/null +++ b/09/utils/zufallsarray.h @@ -0,0 +1,6 @@ +#ifndef ZUFALLSARRAY_H +#define ZUFALLSARRAY_H + +void fuelleArray(int array[], int anzahl, int min, int max); + +#endif \ No newline at end of file