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

Vorbereitung 10.12.2015

parent 04140f19
No related branches found
No related tags found
No related merge requests found
File added
% ainf-20151203.pdf - Lecture Slides on Applied Computer Sciences
% Copyright (C) 2012, 2013, 2015 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{pdftricks}
\usepackage{rotating}
\newrgbcolor{orange}{0.7 0.2 0.0}
\begin{psinputs}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\renewcommand*\familydefault{\sfdefault}
\usepackage{pstricks,pst-grad,pst-node,pst-plot}
\end{psinputs}
\lstdefinestyle{asm}{basicstyle=\color{structure},
language={},
gobble=4}
\title{Angewandte Informatik}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{3.\ Dezember 2015}
\begin{document}
\maketitleframe
\sectionnonumber{\inserttitle}
\begin{frame}
\showsectionnonumber
\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}
\begin{itemize}
\item[4.1] Differentialgleichungen
\item[4.2] Rekursion
\color{medgreen}
\item[4.3] Stack und FIFO
\color{red}
\item[4.4] Aufwandsabschätzungen
\end{itemize}
\item[\textbf{5}] \textbf{Hardwarenahe Programmierung}
\begin{itemize}
\color{medgreen}
\item[5.1] Bit-Operationen
\item[5.2] I/O-Ports
\color{red}
\item[5.3] Interrupts
\item[5.4] volatile-Variable
\color{black}
\item[5.5] Software-Interrupts
\item[\dots]
\end{itemize}
\color{gray}
\item[\makebox(0,0){\textbf{\raisebox{0.5ex}{\dots}}}]
% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke}
\end{itemize}
\end{frame}
\section{Einführung}
\section{Einführung in C}
\section{Bibliotheken}
\section{Algorithmen}
\subsection{Differentialgleichungen}
\subsection{Rekursion}
\subsection{Stack und FIFO}
\begin{frame}[fragile]
\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>{\vector(1,-1){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>{\vector(1,-1){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>{\vector(1,-1){0.45}}}
\put(1.0,1.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(4.55,1.05){\only<4>{\vector(1,1){0.45}}}
\put(5.00,1.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 3}}}
\put(3.55,1.05){\only<5>{\vector(1,1){0.45}}}
\put(4.00,1.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(2.55,1.05){\only<6>{\vector(1,1){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>{\vector(1,-1){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>{\vector(1,-1){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>{\vector(1,-1){0.45}}}
\put(2.0,3.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(3.55,3.05){\only<4>{\vector(1,1){0.45}}}
\put(4.00,3.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 137}}}
\put(3.55,2.05){\only<5>{\vector(1,1){0.45}}}
\put(4.00,2.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(3.55,1.05){\only<6>{\vector(1,1){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}
\section{Hardwarenahe Programmierung}
\subsection{Bit-Operationen}
\subsubsection{Zahlensysteme}
\begin{frame}[fragile]
\showsection
\vspace*{-\smallskipamount}
\showsubsection
\vspace*{-\medskipamount}
\showsubsubsection
Oktal- und Hexadezimal-Zahlen lassen sich ziffernweise\\
in Binär-Zahlen umrechnen:
\begin{verbatim}
000 0 0000 0 1000 8
001 1 0001 1 1001 9
010 2 0010 2 1010 A
011 3 0011 3 1011 B
100 4 0100 4 1100 C
101 5 0101 5 1101 D
110 6 0110 6 1110 E
111 7 0111 7 1111 F
\end{verbatim}
\begin{picture}(0,0)
\put(8,3){\begin{rotate}{15}
\color{red}\bf
Auswendig lernen!
\end{rotate}}
\end{picture}
\end{frame}
\subsubsection{Bit-Operationen in C}
\begin{frame}[fragile]
\showsubsubsection
\begin{tabular}{lll}
C-Operator & Verknüpfung & Anwendung \\[\smallskipamount]
\lstinline,&, & Und & Bits gezielt löschen \\
\lstinline,|, & Oder & Bits gezielt setzen \\
\lstinline,^, & Exklusiv-Oder & Bits gezielt invertieren \\
\lstinline,~, & Nicht & Alle Bits invertieren \\[\smallskipamount]
\lstinline,<<, & Verschiebung nach links & Maske generieren \\
\lstinline,>>, & Verschiebung nach rechts & Bits isolieren
\end{tabular}
\bigskip
Beispiele:
\begin{itemize}
\item
Bit Nr.\ 5 gezielt auf 1 setzen:
\lstinline{PORTB |= 1 << 5;}
\item
Bit Nr.\ 6 gezielt auf 0 setzen:
\lstinline{PORTA &= ~(1 << 6);}
\item
Ist Bit Nr.\ 4 gesetzt?
\lstinline{if (PINC & (1 << 4) ...}
\smallskip
\item
Umschalten zwischen Ein- und Ausgabe: \lstinline{DDR}\\
Bit = 1: Ausgabe; Bit = 0: Eingabe
\end{itemize}
\begin{picture}(0,0)
\put(8.4,1){\begin{rotate}{15}
\color{red}\bf
\shortstack{Details abhängig von\\Prozessor und Compiler!}
\end{rotate}}
\end{picture}
\end{frame}
\subsection{I/O-Ports}
\begin{frame}[fragile]
% \showsection
\showsubsection
\vspace*{-1.5\medskipamount}
{\large\textbf{\color{structure}5.3\quad Interrupts}}
\bigskip
Kommunikation mit externen Geräten
\bigskip
\begin{center}
\begin{pdfpic}
\psset{unit=1cm}%
\begin{pspicture}(0,0)(10,6)
\rput(0,0){\psframe[framearc=0.25](2,5)}
\rput(1,4.5){\makebox(0,0)[t]{Prozessor}}
\rput(2.0,3.7){\pnode{p0}}
\rput(2.0,3.3){\pnode{q0}}
\rput(2.0,1.0){\pnode{i0}}
\rput(2.12,1.2){\pnode{j0}}
\rput(2.02,1.3){\pnode{k0}}
\rput(2.12,1.4){\pnode{l0}}
\rput(3.5,1.4){\pnode{m0}}
\rput(8,0){\psframe[framearc=0.25](2,5)}
\rput(9,4.5){\makebox(0,0)[t]{\shortstack{externes\\Gerät}}}
\rput(8.0,3.7){\pnode{p1}}
\rput(7.88,3.3){\pnode{q1}}
\rput(7.98,3.2){\pnode{r1}}
\rput(7.88,3.1){\pnode{s1}}
\rput(6.5,3.1){\pnode{t1}}
\rput(8.0,1.0){\pnode{i1}}
\rput(8.0,1.2){\pnode{j1}}
\ncline{->}{p0}{p1}
\ncline{q0}{q1}
\nccurve[angleB=90]{q1}{r1}
\nccurve[angleA=-90]{r1}{s1}
\ncline{->}{s1}{t1}
\rput(2.2,3.8){\makebox(0,0)[lb]{Prozessor schreibt in Output-Port}}
\rput(2.2,3.1){\makebox(0,0)[lt]{Prozessor liest Input-Port}}
\ncline{->}{i1}{i0}
\rput(7.8,1.1){\makebox(0,0)[rb]{externes Gerät ruft Interrupt auf}}
\end{pspicture}
\end{pdfpic}
\end{center}
\end{frame}
\begin{frame}[fragile]
\showsubsection
In Output-Port schreiben = Leitungen ansteuern
\medskip
Datei: \file{RP6Base/RP6Base\_Examples/RP6Examples\_20080915/\\
RP6Lib/RP6base/RP6RobotBaseLib.c}
Suchbegriff: \lstinline{setMotorDir}
\medskip
\begin{lstlisting}
void setMotorDir(uint8_t left_dir, uint8_t right_dir)
{
/* ... */
if(left_dir)
PORTC |= DIR_L;
else
PORTC &= ~DIR_L;
if(right_dir)
PORTC |= DIR_R;
else
PORTC &= ~DIR_R;
}
\end{lstlisting}
\begin{picture}(0,0)
\color{red}
\put(2.2,0.50){\vector(-2,1){0.5}}
\put(2.25,0.40){\makebox(0,0)[l]{Output-Port}}
\put(4.7,3.0){\vector(-2,1){0.75}}
\put(4.7,3.0){\vector(-2,-1){0.75}}
\put(4.8,3.0){\makebox(0,0)[l]{Manipulation einzelner Bits}}
\end{picture}
\vspace*{-1.5cm}
\strut\hfill\textarrow\ Steuerung der Motordrehrichtung
\end{frame}
\subsection{Interrupts}
\begin{frame}[fragile]
\showsubsection
Externes Gerät ruft (per Stromsignal) Unterprogramm auf
Zeiger hinterlegen: "`Interrupt-Vektor"'
\medskip
Datei: \file{RP6Base/RP6Base\_Examples/RP6Examples\_20080915/\\
RP6Lib/RP6base/RP6RobotBaseLib.c}
Suchbegriff: \lstinline{ISR}
\vspace{2.0cm}
\begin{lstlisting}
ISR (INT0_vect)
{
mleft_dist++;
mleft_counter++;
/* ... */
}
\end{lstlisting}
\begin{picture}(0,0)
\color{red}
\put(1.9,4.3){\vector(-1,-1){1.4}}
\put(2.0,4.4){\makebox(0,0)[l]{"`Dies ist ein Interrupt-Handler."'}}
\put(2.3,3.6){\vector(-1,-1){0.7}}
\put(2.4,3.6){\makebox(0,0)[l]{Interrupt-Vektor 0 darauf zeigen lassen}}
\put(3.7,2.9){\makebox(0,0)[l]{Schreibweise herstellerspezifisch!}}
\end{picture}
\vspace*{-1.5cm}
\strut\hfill Aufruf durch Sensor an Encoder-Scheibe\\
\strut\hfill\textarrow\ Entfernungsmessung
\end{frame}
\subsection{volatile-Variable}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
volatile uint16_t mleft_counter;
/* ... */
volatile uint16_t mleft_dist;
/* ... */
ISR (INT0_vect)
{
mleft_dist++;
mleft_counter++;
/* ... */
}
\end{lstlisting}
\begin{picture}(0,0)
\color{red}
\put(1.6,5.2){\vector(-1,-1){0.55}}
\put(1.6,5.2){\vector(-1,1){0.55}}
\put(1.6,5.2){\makebox(0,0)[l]{"`Immer lesen und schreiben. Nicht wegoptimieren."'}}
\end{picture}
\vspace{-3cm}
\begin{lstlisting}
int main (void)
{
int prev_mleft_dist = mleft_dist;
while (mleft_dist == prev_mleft_dist)
/* just wait */;
}
\end{lstlisting}
\end{frame}
\iffalse
\subsection{Software-Interrupts}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}[style=asm]
mov ax, 0012
int 10
\end{lstlisting}
\pause
\begin{picture}(0,0)
\color{red}
\put(2.8,0.95){\vector(-1,0){0.55}}
\put(2.9,0.95){\makebox(0,0)[l]{Parameter in Prozessorregister}}
\put(1.7,0.50){\vector(-1,0){0.55}}
\put(1.8,0.50){\makebox(0,0)[l]{Funktionsaufruf über Interrupt-Vektor}}
\end{picture}
\pause
Beispiel: VGA-Grafikkarte
\begin{itemize}
\item Modus setzen: \lstinline{mov ah, 00}
\item Grafikmodus: \lstinline{mov al, 12}
\item Textmodus: \lstinline{mov al, 03}
\end{itemize}
\pause
\bigskip
Verschiedene Farben: Output-Ports
\begin{itemize}
\item \newterm{Graphics Register\/}: Index \lstinline{03CE}, Daten \lstinline{03CF}
\item Index 0: \newterm{Set/Reset Register}
\item Index 1: \newterm{Enable Set/Reset Register}
\item Index 8: \newterm{Bit Mask Register}
\item Jedes Bit steht für Schreibzugriff auf eine Speicherbank.
\item 4 Speicherbänke \textarrow\ 16 Farben
\end{itemize}
\end{frame}
\fi
\subsection{Aufwandsabschätzungen}
\begin{frame}[fragile]
\showsubsection
Beispiel: Sortieralgorithmen
\begin{itemize}
\item
Maximum suchen\only<2->{: $\mathcal{O}(n)$}
\hfill
\begin{minipage}[t]{5.6cm}
\vspace*{-1.3cm}
\begin{pdfpic}
\psset{unit=0.5pt}
\begin{pspicture}(-20,-20)(250,200)
\psline[arrows=->](-10,0)(200,0)
\psline[arrows=->](0,-10)(0,200)
\psplot[plotpoints=200]{1}{125}{2 x 0.06 mul exp}
\put(70,190){\mbox{$g(n) \sim 2^n$}}
\psplot[plotpoints=200]{0}{190}{x x mul 0.005 mul}
\put(190,190){\mbox{$g(n) \sim n^2$}}
\psplot[plotpoints=200]{1}{190}{x ln x mul 0.1 mul}
\put(195,100){\mbox{$g(n) \sim n \log n$}}
\psplot[plotpoints=200]{0}{190}{x 0.4 mul}
\put(195,75){\mbox{$g(n) \sim n$}}
\psplot[plotpoints=200]{1}{190}{x ln 10 mul}
\put(195,50){\mbox{$g(n) \sim \log n$}}
\put(205,0){\makebox(0,0)[l]{$n$}}
\put(-10,210){\makebox(0,0)[l]{$g(n)$}}
\psplot[plotpoints=200]{1}{190}{30}
\put(195,25){\mbox{$g(n) \sim 1$}}
\end{pspicture}
\end{pdfpic}
\small
\begin{description}\itemsep0pt
\item[$n$:] Eingabedaten
\item[$g(n)$:] Rechenzeit
\end{description}
\vspace*{-10cm}\strut
\end{minipage}
\pause[3]
\item
Maximum ans Ende tauschen\\
\textarrow\ Selectionsort\pause: $\mathcal{O}(n^2)$
\pause
\item
Während Maximumsuche prüfen,\\abbrechen, falls schon sortiert\\
\textarrow\ Bubblesort\pause: $\mathcal{O}(n)$ bis $\mathcal{O}(n^2)$
\pause
\item
Rekursiv sortieren\\
\textarrow\ Quicksort\pause: $\mathcal{O}(n\log n)$ bis $\mathcal{O}(n^2)$\hfill
\end{itemize}
\end{frame}
\sectionnonumber{\inserttitle}
\begin{frame}
\showsectionnonumber
\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}
\begin{itemize}
\item[4.1] Differentialgleichungen
\item[4.2] Rekursion
\item[4.3] Stack und FIFO
\color{medgreen}
\item[4.4] Aufwandsabschätzungen
\end{itemize}
\item[\textbf{5}] \textbf{Hardwarenahe Programmierung}
\begin{itemize}
\color{medgreen}
\item[5.1] Bit-Operationen
\item[5.2] I/O-Ports
\item[5.3] Interrupts
\item[5.4] volatile-Variable
\color{black}
\item[5.5] Software-Interrupts
\item[\dots]
\end{itemize}
\color{gray}
\item[\makebox(0,0){\textbf{\raisebox{0.5ex}{\dots}}}]
% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke}
\end{itemize}
\end{frame}
\end{document}
File added
% ainf-uebung-20151210.pdf - Exercises on Applied Computer Sciences
% Copyright (C) 2013, 2015 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{pdftricks}
\usepackage{sfmath}
\begin{psinputs}
\usepackage{pgscript}
% \definecolor{PracticallyWhite}{rgb}{0.99 0.99 0.99}
\definecolor{verylightgray}{rgb}{0.95 0.95 0.95}
\end{psinputs}
\newcounter{exercise}
\newcommand{\exercise}[1]{\addtocounter{exercise}{1}\subsection*{Aufgabe \arabic{exercise}: #1}}
\newcounter{points}
\newcommand{\onepoint}{(1 Punkt)\addtocounter{points}{1}}
\newcommand{\points}[1]{(#1 Punkte)\addtocounter{points}{#1}}
\begin{document}
\thispagestyle{empty}
\section*{Angewandte Informatik\\Übungsaufgaben -- 10.\ Dezember 2015}
\exercise{Bit-Array}
Schreiben Sie C-Funktionen, die ein "`Array von Bits"' realisieren, z.\,B.:
\begin{tabular}[t]{ll}
\lstinline|void set_bit (int i);| & Bei Index $i$ auf 1 setzen \\
\lstinline|void clear_bit (int i);| & Bei Index $i$ auf 0 setzen \\
\lstinline|int get_bit (int i);| & Bei Index $i$ lesen
\end{tabular}
Die Funktionen sollen "`robust"' sein,
d.\,h.\ sie sollen im Fehlerfall eine sinnvolle Fehlermeldung ausgeben,
anstatt einen Absturz zu verursachen.
\points{8}
\medskip
Hinweise:
\vspace*{-\medskipamount}
\begin{itemize}\itemsep0pt
\item
Die Größe des Bit-"`Arrays"' (z.\,B.\ 1000) dürfen Sie als \emph{vorher bekannt\/} voraussetzen.
\item
Sie benötigen ein Array, z.\,B.\ von \lstinline|char|- oder \lstinline|int|-Variablen.
\item
Sie benötigen eine Division (\lstinline|/|) sowie den Divisionsrest (Modulo: \lstinline|%|).
\end{itemize}
\exercise{Arrays mit Zahlen}
\begin{minipage}[t]{0.5\textwidth}
Wir betrachten das folgende Programm:
\begin{lstlisting}[gobble=6]
#include <stdio.h>
void f (int *s0, int *s1)
{
while (*s0 >= 0)
{
int *s = s1;
while (*s >= 0)
if (*s0 == *s++)
printf ("%d ", *s0);
s0++;
}
printf ("\n");
}
int main (void)
{
int a[] = { 10, 4, 3, 7, 12, 0, 1, -1 };
int b[] = { 7, 14, 0, 8, 9, 22, 10, -1 };
f (a, b);
return 0;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.5\textwidth}
\vspace*{-\bigskipamount}
\begin{enumerate}[\quad(a)]
\item
Was bewirkt die Funktion \lstinline{f} und warum?\\
\points{4}
\item
Von welcher Ordnung (Landau-Symbol) ist die Funktion und warum?
Wir beziehen uns hierbei auf die Anzahl der Vergleiche
in Abhängigkeit von der Länge der Eingabedaten \lstinline{s0} und \lstinline{s1}.
Für die Rechnung dürfen Sie beide Längen mit $n$ gleichsetzen,
obwohl sie normalerweise nicht gleich sind.
\points{2}
\item
Was passiert, wenn Sie beim Aufruf der Funktion für einen der
Parameter den Wert \lstinline{NULL} übergeben und warum?
\points{2}
\item
Was passiert, wenn Sie das Hauptprogramm wie folgt abändern
(\file{aufgabe-1d.c}) und warum?
\begin{lstlisting}[gobble=8]
int main (void)
{
int a[] = { 10, 4, 3, 7, 12, 0, 1 };
int b[] = { 7, 14, 0, 8, 9, 22, 10 };
f (a, b);
return 0;
}
\end{lstlisting}
\points{2}
\item
Beschreiben Sie -- in Worten und/oder als C-Quelltext --, wie
sich die Funktion \lstinline{f} effizienter gestalten läßt,
wenn man die ihr übergebenen Arrays \lstinline{s0} und
\lstinline{s1} als sortiert voraussetzt.\\
\points{5}
\item
Von welcher
Ordnung (Landau-Symbol) ist Ihre effizientere Version der Funktion und warum?\\
\points{2}
\end{enumerate}
\end{minipage}
\end{document}
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "pair-storage.h"
pair fifo[STORAGE_SIZE];
int fifo_read_pointer = 0;
int fifo_write_pointer = 0;
void fatal_error (char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (1);
}
int storage_empty (void)
{
return fifo_read_pointer == fifo_write_pointer;
}
void push (int x, int y)
{
if (fifo_write_pointer < 0 || fifo_write_pointer >= STORAGE_SIZE)
fatal_error ("push: fifo inconsistence");
else
{
fifo[fifo_write_pointer].x = x;
fifo[fifo_write_pointer].y = y;
fifo_write_pointer++;
if (fifo_write_pointer >= STORAGE_SIZE)
fifo_write_pointer = 0;
if (fifo_write_pointer == fifo_read_pointer)
fatal_error ("fifo overflow");
}
}
void pop (int *x, int *y)
{
if (fifo_read_pointer < 0 || fifo_read_pointer >= STORAGE_SIZE)
fatal_error ("pop: fifo inconsistence");
else if (fifo_read_pointer == fifo_write_pointer)
fatal_error ("fifo underflow");
else
{
*x = fifo[fifo_read_pointer].x;
*y = fifo[fifo_read_pointer].y;
fifo[fifo_read_pointer].x = 0;
fifo[fifo_read_pointer].y = 0;
fifo_read_pointer++;
if (fifo_read_pointer >= STORAGE_SIZE)
fifo_read_pointer = 0;
}
}
void dump (void)
{
if (fifo_read_pointer == fifo_write_pointer)
printf ("fifo is empty\n");
else
{
printf ("fifo:");
int i = fifo_read_pointer;
while (i != fifo_write_pointer)
{
printf (" (%d,%d)", fifo[i].x, fifo[i].y);
i++;
if (i >= STORAGE_SIZE)
i = 0;
}
printf ("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#include "pair-storage.h"
#include <stdio.h>
#include <stdlib.h>
pair stack[STORAGE_SIZE];
int stack_pointer = 0;
void fatal_error (char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (1);
}
int storage_empty (void)
{
return stack_pointer <= 0;
}
void push (int x, int y)
{
if (stack_pointer < 0 || stack_pointer >= STORAGE_SIZE)
fatal_error ("stack overflow");
else
{
stack[stack_pointer].x = x;
stack[stack_pointer].y = y;
stack_pointer++;
}
}
void pop (int *x, int *y)
{
if (stack_pointer < 1 || stack_pointer > STORAGE_SIZE)
fatal_error ("stack underflow");
else
{
--stack_pointer;
if (!x)
fatal_error ("pop: parameter x is NULL");
else
*x = stack[stack_pointer].x;
if (!y)
fatal_error ("pop: parameter y is NULL");
else
*y = stack[stack_pointer].y;
}
}
void dump (void)
{
if (stack_pointer < 0)
fatal_error ("stack underflow");
else if (stack_pointer >= STORAGE_SIZE)
fatal_error ("stack overflow");
else if (stack_pointer == 0)
printf ("stack is empty\n");
else
{
printf ("stack:");
int i;
for (i = 0; i < stack_pointer; i++)
printf (" (%d,%d)", stack[i].x, stack[i].y);
printf ("\n");
}
}
#define STORAGE_SIZE 1000
typedef struct
{
int x, y;
}
pair;
extern int storage_empty (void);
extern void push (int x, int y);
extern void pop (int *x, int *y);
extern void dump (void);
../common/pgscript.sty
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
#include <stdio.h>
#include "pair-storage.h"
int main (void)
{
int x, y;
dump ();
push (1, 2);
dump ();
push (7, 3);
dump ();
pop (&x, &y);
printf ("popped (%d,%d)\n", x, y);
dump ();
push (42, 137);
dump ();
pop (&x, &y);
printf ("popped (%d,%d)\n", x, y);
dump ();
pop (&x, &y);
printf ("popped (%d,%d)\n", x, y);
dump ();
return 0;
}
#include <stdio.h>
#include "textgraph.h"
int main (void)
{
clear ('.');
display ();
printf ("\n");
for (int x = 5; x <= 15; x++)
putPoint (x, 3, '#');
for (int y = 4; y < 7; y++)
putPoint (4, y, '#');
for (int y = 4; y < 7; y++)
putPoint (18 - y, y, '#');
for (int x = 5; x <= 11; x++)
putPoint (x, 7, '#');
display ();
printf ("\n");
fill (9, 5, 'a', '.');
display ();
return 0;
}
#include "textgraph.h"
#include "pair-storage.h"
#include <stdio.h>
char screen[WIDTH][HEIGHT];
// Setze alle Einträge in display auf c
void clear (char c)
{
for (int x = 0; x < WIDTH; x++)
for (int y = 0; y < HEIGHT; y++)
screen[x][y] = c;
}
// Setze display [x][y] auf c
void putPoint (int x, int y, char c)
{
screen[x][y] = c;
}
// Gib display[x][y] zurück
char getPoint (int x, int y)
{
char c = screen[x][y];
return c;
}
// Fülle eine Fläche in display, die aus den Zeichen o besteht,
// mit den Zeichen c aus; zähle dabei c hoch
void fill (int x, int y, char c, char o)
{
push (x, y);
while (!storage_empty ())
{
pop (&x, &y);
if (getPoint (x, y) == o)
{
putPoint (x, y, c++);
push (x + 1, y);
push (x - 1, y);
push (x, y + 1);
push (x, y - 1);
}
}
}
// Gib display aus
void display (void)
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
printf ("%c", screen[x][y]);
printf ("\n");
}
}
#define WIDTH 20
#define HEIGHT 10
extern void clear (char c);
extern void putPoint (int x, int y, char c);
extern char getPoint (int x, int y);
extern void fill (int x, int y, char c, char o);
extern void display (void);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment