Skip to content
Snippets Groups Projects
Commit 85f30356 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vorbereitung 16.1.2017

parent baf6f1d9
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#define STACK_SIZE 10
int stack[STACK_SIZE];
int stack_pointer = 0;
void push (int x)
{
stack[stack_pointer++] = x;
}
int pop (void)
{
return stack[--stack_pointer];
}
void show (void)
{
printf ("stack content:");
for (int i = 0; i < stack_pointer; i++)
printf (" %d", stack[i]);
if (stack_pointer)
printf ("\n");
else
printf (" (empty)\n");
}
void insert (int x, int pos)
{
for (int i = pos; i < stack_pointer; i++)
stack[i + 1] = stack[i];
stack[pos] = x;
stack_pointer++;
}
void insert_sorted (int x)
{
int i = 0;
while (i < stack_pointer && x < stack[i])
i++;
insert (x, i);
}
int main (void)
{
push (3);
push (7);
push (137);
show ();
insert (5, 1);
show ();
insert_sorted (42);
show ();
insert_sorted (2);
show ();
return 0;
}
File added
% hp-20170116.pdf - Lecture Slides on Low-Level Programming
% Copyright (C) 2012, 2013, 2015, 2016, 2017 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{tikz}
\usepackage{rotating}
\title{Angewandte Informatik\\Hardwarenahe Programmierung}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{16.\ Januar 2017}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\item[\textbf{2}] \textbf{Einführung in C}
\item[\textbf{3}] \textbf{Bibliotheken}
\item[\textbf{4}] \textbf{Algorithmen}
\item[\textbf{5}] \textbf{Hardwarenahe Programmierung}
\item[\textbf{6}] \textbf{Objektorientierte Programmierung}
\begin{itemize}
\vspace*{-0.1cm}
\item[\textbf{\dots}]
\color{medgreen}
\item[6.5] Virtuelle Methoden
\item[6.6] Einführung in C++
\end{itemize}
\item[\textbf{7}] \textbf{Datenstrukturen}
\begin{itemize}
\color{orange}
\item[7.1] Stack und FIFO
\vspace*{-0.1cm}
\item[\dots]
\end{itemize}
\end{itemize}
\vspace*{-1cm}
\end{frame}
\newcommand{\half}{\protect\raisebox{0.5ex}{\small 1}\kern-0.1em/\kern-0.02em\protect\raisebox{-0.1ex}{\small 2}}
\setcounter{section}{5}
\section{Objektorientierte Programmierung}
\setcounter{subsection}{4}
\subsection{Virtuelle Methoden}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
void print_object (t_object *this)
{
if (this->base.type == T_INTEGER)
printf ("Integer: %d\n", this->integer.content);
else if (this->base.type == T_STRING)
printf ("String: \"%s\"\n", this->string.content);
}
\end{lstlisting}
\begin{picture}(0,0)
\color{red}
\put(9,1.7){\shortstack[l]{if-Kette:\\\strut wird unübersichtlich}}
\put(1,-2){\mbox{\textarrow}}
\put(0,-3){\mbox{Zeiger auf Funktionen}}
\end{picture}
\begin{lstlisting}[xleftmargin=4cm]
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\showsubsection
Zeiger auf Funktionen
\medskip
\begin{lstlisting}
void (* print) (t_object *this);
\end{lstlisting}
\begin{picture}(0,1.2)(0,-0.9)
\color{red}
\put(0.95,0.3){\mbox{$\underbrace{\rule{1cm}{0pt}}$}}
\put(0.2,-0.7){\shortstack{das, worauf print zeigt,\\ist eine Funktion}}
\end{picture}
\begin{itemize}
\item
Objekt enthält Zeiger auf Funktion
\item
Konstruktor initialisiert diesen Zeiger
\item
Aufruf: "`automatisch"' die richtige Funktion
\medskip
\item
in größeren Projekten:\\
Objekt enthält Zeiger auf Tabelle von Funktionen
\end{itemize}
\end{frame}
\subsection{Einführung in C++}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{center}
\begin{minipage}{5cm}
\begin{lstlisting}[gobble=8]
typedef struct
{
void (* print) (union t_object *this);
} t_base;
\end{lstlisting}
\end{minipage}\\[0.5cm]
\begin{minipage}{5.5cm}
\begin{lstlisting}[gobble=8]
typedef struct
{
void (* print) (...);
int content;
} t_integer;
\end{lstlisting}
\end{minipage}
\begin{minipage}{5cm}
\begin{lstlisting}[gobble=8]
typedef struct
{
void (* print) (union t_object *this);
char *content;
} t_string;
\end{lstlisting}
\end{minipage}
\end{center}
\end{frame}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{center}
\begin{minipage}{5cm}
\begin{lstlisting}[gobble=8]
struct TBase
{
virtual void print (void);
};
\end{lstlisting}
\end{minipage}\\[0.5cm]
\begin{minipage}{5.5cm}
\begin{lstlisting}[gobble=8]
struct TInteger: public TBase
{
virtual void print (void);
int content;
};
\end{lstlisting}
\end{minipage}
\begin{minipage}{5cm}
\begin{lstlisting}[gobble=8]
struct TString: public TBase
{
virtual void print (void);
char *content;
};
\end{lstlisting}
\end{minipage}
\end{center}
\end{frame}
\section{Datenstrukturen}
\subsection{Stack und FIFO}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{minipage}{0.48\textwidth}
Im letzten Praktikumsversuch:
\begin{itemize}
\item
Array nur zum Teil benutzt
\item
Variable speichert genutzte Länge
\item
Elemente hinten anfügen\\
oder entfernen
\arrowitem
Stack
\end{itemize}
\bigskip
\begin{itemize}
\item
hinten anfügen/entfernen: $\mathcal{O}(1)$\hspace*{-1cm}
\item
vorne oder in der Mitte anfügen/entfernen: $\mathcal{O}(n)$
\end{itemize}
\end{minipage}\hfill
\begin{minipage}{0.51\textwidth}
Auch möglich:
\begin{itemize}
\item
Array nur zum Teil benutzt
\item
2 Variable speichern\\genutzte Länge (ringförmig)
\item
Elemente hinten anfügen\\
oder vorne entfernen
\arrowitem
FIFO
\end{itemize}
\bigskip
\begin{itemize}
\item
vorne oder hinten\\
anfügen oder entfernen: $\mathcal{O}(1)$
\item
in der Mitte anfügen/entfernen: $\mathcal{O}(n)$
\end{itemize}
\end{minipage}
\end{frame}
\begin{frame}[fragile]
\showsection
\showsubsection
\bigskip
\begin{minipage}[b]{6cm}
\begin{center}
"`First In -- First Out"'
\bigskip
\begin{picture}(6,4)
\thicklines
\color{structure}
\put(0.5,0){\line(1,0){5}}
\put(3.5,0){\only<1-5>{\line(0,1){1}}}
\put(4.5,0){\only<1-4>{\line(0,1){1}}}
\put(3.5,1){\only<1-4>{\line(1,0){1}}}
\put(4.0,0.5){\only<1-4>{\makebox(0,0){\lstinline{3}}}}
\put(3.0,1.5){\only<1>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(3.0,1.5){\only<1>{\makebox(0,0)[b]{\lstinline{push (3)}}}}
\put(2.5,0){\only<2-6>{\line(0,1){1}}}
\put(2.5,1){\only<2-5>{\line(1,0){1}}}
\put(3.0,0.5){\only<2-5>{\makebox(0,0){\lstinline{7}}}}
\put(2.0,1.5){\only<2>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,1.5){\only<2>{\makebox(0,0)[b]{\lstinline{push (7)}}}}
\put(1.5,0){\only<3-6>{\line(0,1){1}}}
\put(1.5,1){\only<3-6>{\line(1,0){1}}}
\put(2.0,0.5){\only<3-6>{\makebox(0,0){\lstinline{137}}}}
\put(1.0,1.5){\only<3>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(1.0,1.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(4.55,1.05){\only<4>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(5.00,1.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 3}}}
\put(3.55,1.05){\only<5>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,1.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(2.55,1.05){\only<6>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(3.00,1.60){\only<6>{\makebox(0,0)[b]{\lstinline{pop ()}: 137}}}
\end{picture}
\bigskip
FIFO = Queue = Reihe
\end{center}
\end{minipage}\hfill
\begin{minipage}[b]{6cm}
\begin{center}
"`Last In -- First Out"'
\bigskip
\begin{picture}(6,4)
\thicklines
\color{structure}
\put(1.5,0){\line(1,0){3}}
\put(2.5,0){\line(0,1){1}}
\put(3.5,0){\line(0,1){1}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,0.5){\makebox(0,0){\lstinline{3}}}
\put(2.0,1.5){\only<1>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,1.5){\only<1>{\makebox(0,0)[b]{\lstinline{push (3)}}}}
\put(2.5,1){\only<2-5>{\line(0,1){1}}}
\put(3.5,1){\only<2-5>{\line(0,1){1}}}
\put(2.5,2){\only<2-5>{\line(1,0){1}}}
\put(3.0,1.5){\only<2-5>{\makebox(0,0){\lstinline{7}}}}
\put(2.0,2.5){\only<2>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,2.5){\only<2>{\makebox(0,0)[b]{\lstinline{push (7)}}}}
\put(2.5,2){\only<3-4>{\line(0,1){1}}}
\put(3.5,2){\only<3-4>{\line(0,1){1}}}
\put(2.5,3){\only<3-4>{\line(1,0){1}}}
\put(3.0,2.5){\only<3-4>{\makebox(0,0){\lstinline{137}}}}
\put(2.0,3.5){\only<3>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,3.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(3.55,3.05){\only<4>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,3.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 137}}}
\put(3.55,2.05){\only<5>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,2.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(3.55,1.05){\only<6>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,1.60){\only<6>{\makebox(0,0)[b]{\lstinline{pop ()}: 3}}}
\end{picture}
\bigskip
LIFO = Stack = Stapel
\end{center}
\end{minipage}
%
% \dots
\end{frame}
\begin{frame}
\showsection
\showsubsection
\bigskip
\begin{minipage}[t]{6cm}
Array (Stack, FIFO):\\
in der Mitte einfügen
\begin{center}
\begin{picture}(6,3.8)
\thicklines
\color{structure}
\put(1.5,0){\line(1,0){3}}
\put(2.5,0){\line(0,1){3}}
\put(3.5,0){\line(0,1){3}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,0.5){\makebox(0,0){\lstinline{3}}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,1.5){\makebox(0,0){\lstinline{7}}}
\put(2.5,2){\line(1,0){1}}
\put(3.0,2.5){\makebox(0,0){\lstinline{137}}}
\put(2.5,3){\line(1,0){1}}
\put(1.5,1.5){\makebox(0,0)[b]{\lstinline{push (5)}}}
\put(1.5,1.45){\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.95,-0.45);}}}
\put(3.55,2.5){\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)..controls(0.5,0.45)..(0,0.9);}}}
\put(3.55,1.5){\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)..controls(0.5,0.45)..(0,0.9);}}}
\pause
\color{red}
\put(4.1,3.0){\makebox(0,0)[l]{\textbf{1.}}}
\put(4.1,2.0){\makebox(0,0)[l]{\textbf{2.}}}
\put(1.5,1.9){\makebox(0,0)[b]{\textbf{3.}}}
\pause
\put(6.0,1.5){\makebox(0,0)[tl]{$\mathcal{O}(n)$}}
\put(5.95,1.45){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-1.3,0.4);}}}
\put(5.95,1.49){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-1.3,1.1);}}}
\end{picture}
\end{center}
\end{minipage}\pause\hfill
\begin{minipage}[t]{5cm}
In Array (Stack, FIFO) \dots
\begin{itemize}
\item
einfügen: $\mathcal{O}(n)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:\\
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}
\end{frame}
\subsection{Verkettete Listen}
\begin{frame}
\showsection
\showsubsection
\begin{tikzpicture}
\color{structure}
\node(first) at (0,0.5) {first};
\node[shape=rectangle,draw,line width=1pt](3) at (1,2) {3};
\node[shape=rectangle,draw,line width=1pt](5) at (2,1) {5};
\node[shape=rectangle,draw,line width=1pt](7) at (3,2) {7};
\node[shape=rectangle,draw,line width=1pt](137) at (5,2) {137};
\node(NULL) at (7,2) {NULL};
\draw[-latex](first)--(3);
\only<1>{\draw[-latex](3)--(7);}
\only<2>{\draw[-latex](3) to[out=0] (5);}
\only<2>{\draw[-latex](5) to[in=180] (7);}
\draw[-latex](7)--(137);
\draw[-latex](137)--(NULL);
\end{tikzpicture}
\begin{itemize}
\item
Jeder Datensatz enthält einen Zeiger auf das nächste Element.
\item
Beim letzten Element zeigt der Zeiger auf \lstinline{NULL}.
\item
Eine Variable zeigt auf das erste Element.
\item
Wenn die Liste leer ist, zeigt die Variable auf \lstinline{NULL}.
\arrowitem
(einfach) \textbf{verkettete Liste}
\end{itemize}
\end{frame}
\begin{frame}
\showsection
\showsubsection
\begin{minipage}[t]{5cm}
In Array (Stack, FIFO) \dots
\begin{itemize}
\item
einfügen: $\mathcal{O}(n)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:\\
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}\hfill
\begin{minipage}[t]{6cm}
In (einfach) verkettete/r Liste \dots
\begin{itemize}
\item
einfügen: $\mathcal{O}(1)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt \hspace*{-1.7cm}\tikz{\draw[red](-1.7,0)--(0,0.2);\draw[red](-1.7,0.2)--(0,0);}
suchen: {\color{red}$\mathcal{O}(n)$}
\item
beim Einfügen sortieren:
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}
\pause
\medskip
\begin{center}
\begin{minipage}[t]{6cm}
In (ausbalancierten) Bäumen \dots
\begin{itemize}
\item
einfügen: $\mathcal{O}(\log n)$
\item
suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:
$\mathcal{O}(n \log n)$
\end{itemize}
\end{minipage}
\end{center}
\end{frame}
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\item[\textbf{2}] \textbf{Einführung in C}
\item[\textbf{3}] \textbf{Bibliotheken}
\item[\textbf{4}] \textbf{Algorithmen}
\item[\textbf{5}] \textbf{Hardwarenahe Programmierung}
\item[\textbf{6}] \textbf{Objektorientierte Programmierung}
\item[\textbf{7}] \textbf{Datenstrukturen}
\begin{itemize}
\color{medgreen}
\item[7.1] Stack und FIFO
\item[7.2] Verkettete Listen
\color{red}
\item[7.3] Bäume
\end{itemize}
\end{itemize}
\end{frame}
\end{document}
File added
% hp-uebung-20170116.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016, 2017 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{enumerate}
\usepackage{ifthen}
\usepackage{gensymb}
\newcounter{exercise}
\newcommand{\exercise}[1]{\addtocounter{exercise}{1}\subsection*{Aufgabe \arabic{exercise}: #1}}
\newcounter{points}
\newcommand{\points}[1]{\ifthenelse{#1=1}{(1 Punkt)}{(#1 Punkte)}\addtocounter{points}{#1}}
\newcommand{\ItwoC}{I\raisebox{0.5ex}{\footnotesize 2}C}
\newcommand{\ITWOC}{I\raisebox{0.5ex}{\normalsize 2}C}
\newcommand{\gitfile}[2]{\href{https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/#1/#2}{\file{#2}}}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 16.\ Januar 2017}
Diese Übung enthält Punkteangaben wie in einer Klausur.
Um zu "`bestehen"', müssen Sie innerhalb von 90 Minuten
unter Verwendung ausschließlich zugelassener Hilfsmittel
15 Punkte (von insgesamt \totalpoints) erreichen.
\exercise{Stack-Operationen}
Wir betrachten das folgende Programm (\gitfile{20170116}{aufgabe-1.c}):
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}[gobble=6]
#include <stdio.h>
#define STACK_SIZE 10
int stack[STACK_SIZE];
int stack_pointer = 0;
void push (int x)
{
stack[stack_pointer++] = x;
}
int pop (void)
{
return stack[--stack_pointer];
}
void show (void)
{
printf ("stack content:");
for (int i = 0; i < stack_pointer; i++)
printf (" %d", stack[i]);
if (stack_pointer)
printf ("\n");
else
printf (" (empty)\n");
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}[gobble=6]
void insert (int x, int pos)
{
for (int i = pos; i < stack_pointer; i++)
stack[i + 1] = stack[i];
stack[pos] = x;
stack_pointer++;
}
void insert_sorted (int x)
{
int i = 0;
while (i < stack_pointer && x < stack[i])
i++;
insert (x, i);
}
int main (void)
{
push (3);
push (7);
push (137);
show ();
insert (5, 1);
show ();
insert_sorted (42);
show ();
insert_sorted (2);
show ();
return 0;
}
\end{lstlisting}
\end{minipage}
\begin{enumerate}[\quad(a)]
\item
Ändern Sie das Programm so,
daß die Funktion \lstinline{insert()} ihren Parameter \lstinline{x}
an der Stelle \lstinline{pos} in den Stack einfügt,
und den sonstigen Inhalt des Stacks verschiebt, aber nicht zerstört.
\points{3}
\item
Ändern Sie das Programm so,
daß die Funktion \lstinline{insert_sorted()} ihren Parameter \lstinline{x}
an derjenigen Stelle einfügt, an die er von der Sortierung her gehört.
(Der Stack wird hierbei vor dem Funktionsaufruf als sortiert vorausgesetzt.)
\points{2}
\item
Schreiben Sie eine zusätzliche Funktion \lstinline{int search (int x)},
die die Position (Index) des Elements \lstinline{x}
innerhalb des Stack zurückgibt
-- oder \lstinline{-1}, wenn \lstinline{x} nicht im Stack enthalten ist.
Der Rechenaufwand darf höchstens $\mathcal{O}(n)$ betragen.
\points{3}
\item
Wie (c), aber der Rechenaufwand darf höchstens $\mathcal{O}(\log n)$ betragen.
\points{4}
\end{enumerate}
\goodbreak
\exercise{Iterativer Floodfill}
In Übungsaufgabe 3 vom 14.\,11.\,2016
(siehe \gitfile{20161114}{hp-uebung-20161114.pdf})
haben wir einen rekursiven Floodfill-Algorithmus implementiert.
\begin{enumerate}[\quad(a)]
\item
Ergänzen Sie die Funktion \lstinline{fill()} so,
daß Sie verfolgen können ("`Animation"'),
in welcher Reihenfolge die Punkte auf dem Bildschirm "`ausgemalt"' werden.
\points{4}
Hinweis: Eine Möglichkeit,
den Bildschirm zu löschen und eine kurze Zeit lang zu warten,
können Sie den Beispielaufgaben zum Sortieren
(z.\,B.\ \gitfile{20161114}{sort-2.c}) entnehmen.
(Um \lstinline{usleep()} nutzen zu können,
ist beim Compilieren mit \lstinline[style=cmd]{gcc}
die Option \lstinline[style=cmd]{-std=gnu99} erforderlich.)
\item
Unter Verwendung eines Stack ist es möglich,
den Floodfill-Algorithmus iterativ (also mit Schleife)
statt rekursiv zu implementieren.
Informieren Sie sich per Web-Suche über die Details
und implementieren Sie einen iterativen Floodfill-Algorithmus mit Stack
für die Text-Grafik-Bibliothek.
Die Punkte sollen dabei in derselben Reihenfolge "`ausgemalt"' werden
wie bisher in der rekursiven Floodfill-Implementation.
\points{4}
\item
Ersetzen Sie nun den Stack durch einen FIFO.
Der Floodfill-Algorithmus sollte weiterhin funktionieren,
nur daß die Punkte in einer anderen Reihenfolge "`ausgemalt"' werden.
Beschreiben Sie diesen Unterschied.
\points{3}
(Ein iterativer Floodfill mit FIFO ist nützlich für die Wegfindung
und verwandte Aufgabenstellungen.)
\end{enumerate}
\exercise{Doppelt verkettete Liste}
In der Vorlesung wurde ein Beispiel-Programm
\href{https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170116/hp-uebung-20170116.pdf}{(Link wird nachgereicht)}
zur Verwaltung einfach verketteter Listen erstellt
(an die Liste anhängen, in die Liste einfügen, Liste ausgeben usw.).
Schreiben Sie dieses Programm um für doppelt verkettete Listen.
\points{8}
\bigskip
\begin{flushright}
\textit{Viel Erfolg!}
\end{flushright}
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}}
\makeatother
\end{document}
#include <stdio.h>
#define STACK_SIZE 10
int stack[STACK_SIZE];
int stack_pointer = 0;
void push (int x)
{
stack[stack_pointer++] = x;
}
int pop (void)
{
return stack[--stack_pointer];
}
void show (void)
{
printf ("stack content:");
for (int i = 0; i < stack_pointer; i++)
printf (" %d", stack[i]);
if (stack_pointer)
printf ("\n");
else
printf (" (empty)\n");
}
void insert (int x, int pos)
{
for (int i = stack_pointer - 1; i >= pos; i--)
stack[i + 1] = stack[i];
stack[pos] = x;
stack_pointer++;
}
void insert_sorted (int x)
{
int i = stack_pointer - 1;
while (i >= 0 && x < stack[i])
{
stack[i + 1] = stack[i];
i--;
}
stack[i + 1] = x;
stack_pointer++;
}
int main (void)
{
push (3);
push (7);
push (137);
show ();
insert (5, 1);
show ();
insert_sorted (42);
show ();
insert_sorted (2);
show ();
return 0;
}
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
../common/pgscript.sty
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
#include <stdio.h>
#define STACK_SIZE 10
int stack[STACK_SIZE];
int stack_pointer = 0;
void push (int x)
{
stack[stack_pointer++] = x;
}
int pop (void)
{
return stack[--stack_pointer];
}
int main (void)
{
push (3);
push (7);
push (137);
printf ("%d\n", pop ());
printf ("%d\n", pop ());
printf ("%d\n", pop ());
return 0;
}
......@@ -5,7 +5,7 @@ Lehrveranstaltung im Wintersemester 2016/17
Hochschule Bochum, Campus Velbert/Heiligenhaus
Prof. Dr. rer. nat. Peter Gerwinski
Copyright © 2012–2016 Peter Gerwinski
Copyright © 2012–2017 Peter Gerwinski
**Diese Lehrmaterialien sind freie Software.**
Sie dürfen diese gemäß den jeweils angegebenen Lizenzen
......@@ -30,6 +30,7 @@ Vortragsfolien:
* [19.12.2016: Hardwarenahe Programmierung (Binärdarstellung von Zahlen), Objektorientierte Programmierung (Unions, GUI)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161219/hp-20161219.pdf)
* [02.01.2017: Special: Software und Urheberrecht, Exploits](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170102/hp-20170102.pdf)
* [09.01.2017: Objektorientierte Programmierung (virtuelle Methoden, C++), Datenstrukturen (Stack und FIFO)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170109/hp-20170109.pdf)
* [16.01.2017: Datenstrukturen (Stack und FIFO, verkettete Listen)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170116/hp-20170116.pdf)
* [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/hp-slides-2016ws.pdf)
Tafelbilder:
......@@ -61,6 +62,7 @@ Tafelbilder:
* [12.12.2016: Daten im Speicher, Zeigerarithmetik, XBM-Grafik](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161212/hp-uebung-20161212.pdf)
* [19.12.2016: Bürgerentscheid, Lokale Variable im Speicher, Blinkende LEDs, Objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161219/hp-uebung-20161219.pdf)
* [09.01.2017: Objektorientierte Programmierung mit dem C-Datentyp _union_, Objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170109/hp-uebung-20170109.pdf)
* [16.01.2017: Stack-Operationen, Iterativer Floodfill, Doppelt verkettete Listen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170116/hp-uebung-20170116.pdf)
Musterlösungen zu den Übungsaufgaben:
-------------------------------------
......@@ -102,7 +104,7 @@ Course in winter semester 2016–17
Bochum University of Applied Sciences, Campus Velbert/Heiligenhaus
Prof. Dr. rer. nat. Peter Gerwinski
Copyright © 2012–2016 Peter Gerwinski
Copyright © 2012–2017 Peter Gerwinski
**These teaching materials are Free Software.**
You may study, copy, modify, and/or distribute them
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment