forked from paulusja/info1_uebungen
Add exercises 09.
This commit is contained in:
parent
21b9b70871
commit
487f103de4
9
09/sortierung/makefile
Normal file
9
09/sortierung/makefile
Normal file
@ -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)
|
||||
8
09/sortierung/sortierung.c
Normal file
8
09/sortierung/sortierung.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "sortierung.h"
|
||||
|
||||
void tausche(int *zahl1, int *zahl2)
|
||||
{
|
||||
int tmp = *zahl1;
|
||||
*zahl1 = *zahl2;
|
||||
*zahl2 = tmp;
|
||||
}
|
||||
9
09/sortierung/sortierung.h
Normal file
9
09/sortierung/sortierung.h
Normal file
@ -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
|
||||
57
09/sortierung/sortierungTest.c
Normal file
57
09/sortierung/sortierungTest.c
Normal file
@ -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 <stdio.h>
|
||||
#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;
|
||||
}
|
||||
53
09/sortierung/sortierungsvergleich.c
Normal file
53
09/sortierung/sortierungsvergleich.c
Normal file
@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#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;
|
||||
}
|
||||
7
09/suche/makefile
Normal file
7
09/suche/makefile
Normal file
@ -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)
|
||||
14
09/suche/suche.c
Normal file
14
09/suche/suche.c
Normal file
@ -0,0 +1,14 @@
|
||||
// TODO: Beginn
|
||||
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
||||
6
09/suche/suche.h
Normal file
6
09/suche/suche.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef SUCHE_H
|
||||
#define SUCHE_H
|
||||
|
||||
int *binsuche(int suchElem, int array[], int anzahl);
|
||||
|
||||
#endif
|
||||
78
09/suche/sucheTest.c
Normal file
78
09/suche/sucheTest.c
Normal file
@ -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 <stdio.h>
|
||||
#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");
|
||||
}
|
||||
76
09/suche/zaehleVorkommen.c
Normal file
76
09/suche/zaehleVorkommen.c
Normal file
@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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");
|
||||
}
|
||||
|
||||
14
09/utils/stoppuhr.c
Normal file
14
09/utils/stoppuhr.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <time.h>
|
||||
#include "stoppuhr.h"
|
||||
|
||||
static clock_t zeitStempel = 0;
|
||||
|
||||
void starteUhr()
|
||||
{
|
||||
zeitStempel = clock();
|
||||
}
|
||||
|
||||
double messeZeitInSek()
|
||||
{
|
||||
return (double)(clock() - zeitStempel) / CLOCKS_PER_SEC;
|
||||
}
|
||||
7
09/utils/stoppuhr.h
Normal file
7
09/utils/stoppuhr.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef STOPPUHR_H
|
||||
#define STOPPUHR_H
|
||||
|
||||
void starteUhr();
|
||||
double messeZeitInSek();
|
||||
|
||||
#endif
|
||||
29
09/utils/zahlenEingabe.c
Normal file
29
09/utils/zahlenEingabe.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
8
09/utils/zahlenEingabe.h
Normal file
8
09/utils/zahlenEingabe.h
Normal file
@ -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
|
||||
8
09/utils/zufallsarray.c
Normal file
8
09/utils/zufallsarray.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
6
09/utils/zufallsarray.h
Normal file
6
09/utils/zufallsarray.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef ZUFALLSARRAY_H
|
||||
#define ZUFALLSARRAY_H
|
||||
|
||||
void fuelleArray(int array[], int anzahl, int min, int max);
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user