diff --git a/20231005/hp-20231005.pdf b/20231005/hp-20231005.pdf index bed537dad3781150e86ba681a4326a4d77eaba40..47b910261de1d25fc315bb1e623d076d01245e58 100644 Binary files a/20231005/hp-20231005.pdf and b/20231005/hp-20231005.pdf differ diff --git a/20231005/hp-20231005.tex b/20231005/hp-20231005.tex index bb6663dcc01b228d3261d87ee54c23a3b22c81d0..6f087f64de6231214240c00916c92b6254d635a1 100644 --- a/20231005/hp-20231005.tex +++ b/20231005/hp-20231005.tex @@ -20,7 +20,7 @@ % Attribution-ShareAlike 3.0 Unported License along with this % document. If not, see <http://creativecommons.org/licenses/>. -% README: Einführung, Einführung in C (bis Seiteneffekte) +% README: Einführung, Einführung in C (bis 2.7: Strukturierte Programmierung) \documentclass[10pt,t]{beamer} @@ -842,6 +842,7 @@ \item[2.4] Elementares Rechnen \item[2.5] Verzweigungen \item[2.6] Schleifen + \color{orange} \item[2.7] Strukturierte Programmierung \color{red} \item[2.8] Seiteneffekte @@ -864,6 +865,8 @@ \end{frame} +\iffalse + \subsection{Seiteneffekte} \begin{frame}[fragile] @@ -1022,4 +1025,6 @@ \end{frame} +\fi + \end{document} diff --git a/20231012/aufgabe-4.c b/20231012/aufgabe-4.c new file mode 100644 index 0000000000000000000000000000000000000000..b8b3de796fbef94bd4a86f944e1269b97e97982b --- /dev/null +++ b/20231012/aufgabe-4.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main (void) +{ + for (int i = 10; i = 0; i - 1) + printf ("%d\n", i); + return 0; +} diff --git a/20231012/gitlab.png b/20231012/gitlab.png new file mode 100644 index 0000000000000000000000000000000000000000..5724da8b8338534857e5e845f1f1a9b67f0fab1a Binary files /dev/null and b/20231012/gitlab.png differ diff --git a/20231012/hp-20231012-fig1.pdf b/20231012/hp-20231012-fig1.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2e878f1026f04f8363df3339f30f5cb936734baf Binary files /dev/null and b/20231012/hp-20231012-fig1.pdf differ diff --git a/20231012/hp-20231012-fig1.tex b/20231012/hp-20231012-fig1.tex new file mode 100644 index 0000000000000000000000000000000000000000..9993254ede0128398967c2a56ca2898533e2b861 --- /dev/null +++ b/20231012/hp-20231012-fig1.tex @@ -0,0 +1,60 @@ +\documentclass{article} +\input tmp.inputs +\pagestyle{empty} +\begin{document} + \psscalebox{0.79}{% + \begin{pspicture}(-5,0)(12,12) + \small + \psset{unit=0.5cm} + \psline[arrows=<->](-1,0)(-1,22) + \rput(-1.3,0){\makebox(0,0)[br]{\textbf{gegenständlich}}} + \rput(-1.3,22){\makebox(0,0)[tr]{\textbf{abstrakt}}} + \rput(-1.3,2){\makebox(0,0)[r]{Elektromagnetismus, Halbleiter}} + \rput(-1.3,4){\makebox(0,0)[r]{Elektronische Bauelemente}} + \rput(-1.3,6){\makebox(0,0)[r]{Logik-Schaltkreise}} + \rput(-1.3,8){\makebox(0,0)[r]{Prozessoren}} + \rput(-1.3,9){\makebox(0,0)[r]{Maschinensprache}} + \rput(-1.3,10){\makebox(0,0)[r]{Assembler}} + \rput(-1.3,11){\makebox(0,0)[r]{Ein-/Ausgabe}} + \rput(-1.3,12.35){\makebox(0,0)[r]{\textbf{hardwarenahe Programmierung} (z.\,B.\ in C)}} + \rput(-1.3,14){\makebox(0,0)[r]{\shortstack[r]{abstrahierende Programmierung\\(z.\,B.\ in C++, Java)}}} +% \rput(-1.3,15){\makebox(0,0)[r]{Programmierung}} + \rput(-1.3,16){\makebox(0,0)[r]{Algorithmen, Datenstrukturen, Software-Entwurf}} + \rput(-1.3,17){\makebox(0,0)[r]{Requirements Engineering}} + \rput(-1.3,18){\makebox(0,0)[r]{formale Sprachen, Berechenbarkeit}} + \rput(-1.3,19){\makebox(0,0)[r]{mathematische Strukturen}} + \rput(-1.3,20){\makebox(0,0)[r]{mathematische Beweise}} + \rput(2.1,0.5){\makebox(0,0)[l]{Physik}} + \rput(4.1,4){\makebox(0,0)[l]{Elektrotechnik}} + \rput(6.1,8){\makebox(0,0)[l]{Rechnertechnik}} + \rput(8.1,12.35){\makebox(0,0)[l]{angewandte Informatik}} + \rput(10.1,16){\makebox(0,0)[l]{\shortstack[l]{Softwaretechnik und\\theoretische Informatik}}} + \rput(12.1,21){\makebox(0,0)[l]{Mathematik}} + \psset{linewidth=0.001,linestyle=none,fillstyle=gradient,gradmidpoint=1.0,gradlines=1000} + \definecolor{RGBwhite}{rgb}{1.0,1.0,1.0} + \definecolor{RGBblue}{rgb}{0.0,0.0,1.0} + \definecolor{RGBred}{rgb}{1.0,0.0,0.0} + \definecolor{RGBgreen}{rgb}{0.0,1.0,0.0} + \definecolor{RGByellow}{rgb}{1.0,1.0,0.0} + \definecolor{RGBorange}{rgb}{1.0,0.7,0.0} + \definecolor{RGBgrey}{rgb}{0.7,0.7,0.7} + \rput(0,2){\psframe[gradbegin=RGBwhite,gradend=RGBblue](2,2)} + \rput(0,0){\psframe[fillstyle=solid,fillcolor=RGBblue](2,2.01)} + \rput(2,6){\psframe[gradbegin=RGBwhite,gradend=RGBred](2,2)} + \rput(2,2){\psframe[gradbegin=RGBred,gradend=RGBwhite](2,2)} + \rput(2,3.99){\psframe[fillstyle=solid,fillcolor=RGBred](2,2.02)} + \rput(4,10){\psframe[gradbegin=RGBwhite,gradend=RGBgreen](2,2)} + \rput(4,6){\psframe[gradbegin=RGBgreen,gradend=RGBwhite](2,2)} + \rput(4,7.99){\psframe[fillstyle=solid,fillcolor=RGBgreen](2,2.02)} + \rput(6,14){\psframe[gradbegin=RGBwhite,gradend=RGByellow](2,2)} + \rput(6,10){\psframe[gradbegin=RGByellow,gradend=RGBwhite](2,2)} + \rput(6,11.99){\psframe[fillstyle=solid,fillcolor=RGByellow](2,2.02)} + \rput(8,18){\psframe[gradbegin=RGBwhite,gradend=RGBorange](2,2)} + \rput(8,14){\psframe[gradbegin=RGBorange,gradend=RGBwhite](2,2)} + \rput(8,15.99){\psframe[fillstyle=solid,fillcolor=RGBorange](2,2.02)} + \rput(10,18){\psframe[gradbegin=RGBgrey,gradend=RGBwhite](2,2)} + \rput(10,19.99){\psframe[fillstyle=solid,fillcolor=RGBgrey](2,2.01)} + \end{pspicture} + } + +\end{document} diff --git a/20231012/hp-20231012.pdf b/20231012/hp-20231012.pdf new file mode 100644 index 0000000000000000000000000000000000000000..93c71c869abba34501199b479e959a4dc9e50d04 Binary files /dev/null and b/20231012/hp-20231012.pdf differ diff --git a/20231012/hp-20231012.tex b/20231012/hp-20231012.tex new file mode 100644 index 0000000000000000000000000000000000000000..ed26b56d5b6efb14dc4617d4f6ade27732c3e318 --- /dev/null +++ b/20231012/hp-20231012.tex @@ -0,0 +1,1601 @@ +% hp-20231012.pdf - Lecture Slides on Low-Level Programming +% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 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/>. + +% README: Einführung in C (ab 2.8: Seiteneffekte) + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{pdftricks} +\usepackage{tikz} + +\begin{psinputs} + \usepackage[utf8]{inputenc} + \usepackage[german]{babel} + \usepackage[T1]{fontenc} + \usepackage{helvet} + \renewcommand*\familydefault{\sfdefault} + \usepackage{pstricks,pst-grad} +\end{psinputs} + +\newcommand{\redurl}[1]{\href{#1}{\color{red}\nolinkurl{#1}}} + +\title{Hardwarenahe Programmierung} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{12.\ Oktober 2023} + +\begin{document} + +\maketitleframe + +%\date{\begin{picture}(0,0) +% \color{red} +% \put(0.65,1.05){\makebox(0,0)[t]{$\underbrace{\rule{1.45cm}{0pt}}_{% +% \mbox{\emph{rerum naturalium\/} = der natürlichen Dinge (lat.)}}$}} +% \put(1.65,-3){\makebox(0,0)[bl]{\redurl{https://www.peter.gerwinski.de/physik/}}} +% \end{picture}% +% 12.\ Oktober 2023} +% +%\maketitleframe + +\title{Hardwarenahe Programmierung} + +\sectionnonumber{Vorab: Online-Werkzeuge} + +\begin{frame} + + \showsectionnonumber + \begin{itemize} + \item + Diese Veranstaltung findet \textbf{in Präsenz} statt.\\ + Wir versuchen aber, auch eine Online-Teilnahme zu ermöglichen. + \medskip + \item + \textbf{Mumble}: Seminarraum 2\\ + Fragen: Mikrofon einschalten oder über den Chat\\ + Umfragen: über den Chat -- \textbf{auch während der Präsenz-Veranstaltung} + \smallskip + \item + \textbf{VNC}: Kanal 6, Passwort: \lstinline[style=cmd]{testcvh}\\ + Eigenen Bildschirm freigeben: VNC-Software oder Web-Interface \emph{yesVNC}\\ + Eigenes Kamerabild übertragen: Web-Interface \emph{CVH-Camera} + \smallskip + \item + Allgemeine Informationen: + \url{https://www.cvh-server.de/online-werkzeuge/} + \smallskip + \item + Notfall-Schnellzugang: \url{https://www.cvh-server.de/virtuelle-raeume/}\\ + Seminarraum 2, VNC-Passwort: \lstinline[style=cmd]{testcvh} +% \smallskip +% \item +% Bei Problemen: bitte notieren:\\ +% Art des Problems, genaue Uhrzeit, eigener Internet-Zugang + \bigskip + \item + \textbf{Lehrmaterialien:} \url{https://gitlab.cvh-server.de/pgerwinski/hp} + \end{itemize} + +\end{frame} + +\begin{frame}[fragile] + + \vspace{-0.6cm} + \begin{pdfpic} + \psscalebox{0.79}{% + \begin{pspicture}(-5,0)(12,12) + \small + \psset{unit=0.5cm} + \psline[arrows=<->](-1,0)(-1,22) + \rput(-1.3,0){\makebox(0,0)[br]{\textbf{gegenständlich}}} + \rput(-1.3,22){\makebox(0,0)[tr]{\textbf{abstrakt}}} + \rput(-1.3,2){\makebox(0,0)[r]{Elektromagnetismus, Halbleiter}} + \rput(-1.3,4){\makebox(0,0)[r]{Elektronische Bauelemente}} + \rput(-1.3,6){\makebox(0,0)[r]{Logik-Schaltkreise}} + \rput(-1.3,8){\makebox(0,0)[r]{Prozessoren}} + \rput(-1.3,9){\makebox(0,0)[r]{Maschinensprache}} + \rput(-1.3,10){\makebox(0,0)[r]{Assembler}} + \rput(-1.3,11){\makebox(0,0)[r]{Ein-/Ausgabe}} + \rput(-1.3,12.35){\makebox(0,0)[r]{\textbf{hardwarenahe Programmierung} (z.\,B.\ in C)}} + \rput(-1.3,14){\makebox(0,0)[r]{\shortstack[r]{abstrahierende Programmierung\\(z.\,B.\ in C++, Java)}}} +% \rput(-1.3,15){\makebox(0,0)[r]{Programmierung}} + \rput(-1.3,16){\makebox(0,0)[r]{Algorithmen, Datenstrukturen, Software-Entwurf}} + \rput(-1.3,17){\makebox(0,0)[r]{Requirements Engineering}} + \rput(-1.3,18){\makebox(0,0)[r]{formale Sprachen, Berechenbarkeit}} + \rput(-1.3,19){\makebox(0,0)[r]{mathematische Strukturen}} + \rput(-1.3,20){\makebox(0,0)[r]{mathematische Beweise}} + \rput(2.1,0.5){\makebox(0,0)[l]{Physik}} + \rput(4.1,4){\makebox(0,0)[l]{Elektrotechnik}} + \rput(6.1,8){\makebox(0,0)[l]{Rechnertechnik}} + \rput(8.1,12.35){\makebox(0,0)[l]{angewandte Informatik}} + \rput(10.1,16){\makebox(0,0)[l]{\shortstack[l]{Softwaretechnik und\\theoretische Informatik}}} + \rput(12.1,21){\makebox(0,0)[l]{Mathematik}} + \psset{linewidth=0.001,linestyle=none,fillstyle=gradient,gradmidpoint=1.0,gradlines=1000} + \definecolor{RGBwhite}{rgb}{1.0,1.0,1.0} + \definecolor{RGBblue}{rgb}{0.0,0.0,1.0} + \definecolor{RGBred}{rgb}{1.0,0.0,0.0} + \definecolor{RGBgreen}{rgb}{0.0,1.0,0.0} + \definecolor{RGByellow}{rgb}{1.0,1.0,0.0} + \definecolor{RGBorange}{rgb}{1.0,0.7,0.0} + \definecolor{RGBgrey}{rgb}{0.7,0.7,0.7} + \rput(0,2){\psframe[gradbegin=RGBwhite,gradend=RGBblue](2,2)} + \rput(0,0){\psframe[fillstyle=solid,fillcolor=RGBblue](2,2.01)} + \rput(2,6){\psframe[gradbegin=RGBwhite,gradend=RGBred](2,2)} + \rput(2,2){\psframe[gradbegin=RGBred,gradend=RGBwhite](2,2)} + \rput(2,3.99){\psframe[fillstyle=solid,fillcolor=RGBred](2,2.02)} + \rput(4,10){\psframe[gradbegin=RGBwhite,gradend=RGBgreen](2,2)} + \rput(4,6){\psframe[gradbegin=RGBgreen,gradend=RGBwhite](2,2)} + \rput(4,7.99){\psframe[fillstyle=solid,fillcolor=RGBgreen](2,2.02)} + \rput(6,14){\psframe[gradbegin=RGBwhite,gradend=RGByellow](2,2)} + \rput(6,10){\psframe[gradbegin=RGByellow,gradend=RGBwhite](2,2)} + \rput(6,11.99){\psframe[fillstyle=solid,fillcolor=RGByellow](2,2.02)} + \rput(8,18){\psframe[gradbegin=RGBwhite,gradend=RGBorange](2,2)} + \rput(8,14){\psframe[gradbegin=RGBorange,gradend=RGBwhite](2,2)} + \rput(8,15.99){\psframe[fillstyle=solid,fillcolor=RGBorange](2,2.02)} + \rput(10,18){\psframe[gradbegin=RGBgrey,gradend=RGBwhite](2,2)} + \rput(10,19.99){\psframe[fillstyle=solid,fillcolor=RGBgrey](2,2.01)} + \end{pspicture} + } + \end{pdfpic} + +\end{frame} + +\sectionnonumber{Hardwarenahe Programmierung} +\subsectionnonumber{Programmierung in C} + +\begin{frame} + + \showsectionnonumber + + Man kann Computer vollständig beherrschen. + +% \begin{onlyenv}<1> +% +% \vspace*{2cm} +% +% {\Large\color{structure}\textbf{Rechnertechnik}} +% +% \bigskip +% +% Man kann vollständig verstehen, wie Computer funktionieren. +% +% \vspace*{-3cm} +% +% \end{onlyenv} + +% \pause + + \bigskip + \bigskip + + \showsubsectionnonumber + + \vspace{-\medskipamount} + + \begin{itemize} +% \item +% kleinster gemeinsamer Nenner für viele Plattformen\\ +% \pause +% \begin{picture}(0,1) +% \color{red} +% \put(7.2,0.6){\vector(0,1){0.4}} +% \put(7.2,0.5){\makebox(0,0)[t]{\color{black}Hardware und/oder Betriebssystem}} +% \end{picture} +% \pause +% \medskip + \item + Hardware direkt ansprechen und effizient einsetzen + \medskip + \item + \dots\ bis hin zu komplexen Software-Projekten + \medskip + \item + Programmierkenntnisse werden nicht vorausgesetzt,\\ + aber schnelles Tempo + \end{itemize} + +\end{frame} + +\iffalse + +\subsectionnonumber{Programmierung in C und C++} + +\begin{frame} + + \showsectionnonumber + + Man kann Computer vollständig beherrschen. + + \bigskip + + \showsubsectionnonumber + + Etabliertes Profi-Werkzeug + \begin{itemize} + \item + kleinster gemeinsamer Nenner für viele Plattformen\\ + \begin{picture}(0,1) + \color{red} + \put(7.2,0.6){\vector(0,1){0.4}} + \put(7.2,0.5){\makebox(0,0)[t]{\color{black}Hardware und/oder Betriebssystem}} + \end{picture} + \medskip + \item + Programmierkenntnisse werden nicht vorausgesetzt,\\ + aber schnelles Tempo + \bigskip + \item + Hardware direkt ansprechen und effizient einsetzen + \bigskip + \item + \dots\ bis hin zu komplexen Software-Projekten + \end{itemize} + +\end{frame} + +\fi + +\subsectionnonumber{Was ist C?} + +\begin{frame} + + \showsectionnonumber + \showsubsectionnonumber + + Etabliertes Profi-Werkzeug + \begin{itemize} + \item + kleinster gemeinsamer Nenner für viele Plattformen +% \pause + \begin{picture}(0,0) + \color{red} + \put(-1.0,-0.8){\vector(0,1){0.7}} + \put(-1.0,-0.9){\makebox(0,0)[t]{\color{black}Hardware und/oder Betriebssystem}} + \end{picture} +% \pause + \item + leistungsfähig, aber gefährlich + \end{itemize} + +% \pause + \medskip + "`High-Level-Assembler"' + \begin{itemize} +% \pause + \item + kein "`Fallschirm"' +% \pause + \item + kompakte Schreibweise + \end{itemize} + +% \pause + \medskip + Unix-Hintergrund + \begin{itemize} +% \pause + \item + Baukastenprinzip +% \pause + \item + konsequente Regeln +% \pause + \item + kein "`Fallschirm"' + \end{itemize} + +\end{frame} + +\sectionnonumber{Zu dieser Lehrveranstaltung} + +\begin{frame} + + \showsectionnonumber + + \begin{itemize} + \item + \textbf{Lehrmaterialien:}% + \hfill\makebox(0,0)[r]{\includegraphics[width=3.5cm]{gitlab.png}}\\ + \url{https://gitlab.cvh-server.de/pgerwinski/hp} + \item + \textbf{Klausur:} + + Zeit: 120 Minuten + + Zulässige Hilfsmittel: + \begin{itemize} + \item Schreibgerät + \item beliebige Unterlagen in Papierform und/oder auf Datenträgern + \item elektronische Rechner (Notebook, Taschenrechner o.\,ä.) + \item \emph{kein\/} Internet-Zugang + \end{itemize} + \item + \textbf{Übungen}\\ + sind mit der Vorlesung und dem Praktikum integriert. + \item + Das \textbf{Praktikum}\\ + findet jede Woche statt.\\ + Diese Woche: vorbereitende Maßnahmen,\\ + Kennenlernen der verwendeten Werkzeuge.\\ + Im Laufe des Semesters: \textbf{4 Praktikumsversuche} + \end{itemize} + +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}} + \begin{itemize} + \color{medgreen} + \item[1.1] Was ist hardwarenahe Programmierung? + \item[1.2] Programmierung in C + \item[1.3] Zu dieser Lehrveranstaltung + \end{itemize} + \item[\textbf{2}] \textbf{Einführung in C} + \begin{itemize} + \color{medgreen} + \item[2.1] Hello, world! + \item[2.2] Programme compilieren und ausführen + \item[2.3] Elementare Aus- und Eingabe + \item[2.4] Elementares Rechnen + \item[2.5] Verzweigungen + \item[2.6] Schleifen + \color{orange} + \item[2.7] Strukturierte Programmierung + \item[\dots] +% \item[2.8] Seiteneffekte +% \item[2.9] Funktionen +% \item[2.10] Zeiger +% \item[2.11] Arrays und Strings +% \item[2.12] Strukturen +% \item[\dots] + \end{itemize} + \color{gray} + \item[\textbf{3}] \textbf{Bibliotheken} + \vspace*{-\smallskipamount} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + +\end{frame} + +\section{Einführung} +\section{Einführung in C} +\subsection{Hello, world!} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + Text ausgeben + + \bigskip + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + printf ("Hello, world!\n"); + return 0; + } + \end{lstlisting} +% \pause + \begin{picture}(0,0) + \color{red} + \put(5.0,1.5){\makebox(0,0)[bl]{printf = "`print formatted"'}} +% \pause + \put(4.0,0.4){\makebox(0,0)[br]{\tikz{\draw[-latex](0.0,0.0)--(-0.3,0.7);}}} + \put(4.0,0.3){\makebox(0,0)[t]{\textbackslash n: Zeilenschaltung}} + \end{picture} + +\end{frame} + +\subsection{Programme compilieren und ausführen} + +\begin{frame}[fragile] + + \showsubsection + + \begin{onlyenv}<1-5> + \begin{lstlisting}[style=terminal,gobble=6] + $ ¡gcc hello-1.c -o hello-1¿ + $ ¡./hello-1¿ + Hello, world! + $ + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<6-> + \begin{lstlisting}[style=terminal,gobble=6] + $ ¡gcc -Wall -O hello-1.c -o hello-1¿ + $ ¡./hello-1¿ + Hello, world! + $ + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<3> + \bigskip + Hier: Kommandozeilen-Interface (CLI) + \begin{itemize} + \item + Der C-Compiler (hier: \lstinline[style=cmd]{gcc}) + muß installiert sein und sich im \lstinline[style=terminal]{PATH} befinden. + \item + Der Quelltext (hier: \file{hello-1.c}) + muß sich im aktuellen Verzeichnis befinden. + \item + aktuelles Verzeichnis herausfinden: \lstinline[style=cmd]{pwd} + \item + aktuelles Verzeichnis wechseln: \lstinline[style=cmd]{cd foobar}, \lstinline[style=cmd]{cd ..} + \item + Inhalt des aktuellen Verzeichnisses ausgeben: \lstinline[style=cmd]{ls}, \lstinline[style=cmd]{ls -l} + \item + Ausführen des Programms (\file{hello-1}) im aktuellen Verzeichnis (\file{.}):\\ + \lstinline[style=cmd]{./hello-1} + \end{itemize} + + Alternative: Integrierte Entwicklungsumgebung (IDE)\\ + mit graphischer Benutzeroberfläche (GUI) + \begin{itemize} + \item + Das können Sie bereits. + \end{itemize} + \vspace*{-1cm} + \end{onlyenv} + \begin{onlyenv}<4> + \bigskip + GNU Compiler Collection (GCC) für verschiedene Plattformen: + \begin{itemize} + \item + GNU/Linux: \file{gcc} + \item + Apple Mac OS: \file{Xcode} + \item + Microsoft Windows: \file{Cygwin}\\ + oder \file{MinGW} mit \file{MSYS}\\ + oder \file{WSL} mit darin installiertem \file{GNU/Linux} + \medskip + \item + außerdem: Texteditor\\ + \file{vi}(\file{m}), \file{nano}, + \file{Emacs}, \file{Notepad++}, \dots\\ + (Microsoft Notepad ist \emph{nicht} geeignet!) + \end{itemize} + \end{onlyenv} + \begin{onlyenv}<2> + \begin{picture}(0,0) + \color{red} + \put(4.0,0.5){\tikz{\draw[-latex](0.0,0.0)--(-2.0,1.0);}} + \put(4.0,0.3){\makebox(0,0)[tl]{\begin{minipage}[t]{8cm} + \begin{tabular}{ll} + \lstinline[style=cmd]{-o hello-1} + & Name für Ausgabe-Datei ("`output"') \\ + & unter Unix: ohne Endung \\ + & unter MS-Windows: Endung \lstinline[style=cmd]{.exe} + \end{tabular} + \end{minipage}}} + \end{picture} + \end{onlyenv} + \begin{onlyenv}<6-> + \begin{picture}(0,0) + \color{red} + \put(3.0,0.5){\tikz{\draw[-latex](0.0,0.0)--(-3.0,1.0);}} + \put(4.0,0.3){\makebox(0,0)[tl]{\begin{minipage}[t]{6cm} + \begin{tabular}{ll} + \lstinline[style=cmd]{-Wall} & alle Warnungen einschalten\\ + \lstinline[style=cmd]{-O} & optimieren\\ + \lstinline[style=cmd]{-O3} & maximal optimieren\\ + \lstinline[style=cmd]{-Os} & Codegröße optimieren\\[\smallskipamount] + \dots & gcc hat \emph{sehr viele\/} Optionen. + \end{tabular} + \end{minipage}}} + \end{picture} + \end{onlyenv} + +\end{frame} + +\subsection{Elementare Aus- und Eingabe} + +\begin{frame}[fragile] + + \showsubsection + + Wert ausgeben + + \bigskip + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + printf ("Die Antwort lautet: "); + printf (42); + printf ("\n"); + return 0; + } + \end{lstlisting} +% \pause + \medskip + \textarrow\ Absturz + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Wert ausgeben + + \bigskip + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + printf ("Die Antwort lautet: %d\n", 42); + return 0; + } + \end{lstlisting} + \begin{picture}(0,0) + \color{red} + \put(5.3,0.65){\tikz{\draw[-latex](0.0,0.0)--(-0.5,0.5);}} + \put(5.9,0.35){\begin{minipage}[t]{6.5cm} + Formatspezifikation "`d"': "`dezimal"'\\[\smallskipamount] + Weitere Formatspezifikationen:\\ + siehe Dokumentation (z.\,B.\ man 3 printf),\\ + Internet-Recherche oder Literatur + \end{minipage}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Wert einlesen + + \bigskip + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + double a; + printf ("Bitte eine Zahl eingeben: "); + scanf ("%lf", &a); + printf ("Ihre Antwort war: %lf\n", a); + return 0; + } + \end{lstlisting} + \begin{picture}(0,0) + \color{red} + \put(3.4,1.8){\tikz{\draw[-latex](0.0,0.0)--(-3.5,0);}} + \put(7.0,1.9){\begin{minipage}[t]{5cm} + Formatspezifikation "`lf"':\\ + "`long floating-point"'\\[\smallskipamount] + Das "`\&"' nicht vergessen! + \end{minipage}} + \end{picture} + +\end{frame} + +\subsection{Elementares Rechnen} + +\begin{frame}[fragile] + + \showsubsection + + Wert an Variable zuweisen + + \bigskip + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + int a; + printf ("Bitte eine Zahl eingeben: "); + scanf ("%d", &a); + a = 2 * a; + printf ("Das Doppelte ist: %d\n", a); + return 0; + } + \end{lstlisting} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Variable bei Deklaration initialisieren + + \bigskip + + \begin{lstlisting} + int a = 42; + a = 137; + \end{lstlisting} + + \bigskip + + \textbf{Achtung:} Initialisierung $\ne$ Zuweisung + + \smallskip + + Die beiden Gleichzeitszeichen haben\\ + \emph{subtil unterschiedliche\/} Bedeutungen! + +\end{frame} + +\subsection{Verzweigungen} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}[t]{6cm} + \lstinline{if}-Verzweigung + + \bigskip + + \begin{lstlisting}[gobble=6] + if (b != 0) + printf ("%d\n", a / b); + \end{lstlisting} + \end{minipage}% \pause + \begin{minipage}[t]{6cm} + \textbf{Wahrheitswerte} in C: \textbf{numerisch}\\[\medskipamount] + 0 steht für \emph{falsch (false)},\\ + $\ne 0$ steht für \emph{wahr (true)}. + + \bigskip + + \begin{lstlisting}[gobble=6] + if (b) + printf ("%d\n", a / b); + \end{lstlisting} + \end{minipage} + +\end{frame} + +\subsection{Schleifen} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}[t]{6cm} + \lstinline{while}-Schleife + \bigskip + \begin{lstlisting}[gobble=6] + a = 1; + while (a <= 10) + { + printf ("%d\n", a); + a = a + 1; + } + \end{lstlisting} + \end{minipage}%\pause + \begin{minipage}[t]{6cm} + \lstinline{for}-Schleife + \bigskip + \begin{lstlisting}[gobble=6] + for (a = 1; a <= 10; a = a + 1) + printf ("%d\n", a); + \end{lstlisting} + \vspace{1.5cm} +% \pause + \lstinline{do}-\lstinline{while}-Schleife + \bigskip + \begin{lstlisting}[gobble=6] + a = 1; + do + { + printf ("%d\n", a); + a = a + 1; + } + while (a <= 10); + \end{lstlisting} + \end{minipage} + +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}} + \begin{itemize} + \color{medgreen} + \item[1.1] Was ist hardwarenahe Programmierung? + \item[1.2] Programmierung in C + \item[1.3] Zu dieser Lehrveranstaltung + \end{itemize} + \item[\textbf{2}] \textbf{Einführung in C} + \begin{itemize} + \color{medgreen} + \item[2.1] Hello, world! + \item[2.2] Programme compilieren und ausführen + \item[2.3] Elementare Aus- und Eingabe + \item[2.4] Elementares Rechnen + \item[2.5] Verzweigungen + \item[2.6] Schleifen + \color{orange} + \item[2.7] Strukturierte Programmierung + \color{red} + \item[2.8] Seiteneffekte + \item[2.9] Funktionen + \item[2.10] Zeiger + \item[\dots] +% \item[2.11] Arrays und Strings +% \item[2.12] Strukturen +% \item[\dots] + \end{itemize} + \color{gray} + \item[\textbf{3}] \textbf{Bibliotheken} + \vspace*{-\smallskipamount} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\subsection{Strukturierte Programmierung} + +\begin{frame}[fragile] + + \visible<4->{\showsubsection} + + \begin{minipage}[t]{6cm} + \begin{onlyenv}<2-> + \begin{lstlisting}[gobble=8] + ¡i = 0; + while (1) + { + if (i >= 10) + break; + printf ("%d\n", i++); + }¿ + \end{lstlisting} + \end{onlyenv} + \strut + \bigskip + \begin{onlyenv}<3-> + \begin{lstlisting}[gobble=8] + ¡i = 0; + loop: + if (i >= 10) + goto endloop; + printf ("%d\n", i++); + goto loop; + endloop:¿ + \end{lstlisting} + \end{onlyenv} + \end{minipage} + \begin{minipage}[t]{6cm} + \vspace{-\bigskipamount} + \begin{lstlisting} + ¡int i; + \end{lstlisting} + \bigskip + \begin{lstlisting} + ¡i = 0; + while (i < 10) + { + printf ("%d\n", i); + i++; + }¿ + \end{lstlisting} + \bigskip + \begin{lstlisting} + ¡for (i = 0; i < 10; i++) + printf ("%d\n", i);¿ + \end{lstlisting} + \bigskip + \begin{lstlisting} + ¡i = 0; + while (i < 10) + printf ("%d\n", i++);¿ + \end{lstlisting} + \bigskip + \begin{lstlisting} + ¡for (i = 0; i < 10; printf ("%d\n", i++));¿ + \end{lstlisting} + \end{minipage}% + \begin{onlyenv}<5-> + \begin{picture}(0,0) + \put(-1.9,-3.0){\mbox{\color{medgreen}gut}} + \put(-1.9,-5.2){\begin{minipage}{5cm} + \color{orange} + nur, wenn\\ + Sie wissen,\\ + was Sie tun + \end{minipage}} + + \put(-10,-0.5){\mbox{\color{red}fragwürdig}} + \put(-9.3,-4.2){\begin{minipage}[t]{5cm} + \color{red} + sehr fragwürdig\\[\smallskipamount] + \footnotesize(siehe z.\,B.:\\ + http://xkcd.com/292/) + \end{minipage}} + \end{picture} + \end{onlyenv} + \vspace*{-1cm} + +\end{frame} + +\subsection{Seiteneffekte} + +\begin{frame}[fragile] + + \showsubsection + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + printf ("Hello, world!\n"); + "Hello, world!\n"; + return 0; + } + \end{lstlisting} + \pause + \begin{picture}(0,0) + \color{red} + \put(3.1,1.35){\tikz{\draw[-latex](0.0,0.0)--(-0.8,0);}} + \put(4.0,1.35){\makebox(0,0)[l]{Ausdruck als Anweisung: Wert wird ignoriert}} + \pause + \put(3.55,1.40){\tikz{\draw[-latex](0.0,0.0)--(-0.4,0.2);}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + int a = printf ("Hello, world!\n"); + printf ("%d\n", a); + return 0; + } + \end{lstlisting} + \pause + \bigskip + \begin{lstlisting}[style=terminal] + $ ¡gcc -Wall -O side-effects-1.c -o side-effects-1¿ + $ ¡./side-effects-1¿ + Hello, world! + 14 + $ + \end{lstlisting} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + \begin{lstlisting} + #include <stdio.h> + + int main (void) + { + int a = printf ("Hello, world!\n"); + printf ("%d\n", a); + return 0; + } + \end{lstlisting} + \begin{itemize} + \item + \lstinline{printf()} ist eine Funktion. + \pause + \item + "`Haupteffekt"': Wert zurückliefern\\ + (hier: Anzahl der ausgegebenen Zeichen) + \pause + \item + \newterm{Seiteneffekt\/}: Ausgabe + \end{itemize} + +\end{frame} + +\addtocounter{subsection}{-1} +\subsection{Seiteneffekte \protect\color{gray}bei Operatoren} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}[t]{6cm} + Unäre Operatoren: + \begin{itemize} + \item + Negation: \lstinline{-foo} + \item + {\only<2->{\color{red}}Funktionsaufruf}: \lstinline{foo ()} + \item + {\only<2->{\color{red}}Post-Inkrement}: \lstinline{foo++} + \item + {\only<2->{\color{red}}Post-Dekrement}: \lstinline{foo--} + \item + {\only<2->{\color{red}}Prä-Inkrement}: \lstinline{++foo} + \item + {\only<2->{\color{red}}Prä-Dekrement}: \lstinline{--foo} + \end{itemize} + \medskip + Binäre Operatoren: + \begin{itemize} + \item + Rechnen: \lstinline{+ - * / %} + \item + Vergleich: \lstinline{== != < > <= >=} + \item + {\only<2->{\color{red}}Zuweisung}: \lstinline{= += -= *= /= %=} + \item + Ignorieren: \lstinline{,}\quad \lstinline{a, b}: berechne \lstinline{a}, ignoriere es, nimm stattdessen \lstinline{b} + \end{itemize} + \pause + \medskip + {\color{red}rot = mit Seiteneffekt} + \end{minipage} + \pause + \begin{minipage}[t]{6cm} + \vspace{-\bigskipamount} + \begin{lstlisting} + ¡int i;¿ + \end{lstlisting} + \bigskip + \begin{lstlisting} + ¡i = 0; + while (i < 10) + { + printf ("%d\n", i); + i++; + }¿ + \end{lstlisting} + \bigskip + \pause + \begin{lstlisting} + ¡for (i = 0; i < 10; i++) + printf ("%d\n", i);¿ + \end{lstlisting} + \bigskip + \pause + \begin{lstlisting} + ¡i = 0; + while (i < 10) + printf ("%d\n", i++);¿ + \end{lstlisting} + \bigskip + \pause + \begin{lstlisting} + ¡for (i = 0; i < 10; printf ("%d\n", i++));¿ + \end{lstlisting} + \end{minipage} + \vspace*{-1cm} + +\end{frame} + +\subsection{Funktionen} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}[t]{6.5cm} + \vspace{-\medskipamount} + \begin{lstlisting}[gobble=6] + ¡#include <stdio.h> + + int answer (void) + { + return 42; + } + + void foo (void) + { + printf ("%d\n", answer ()); + } + + int main (void) + { + foo (); + return 0; + }¿ + \end{lstlisting} + \vspace*{-1cm} + \end{minipage}% + \begin{minipage}[t]{6cm} + \begin{itemize} + \pause + \item + Funktionsdeklaration:\\[\smallskipamount] + Typ Name ( Parameterliste )\\ + \{\\ + \quad Anweisungen\\ + \} + \end{itemize} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}[t]{6.5cm} + \vspace{-\medskipamount} + \begin{lstlisting}[gobble=6] + ¡#include <stdio.h> + + void add_verbose (int a, int b) + { + printf ("%d + %d = %d\n", a, b, a + b); + } + + int main (void) + { + add_verbose (3, 7); + return 0; + }¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{6cm} + \begin{itemize} + \item + Funktionsdeklaration:\\[\smallskipamount] + Typ Name ( Parameterliste )\\ + \{\\ + \quad Anweisungen\\ + \} + \pause + \bigskip + \item + Der Datentyp \lstinline{void}\\ + steht für "`nichts"'\\ + und \alt<3->{muß}{kann} ignoriert werden. + \end{itemize} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + \begin{minipage}{6cm} + \begin{lstlisting}[gobble=6] + ¡#include <stdio.h> + + int a, b = 3; + + void foo (void) + { + b++; + static int a = 5; + int b = 7; + printf ("foo(): " + "a = %d, b = %d\n", + a, b); + a++; + }¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}{6cm} + \vspace{-1cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + printf ("main(): " + "a = %d, b = %d\n", + a, b); + foo (); + printf ("main(): " + "a = %d, b = %d\n", + a, b); + a = b = 12; + printf ("main(): " + "a = %d, b = %d\n", + a, b); + foo (); + printf ("main(): " + "a = %d, b = %d\n", + a, b); + return 0; + }¿ + \end{lstlisting} + \end{minipage} + +\end{frame} + +\subsection{Zeiger} + +\begin{frame}[fragile] + \showsubsection + + \begin{lstlisting} + #include <stdio.h> + + void calc_answer (int *a) + { + *a = 42; + } + + int main (void) + { + int answer; + calc_answer (&answer); + printf ("The answer is %d.\n", answer); + return 0; + } + \end{lstlisting} + + \pause + \vspace{-5cm}\hspace{5cm}% + \begin{minipage}{7cm} + \begin{itemize} + \item + \lstinline{*a} ist eine \lstinline{int}. + \pause + \item + unärer Operator \lstinline{*}:\\ + Pointer-Dererefenzierung + \pause + \arrowitem + \lstinline{a} ist ein Zeiger (Pointer) auf eine \lstinline{int}. + \pause + \bigskip + \item + unärer Operator \lstinline{&}: Adresse + \end{itemize} + \end{minipage} +\end{frame} + +\subsection{Arrays und Strings} + +\begin{frame}[fragile] + \showsubsection + + Ein Zeiger zeigt auf eine Variable\only<2->{ und deren Nachbarn}. + +% \bigskip + \pause + \pause + + \begin{onlyenv}<1-8> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[5] = { 2, 3, 5, 7, 11 }; + int *p = prime; + for (int i = 0; i < 5; i++) + printf ("%d\n", *(p + i)); + return 0; + } + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<9> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[5] = { 2, 3, 5, 7, 11 }; + int *p = prime; + for (int i = 0; i < 5; i++) + printf ("%d\n", p[i]); + return 0; + } + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<10> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[5] = { 2, 3, 5, 7, 11 }; + for (int i = 0; i < 5; i++) + printf ("%d\n", prime[i]); + return 0; + } + ¡ ¿ + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<11> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[5] = { 2, 3, 5, 7, 11 }; + for (int *p = prime; + p < prime + 5; p++) + printf ("%d\n", *p); + return 0; + } + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<12> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[6] = { 2, 3, 5, 7, 11, 0 }; + for (int *p = prime; *p; p++) + printf ("%d\n", *p); + return 0; + } + ¡ ¿ + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<13-> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + int prime[] = { 2, 3, 5, 7, 11, 0 }; + for (int *p = prime; *p; p++) + printf ("%d\n", *p); + return 0; + } + ¡ ¿ + \end{lstlisting} + \end{onlyenv} + + \pause + \vspace{-3.05cm}\hspace{5.5cm}% + \begin{minipage}{6.5cm} + \begin{itemize} + \item + \lstinline{prime} ist \alt<5->{ein Array}{eine Ansammlung} von\\fünf ganzen Zahlen. + \pause + \pause + \item + \only<6-9>{\begin{picture}(0,0) + \color{red} + \put(-1.6,0.1){\tikz{\draw[-latex](0.0,0.0)--(-1,0);}} + \end{picture}}% + \lstinline{prime} ist ein Zeiger auf eine \lstinline{int}. + \pause + \item + \lstinline{p + i} ist ein Zeiger\\ + auf den \lstinline{i}-ten Nachbarn von \lstinline{*p}. + \pause + \item + \lstinline{*(p + i)} ist der \lstinline{i}-te Nachbar von \lstinline{*p}. + \pause + \item + Andere Schreibweise:\\ + \lstinline{p[i]} statt \lstinline{*(p + i)} + \pause + \pause + \item + Zeiger-Arithmetik:\\ + \lstinline{p++} rückt den Zeiger \lstinline{p}\\ + um eine \lstinline{int} weiter. + \pause + \pause + \item + Array ohne \only<14->{explizite }Längenangabe:\\ + Compiler zählt selbst + \vspace*{-1cm} + \pause + \begin{picture}(0,0) + \put(-5.2,1.0){\makebox(0,0)[br]{\color{red}\bf\shortstack{Die Länge des Arrays\\ist \emph{nicht\/} veränderlich!}}} + \end{picture} + \end{itemize} + \end{minipage} +\end{frame} + +% \begin{frame}[fragile] +% \showsubsection +% +% \begin{lstlisting} +% #include <stdio.h> +% +% int main (void) +% { +% char hello_world[] = "Hello, world!\n"; +% int i = 0; +% while (hello_world[i] != 0) +% printf ("%d", hello_world[i++]); +% return 0; +% } +% \end{lstlisting} +% \end{frame} + +% \begin{frame}[fragile] +% \showsubsection +% +% \begin{lstlisting} +% #include <stdio.h> +% +% int main (void) +% { +% char hello_world[] = "Hello, world!\n"; +% int i = 0; +% while (hello_world[i]) +% printf ("%d", hello_world[i++]); +% return 0; +% } +% \end{lstlisting} +% \end{frame} + +% \begin{frame}[fragile] +% \showsubsection +% +% \begin{lstlisting} +% #include <stdio.h> +% +% int main (void) +% { +% char hello_world[] = "Hello, world!\n"; +% char *p = hello_world; +% while (*p) +% printf ("%c", *p++); +% return 0; +% } +% \end{lstlisting} +% \end{frame} + +\begin{frame}[fragile] + \showsubsection + + \begin{onlyenv}<1-6> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + char hello[] = "Hello, world!\n"; + for (char *p = hello; *p; p++) + printf ("%d", *p); + return 0; + } + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<7> + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + char hello[] = "Hello, world!\n"; + for (char *p = hello; *p; p++) + printf ("%c", *p); + return 0; + } + \end{lstlisting} + \end{onlyenv} +% \begin{onlyenv}<8> +% \begin{lstlisting}[gobble=6] +% #include <stdio.h> +% +% int main (void) +% { +% char hello[] = "Hello, world!\n"; +% printf ("%s", hello); +% return 0; +% } +% ¡ ¿ +% \end{lstlisting} +% \end{onlyenv} +% \begin{onlyenv}<9> +% \begin{lstlisting}[gobble=6] +% #include <stdio.h> +% +% int main (void) +% { +% char *hello = "Hello, world!\n"; +% printf ("%s", hello); +% return 0; +% } +% ¡ ¿ +% \end{lstlisting} +% \end{onlyenv} +% \begin{onlyenv}<10> +% \begin{lstlisting}[gobble=6] +% #include <stdio.h> +% +% int main (void) +% { +% char *hello = "Hello, world!\n"; +% while (*hello) +% printf ("%c", *hello++); +% return 0; +% } +% \end{lstlisting} +% \end{onlyenv} + + \vspace{-1.7cm}\hfill + \begin{minipage}{6.8cm} + \begin{itemize} + \pause[2] + \item + Ein \lstinline{char} ist eine kleinere \lstinline{int}. + \pause + \item + Ein "`String"' in C ist ein Array von \lstinline{char}s\only<4->{,\\ + also ein Zeiger auf \lstinline{char}s}\only<5->{\\ + also ein Zeiger auf (kleinere) Integer}. + \pause + \pause + \pause + \item + Der letzte \lstinline{char} muß 0 sein.\\ + Er kennzeichnet das Ende des Strings. + \pause + \item + Die Formatspezifikation\\ + entscheidet über die Ausgabe:\\[\smallskipamount] + \begin{tabular}{ll} + \lstinline|%d|\hspace*{0.5em}dezimal + & \lstinline|%c|\hspace*{0.5em}Zeichen\\ + \lstinline|%x|\hspace*{0.5em}hexadezimal +% \pause +% & \lstinline|%s|\hspace*{0.5em}String + \end{tabular} + \vspace*{-1cm} + \end{itemize} + \end{minipage} +\end{frame} + +\addtocounter{subsection}{-1} +\subsection{Arrays und Strings \protect\color{gray}und Zeichen} + +\begin{frame}[fragile] + \showsubsection + + \emph{"`Alles ist Zahl."'\/} -- Schule der Pythagoreer, 6.\ Jh.\ v.\,Chr. + + \medskip + + \begin{center} + \renewcommand{\arraystretch}{1.5} + \begin{tabular}{r} + \lstinline|"Hello"|\\ + \lstinline|'H'|\\ + \lstinline|'a' + 4| + \end{tabular} + \renewcommand{\arraystretch}{1.0} + \begin{tabular}{c} + ist nur eine andere\\ + Schreibweise für + \end{tabular} + \renewcommand{\arraystretch}{1.5} + \begin{tabular}{l} + \lstinline|{ 72, 101, 108, 108, 111, 0 }|\\ + \lstinline|72|\\ + \lstinline|'e'| + \end{tabular} + \renewcommand{\arraystretch}{1.0} + \end{center} + + \begin{itemize} + \item + Welchen Zahlenwert hat \lstinline{'*'} im Zeichensatz? + + \smallskip + + \begin{lstlisting}[gobble=8] + printf ("%d\n", '*'); + \end{lstlisting} + + \smallskip + + (normalerweise: ASCII) + \medskip + \item + Ist \lstinline{char ch} ein Großbuchstabe? + + \smallskip + + \begin{lstlisting}[gobble=8] + if (ch >= 'A' && ch <= 'Z') + ... + \end{lstlisting} + \medskip + \item + Groß- in Kleinbuchstaben umwandeln + + \smallskip + + \begin{lstlisting}[gobble=8] + ch += 'a' - 'A'; + \end{lstlisting} + \end{itemize} +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}} + \item[\textbf{2}] \textbf{Einführung in C} + \begin{itemize} + \item[2.1] Hello, world! + \item[2.2] Programme compilieren und ausführen + \item[2.3] Elementare Aus- und Eingabe + \item[2.4] Elementares Rechnen + \item[2.5] Verzweigungen + \item[2.6] Schleifen + \color{medgreen} + \item[2.7] Strukturierte Programmierung + \item[2.8] Seiteneffekte + \item[2.9] Funktionen + \item[2.10] Zeiger + \item[2.11] Arrays und Strings + \color{red} + \item[2.12] Strukturen + \item[\dots] + \end{itemize} + \color{gray} + \item[\textbf{3}] \textbf{Bibliotheken} + \vspace*{-\smallskipamount} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\end{document} diff --git a/20231012/hp-2023ws-p1.pdf b/20231012/hp-2023ws-p1.pdf new file mode 100644 index 0000000000000000000000000000000000000000..49415325a695265b380b9358f2c0ef1f3bcffe5e Binary files /dev/null and b/20231012/hp-2023ws-p1.pdf differ diff --git a/20231012/hp-2023ws-p1.tex b/20231012/hp-2023ws-p1.tex new file mode 100644 index 0000000000000000000000000000000000000000..0a651531289b555a6830aa49aabc85644bbddc7e --- /dev/null +++ b/20231012/hp-2023ws-p1.tex @@ -0,0 +1,204 @@ +% hp-2023ws-p1.pdf - Labor Notes on Low-Level Programming +% Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 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/>. + +% README: Versuch 1: RSA-Verschlüsselung + +\documentclass[a4paper]{article} + +\usepackage{pgscript} +\usepackage{multicol} +\usepackage{sfmath} + +\sloppy +\pagestyle{empty} +\addtolength{\textheight}{1cm} +\newcommand{\sep}{~$\cdot$~} +\newcommand{\mylicense}{CC BY-SA (Version 4.0) oder GNU GPL (Version 3 oder höher)} + +\begin{document} + + \makebox(0,0.005)[tl]{\includegraphics[scale=0.72]{logo-hochschule-bochum-cvh-text-v2.pdf}}\hfill + \makebox(0,0)[tr]{\includegraphics[scale=0.5]{logo-hochschule-bochum.pdf}} + \par\bigskip\bigskip + \begin{center} + \Large\textbf{Praktikumsversuch 1: RSA-Verschlüsselung} + \par\medskip + \normalsize Hardwarenahe Programmierung\sep + Wintersemester 2023/24\sep + Prof.~Dr.~Peter Gerwinski + \end{center} + + Aufgabe: Schreiben Sie ein Programm, das + die Verschlüsselung nach Rivest, Shamir und Adleman (RSA) + sowie die Schwierigkeiten beim Brechen ("`Knacken"') der + Verschlüsselung demonstriert. + + \begin{multicols}{2} + + Schreiben Sie ein C-Programm (oder mehrere), + das folgendes durchführt: + + \begin{itemize} + \item + \textbf{Schlüsselerzeugung} + + Bestimmen Sie drei verschiedene Primzahlen $p$, $q$ und $e$, wobei $e$ + kleiner als $(\kern0.5pt p-1)\cdot(q-1)$ und teilerfremd zu $(\kern0.5pt p-1)\cdot(q-1)$ sei. + (Dies ist z.\,B.\ der Fall, wenn $e$ größer als $p$ und $q$ ist.) + + Berechnen Sie $N = p \cdot q$ + sowie eine natürliche Zahl $d$ mit der Eigenschaft: + \begin{displaymath} + (e \cdot d) \,\%\, \bigl((\kern0.5pt p-1)\cdot(q-1)\bigr) = 1 + \end{displaymath} + ("`$x \,\%\, y\/$"' wird "`x modulo y"' gesprochen und steht + für den Rest, der bei Division von $x$ durch $y$ verbleibt.) + + $N$ und $e$ sind der \newterm{öffentliche Schlüssel}. \\ + $p$, $q$ und $d$ sind der \newterm{geheime Schlüssel}. + + \item + \textbf{Verschlüsselung} + + Wählen Sie eine geheime Botschaft $m$ eine Zahl kleiner als $N$, + die Sie verschlüsseln wollen. + + $m$ muß teilerfremd zu $N$ sein. + (Dies ist der Fall, wenn $m$ weder durch $p$ noch durch $q$ teilbar ist.) + + Schreiben Sie ein Programm, das aus $m$ die verschlüsselte + Nachricht $c$ berechnet: + \begin{displaymath} + c = m^e ~\%\, N + \end{displaymath} + + \textbf{Hinweis:} + \begin{displaymath} + m^e ~\%\, N + = \underbrace{(m \cdot m \cdot \dots \cdot m)}_{\mbox{$e$ Faktoren}} \,\%\, N + \hspace*{2cm} + \end{displaymath} + \vspace*{-\medskipamount} + \begin{displaymath} + = \underbrace{\Bigl(\dots\bigl((m \cdot m) \,\%\, N \cdot m\bigr) \,\%\, N \cdot \dots \cdot m\Bigr) \,\%\, N}_{\mbox{$e$ Faktoren}} + \end{displaymath} + + Dies bedeutet: Multiplizieren Sie die Zahl $m$ $e$-mal mit sich selbst, + wobei Sie \emph{nach jeder Multiplikation\/} modulo $N$ rechnen. + + \item + \textbf{Entschlüsselung} + + Rekonstruieren Sie aus der verschlüsselten Botschaft $c$ + wieder die geheime Botschaft $m$: + \begin{displaymath} + m = c^d ~\%\, N + \end{displaymath} + + \item + \textbf{Verschlüsselung brechen} + + Rekonstruieren Sie aus der verschlüsselten Botschaft $c$ + wieder die geheime Botschaft $m$, + \emph{ohne\/} den geheimen Schlüssel zu kennen, + d.\,h.\ Sie kennen nur $N$ und $e$, nicht jedoch $p$, $q$ und $d$. + + \textbf{Hinweis:} + Sie können z.\,B.\ versuchen, $N$ in seine Primfaktoren zu zerlegen. + Auf diese Weise können Sie zunächst $p$ und $q$ berechnen und danach $d$. + + \item + \textbf{Rechenzeit vergleichen} + + Vergleichen Sie nun + die für das Brechen der Verschlüsselung benötigte Rechenzeit + mit der Zeit, die das reguläre Ver- und Entschlüsseln dauert. + (Auf diesem Schwierigkeitsunterschied + beruht die Sicherheit der RSA-Verschlüsselung.) + + \textbf{Hinweis 1:} +% Wenn Sie ein Programm per Kommandozeile mit \lstinline[style=cmd]{time ./programm} starten, +% wird Ihnen angezeigt, wieviel Rechenzeit das Programm benötigt hat. +% Die drei angezeigten Werte stehen hierbei für die +% tatsächlich verstrichene Gesamtzeit (\lstinline[style=terminal]{real}), +% für die mit Rechnen verbrachte Zeit (\lstinline[style=terminal]{user}) +% und für die mit Systemaufrufen (z.\,B.\ Ein- und Ausgabe) +% verbrachte Zeit (\lstinline[style=terminal]{sys}). + Ein einfacher Weg, die von Ihrem Programm benötigte Rechenzeit zu messen, + ist die Verwendung der Funktion \lstinline{clock()}. + Diese gibt zurück, wieviel Rechenzeit seit Programmstart aufgewendet wurde. + Der Typ dieses Rückgabewerts ist ein ganzzahliger Typ, \lstinline{clock_t}, + mit dem man rechnen und den man mit \lstinline{%ld} ausgeben kann. + Pro Sekunde wächst der Zähler um \lstinline{CLOCKS_PER_SEC} Einheiten. + Typischerweise hat \lstinline{CLOCKS_PER_SEC} den Wert \lstinline{1000000} + oder \lstinline{1000}, die Zeiteinheit ist also eine Mikrosekunde + bzw.\ eine Millisekunde. + + \textbf{Hinweis 2:} + Die mit der o.\,a.\ Methode meßbaren Zeiten sind eigentlich zu ungenau, + um damit die sehr kurzen Rechenzeiten erfassen zu können -- + etwa so, als wollten Sie mit einem Lineal mit Millimetereinteilung + die Dicke eines Blatts Papier messen. Beides ist jedoch möglich. + +% \item +% Die \textbf{Abgabe} erfolgt auf einem zur Verfügung gestellten USB-Stick. +% Legen Sie dort ein Verzeichnis gemäß Ihrem Namen an, +% wie er in Ihrer Hochschul-E-Mail-Adresse auftaucht +% (z.\,B.\ \file{hans-joerg.mueller-mustermann} +% und speichern Sie darin Ihre \emph{Quelltexte} +% mit den Lösungen der Praktikumsaufgabe +% (und insbesondere \emph{nicht} die ausführbaren Dateien). +% Vermerken Sie bitte außerdem die folgenden Angaben +% entweder als Kommentar im Quelltext oder in einer separaten Textdatei: +% Name in korrekter Schreibweise, Matrikel-Nummer, +% Studiengang (MI/MP/TI) und Studienmodell (KIA/KIS/GS). + + \end{itemize} + + \end{multicols} + + \vspace*{-\bigskipamount} + + \strut\hfill\emph{Viel Erfolg!} + + \vfill + + \begingroup + + \small + + \setlength{\leftskip}{3cm} + + Stand: 11.\ Oktober 2023 + +% Soweit nicht anders angegeben:\\ + Copyright \copyright\ 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023\quad Peter Gerwinski\\ + Lizenz: \mylicense + + Sie können diese Praktikumsunterlagen einschließlich \LaTeX-Quelltext +%% und Beispielprogramme\\ + herunterladen unter:\\ + \url{https://gitlab.cvh-server.de/pgerwinski/hp} + + \endgroup + +\end{document} diff --git a/20231012/hp-musterloesung-20231012.pdf b/20231012/hp-musterloesung-20231012.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2b4ec76ac2c8bf4cff30935ba47871d1838cadd8 Binary files /dev/null and b/20231012/hp-musterloesung-20231012.pdf differ diff --git a/20231012/hp-musterloesung-20231012.tex b/20231012/hp-musterloesung-20231012.tex new file mode 100644 index 0000000000000000000000000000000000000000..b4c275a6c418b460b4199c04106b9adfa3695c01 --- /dev/null +++ b/20231012/hp-musterloesung-20231012.tex @@ -0,0 +1,375 @@ +% hp-musterloesung-20231012.pdf - Solutions to the Exercises on Low-Level Programming / Applied Computer Sciences +% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 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/>. + +% README: Schaltjahr ermitteln, Multiplikationstabelle, Fibonacci-Zahlen, fehlerhaftes Programm + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + + \section*{Hardwarenahe Programmierung\\ + Musterlösung zu den Übungsaufgaben -- 12.\ Oktober 2023} + + \exercise{Schaltjahr ermitteln} + + Schreiben Sie ein C-Programm, das eine Jahreszahl erfragt + und ausgibt, ob es sich um ein Schaltjahr handelt. + \begin{itemize} + \item Wenn die Jahreszahl durch 4 teilbar ist, ist das Jahr zunächst einmal ein Schaltjahr. + \item Ausnahme: Wenn die Jahreszahl durch 100 teilbar ist, ist das Jahr kein Schaltjahr. + \item Ausnahme von der Ausnahme: Wenn die Jahreszahl durch 400 teilbar ist,\\ + ist das Jahr doch wieder ein Schaltjahr. + \end{itemize} + + \solution + + Am einfachsten ist es, die Aufgabenstellung in geschachtelte + \lstinline{if}-Verzweigungen zu übersetzen. + Im folgenden finden Sie eine Funktion \lstinline{is_leap_year()}, + der man das Jahr übergibt und die für Schaltjahre \lstinline{1} + zurückgibt und für Nicht-Schaltjahre \lstinline{0}. + \begin{lstlisting} + #include <stdio.h> + + int is_leap_year (int year) + { + int leap_year = 0; + if (year % 4 == 0) + { + leap_year = 1; + if (year % 100 == 0) + { + leap_year = 0; + if (year % 400 == 0) + leap_year = 1; + } + } + return leap_year; + } + \end{lstlisting} + (In C steht \lstinline{0} für den Wahrheitswert "`falsch"' + und jeder Wert ungleich \lstinline{0} für den Wahrheitswert "`wahr'"; + die Zeile \lstinline{leap_year = 0} steht daher wörtlich und + selbsterklärend für "`ist kein Schaltjahr"'.) + + Unter Verwendung von \lstinline{else} läßt sich dies verkürzen zu: + \begin{lstlisting} + #include <stdio.h> + + int is_leap_year (int year) + { + if (year % 4 == 0) + { + if (year % 100 == 0) + { + if (year % 400 == 0) + return 1; + else + return 0; + } + else + return 1; + } + else + return 0; + } + \end{lstlisting} + + Eine andere Möglichkeit ist es, die Schaltjahr-Bedingung in eine + Kette von "`und"'- und "`oder"'-Verknüpfungen + (C-Operatoren \lstinline{&&} und \lstinline{||}) zu übersetzen: + \begin{lstlisting} + int is_leap_year (int year) + { + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + return 1; + else + return 0; + } + \end{lstlisting} + Dies ist zwar kürzer, aber nicht unbedingt übersichtlicher. + Der erzeugte Code ist übrigens \emph{nicht\/} kürzer und/oder + effizienter als bei der Verwendung mehrerer + \lstinline{if}-Verzweigungen. + Wir empfehlen, daß Sie immer so programmieren, + daß Sie selbst den maximalen Überblick über Ihr Programm behalten. + + \goodbreak + + Ein Hauptprogramm, das die o.\,a.\ Funktion aufruft, + könnte dann wie folgt aussehen: + \begin{lstlisting} + int main (void) + { + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (is_leap_year (year)) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; + } + \end{lstlisting} + In den Dateien \gitfile{hp}{2023ws/20231012}{loesung-1-1.c} bis \gitfile{hp}{2023ws/20231012}{loesung-1-3.c} + finden Sie lauffähige Programme, die die o.\,a.\ Funktionen aufrufen. + Beachten Sie, daß die Funktion \emph{vor\/} dem Hauptprogramm + deklariert werden muß, damit das Hauptprogramm sie kennt. + (Es gibt Tricks, mit denen es auch anders geht, + aber was hätten wir in diesem Zusammenhang davon?) + + In \gitfile{hp}{2023ws/20231012}{loesung-1-4.c} und \gitfile{hp}{2023ws/20231012}{loesung-1-5.c} + findet die Schaltjahr-Prüfung direkt im Hauptprogramm statt. + Dies ist ebenfalls eine richtige Lösung der Aufgabe, + schränkt aber die Wiederverwertbarkeit des Codes ein. + + Die Datei \gitfile{hp}{2023ws/20231012}{loesung-1-4.c} enthält darüberhinaus Codeverdopplungen, + nämlich mehrere identische \lstinline{printf()}-Auf"-rufe + an unterschiedlichen Stellen. + Dies ist schlechter Programmierstil ("`Cut-and-paste-Programmierung"'). + + Die besten Lösungen sind \gitfile{hp}{2023ws/20231012}{loesung-1-2.c} + und \gitfile{hp}{2023ws/20231012}{loesung-1-3.c}. + + \goodbreak + + Zum Testen:\vspace*{-\medskipamount} + \begin{itemize}\itemsep0pt + \item 1900 ist kein Schaltjahr. + \item 1902 ist kein Schaltjahr. + \item 1904 ist ein Schaltjahr. + \item 1996 ist ein Schaltjahr. + \item 1998 ist kein Schaltjahr. + \item 2000 ist ein Schaltjahr. + \item 2002 ist kein Schaltjahr. + \item 2004 ist ein Schaltjahr. + \item 2020 ist ein Schaltjahr. + \item 2021 ist kein Schaltjahr. + \item 2022 ist kein Schaltjahr. + \item 2023 ist kein Schaltjahr. + \end{itemize} + + \goodbreak + + Hier noch ein Hinweis für Unix-Shell-Experten: + \begin{lstlisting}[style=cmd] + for y in 1 2 3 4 5; do + clear + for x in 1900 1902 1904 1996 1998 2000 2002 2004 2020 2021 2022 2023; do + echo $x | ./loesung-1-$y + done + sleep 2s + done + \end{lstlisting} + + \exercise{Multiplikationstabelle} + + Geben Sie mit Hilfe einer Schleife ein "`Einmaleins"' aus.\\ + Dabei sollen die Faktoren und Ergebnisse rechtsbündig untereinander stehen: + \begin{lstlisting}[style=terminal] + 1 * 7 = 7 + 2 * 7 = 14 + ... + 10 * 7 = 70 + \end{lstlisting} + Hinweis: Verwenden Sie Formatspezifikationen wie z.\,B.\ \lstinline{%3d}\\ + (siehe dazu die Dokumentation zu \lstinline{printf()}, + z.\,B.\ \,\lstinline[style=cmd]{man 3 printf}\,) + + \solution + + Drei verschiedene richtige Lösungen finden Sie in den Dateien + \gitfile{hp}{2023ws/20231012}{loesung-2-1.c}, \gitfile{hp}{2023ws/20231012}{loesung-2-2.c} und \gitfile{hp}{2023ws/20231012}{loesung-2-3.c}. + (Zum Compilieren von \gitfile{hp}{2023ws/20231012}{loesung-2-2.c} und \gitfile{hp}{2023ws/20231012}{loesung-2-3.c} + ist mindestens der C99-Standard erforderlich; bitte nötigenfalls + in \file{gcc} die Option \lstinline[style=cmd]{-std=c99} mit angeben.) + + Die Lösung in \gitfile{hp}{2023ws/20231012}{loesung-2-3.c} ist zwar richtig, + aber unnötig kompliziert und daher nicht empfohlen. + + Eine \textbf{falsche} Lösung finden Sie in der Datei \gitfile{hp}{2023ws/20231012}{loesung-2-f4.c}: + In der Ausgabe dieses Programms stehen die Faktoren und Ergebnisse + nicht rechtsbündig untereinander. + + \exercise{Fibonacci-Zahlen} + + Die Folge der Fibonacci-Zahlen ist definiert durch: + \begin{quote} + 1.\ Zahl: 0\\ + 2.\ Zahl: 1\\ + nächste Zahl = Summe der beiden vorherigen + \end{quote} + Schreiben Sie ein Programm, das die ersten 50 Fibonacci-Zahlen ausgibt. + + Falls Ihnen dabei irgendwelche Besonderheiten auffallen + und/oder Sie irgendwelche besondere Maßnahmen treffen, + dokumentieren Sie diese. + + (Wem dies zu einfach ist, kann auch gerne + die ersten 100 Fibonacci-Zahlen ausgeben.) + + \solution + + Zwei verschiedene richtige Lösungen finden Sie in den Dateien + \gitfile{hp}{2023ws/20231012}{loesung-3-1.c} und \gitfile{hp}{2023ws/20231012}{loesung-3-2.c}. + + Die Lösung in \gitfile{hp}{2023ws/20231012}{loesung-3-2.c} + speichert alle berechneten Zahlen in einem Array, + die in \gitfile{hp}{2023ws/20231012}{loesung-3-1.c} hingegen + speichert immer nur maximal drei Zahlen gleichzeitig. + Sofern nicht alle berechneten Zahlen später noch benötigt werden, + ist daher \gitfile{hp}{2023ws/20231012}{loesung-3-1.c} zu bevorzugen. + + Wichtig in \gitfile{hp}{2023ws/20231012}{loesung-3-1.c} ist, daß \lstinline{f0 + f1} berechnet wird, + \emph{bevor\/} \lstinline{f0} oder \lstinline{f1} ein neuer Wert zugewiesen wird. + Dies ist nur möglich, weil das Programm + eine zusätzliche Variable (hier: \lstinline{f2}) verwendet. + + \goodbreak + + Eine "`Besonderheit"' besteht darin, daß das Ergebnis + ab der Fibonacci-Zahl Nr.~47 \textbf{falsch} ist: + \begin{lstlisting}[style=terminal] + f[45] = 1134903170 + f[46] = 1836311903 + f[47] = -1323752223 + f[48] = 512559680 + f[49] = -811192543 + \end{lstlisting} + Die Summe zweier positiver Zahlen darf keine negative Zahl sein. + + (Mit der Dokumentation dieser Beobachtung + ist die Aufgabenstellung bereits erfüllt.) + + Der Grund für diese fehlerhafte Rechnung + ist die begrenzte Rechengenauigkeit unserer Rechner, + in diesem Fall ein sogenannter \newterm{Integer-Überlauf}. + Details dazu sind Gegenstand von Kapitel~5 der Lehrveranstaltung. + + Eine "`besondere Maßnahme"' besteht darin, + anstelle des "`normalen"' Ganzzahl-Datentyps \lstinline{int} + "`lange"' ganze Zahlen (\lstinline{long int}) zu verwenden -- + siehe \gitfile{hp}{2023ws/20231012}{loesung-3-3.c}. + Damit nicht nur die Rechnung, sondern auch die Ausgabe funktioniert, + muß die Formatspezifikation in \lstinline{printf()} + von \lstinline{%d} zu \lstinline{%ld} angepaßt werden -- + siehe \gitfile{hp}{2023ws/20231012}{loesung-3-4.c}. + (Anstelle von \lstinline{long int} verwendet man üblicherweise + abkürzend \lstinline{long} -- siehe \gitfile{hp}{2023ws/20231012}{loesung-3-5.c}.) + + Wenn die genaue Rechengenauigkeit wichtig ist, sind die Definitionen + der Datentypen \lstinline{int} und \lstinline{long} zu unpräzise. + Für derartige Fälle gibt es spezielle Datentypen, + z.\,B.\ \lstinline{uint64_t} für vorzeichenlose 64-Bit-Ganzzahlen. + Für die Ausgabe derartiger Datentypen sind spezielle Formatspezifikationen + erforderlich. Die Beispiel-Lösungen \gitfile{hp}{2023ws/20231012}{loesung-3-5.c} + und \gitfile{hp}{2023ws/20231012}{loesung-3-6.c} funktionieren zwar, + zeigen aber \emph{noch nicht\/} die bestmögliche Lösung. + + \textbf{Achtung:} Die Verwendung von Fließkommazahlen + für ganzzahlige Berechnungen wie bei den Fibonacci-Zahlen + ist \textbf{nicht} zielführend! + Die in \gitfile{hp}{2023ws/20231012}{loesung-3-7f.c} + bis \gitfile{hp}{2023ws/20231012}{loesung-3-12f.c} vorgestellte "`Lösung"' + ist \textbf{falsch}. + + \bigskip + + \exercise{Fehlerhaftes Programm} + + \begin{minipage}[t]{0.65\textwidth} + Wir betrachten das nebenstehende C-Programm + (Datei: \gitfile{hp}{2023ws/20231012}{aufgabe-4.c}). + \begin{itemize} + \item[(a)] + Was bewirkt dieses Programm? Begründen Sie Ihre Antwort. + + Schreiben Sie Ihre Begründung so auf, + daß man sie auch dann versteht, + wenn man gerade nicht die Möglichkeit hat, + bei Ihnen persönlich nachzufragen + (z.\,B.\ weil man gerade eine Klausur korrigiert). + + Die Schwierigkeit dieser Aufgabe besteht + nicht allein darin, die Problematik zu verstehen, + sondern auch darin, dieses Verständnis für andere aufzuschreiben. + \item[(b)] + Ändern Sie das Programm so um, + daß es einen "`Countdown"' von 10 bis 0 ausgibt. + \end{itemize} + \end{minipage}\hfill + \begin{minipage}[t]{0.3\textwidth} + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + for (int i = 10; i = 0; i - 1) + printf ("%d\n", i); + return 0; + } + \end{lstlisting} + \end{minipage} + + \solution + + \begin{itemize} + \item[(a)] + \textbf{Was bewirkt dieses Programm? Begründen Sie Ihre Antwort.} + + Dieses Programm bewirkt nichts. + Die \lstinline{for}-Schleife wird nicht ausgeführt. + + Begründung: Die \lstinline{for}-Bedingung ist eine Zuweisung + des Werts \lstinline{0} an die Variable \lstinline{i}. + Neben dem Seiteneffekt der Zuweisung liefert der Ausdruck + einen Wert zurück, nämlich den zugewiesenen Wert + \lstinline{0}. Dieser wird von \lstinline{for} als eine + Bedingung mit dem konstanten Wert "`falsch"' interpretiert. + + (Hinweis: Ohne diese Begründung ist die Aufgabe nur zu einem + kleinen Teil gelöst.) + + Darüberhinaus ist die Zähl-Anwendung unwirksam: Sie berechnet + den Wert \lstinline{i - 1} und vergißt ihn wieder, ohne ihn + einer Variablen (z.\,B.\ \lstinline{i}) zuzuweisen. + + \goodbreak + \item[(b)] + \textbf{Ändern Sie das Programm so, daß es einen "`Countdown"' von 10 bis 0 ausgibt.} + + Datei \gitfile{hp}{2023ws/20231012}{loesung-4.c}: + \begin{lstlisting}[gobble=8] + #include <stdio.h> + + int main (void) + { + for (int i = 10; i >= 0; i--) + printf ("%d\n", i); + return 0; + } + \end{lstlisting} + \end{itemize} + +\end{document} diff --git a/20231012/hp-uebung-20231012.pdf b/20231012/hp-uebung-20231012.pdf new file mode 100644 index 0000000000000000000000000000000000000000..08178850120cc19a3e30334de31ee4885421c376 Binary files /dev/null and b/20231012/hp-uebung-20231012.pdf differ diff --git a/20231012/hp-uebung-20231012.tex b/20231012/hp-uebung-20231012.tex new file mode 100644 index 0000000000000000000000000000000000000000..83ee0020aa1dc83cdf1cd115007bf27e9c422d6c --- /dev/null +++ b/20231012/hp-uebung-20231012.tex @@ -0,0 +1,114 @@ +% hp-uebung-20231012.pdf - Exercises on Low-Level Programming / Applied Computer Sciences +% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 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/>. + +% README: Schaltjahr ermitteln, Multiplikationstabelle, Fibonacci-Zahlen, fehlerhaftes Programm + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + + \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 12.\ Oktober 2023} + + \exercise{Schaltjahr ermitteln} + + Schreiben Sie ein C-Programm, das eine Jahreszahl erfragt + und ausgibt, ob es sich um ein Schaltjahr handelt. + \begin{itemize} + \item Wenn die Jahreszahl durch 4 teilbar ist, ist das Jahr zunächst einmal ein Schaltjahr. + \item Ausnahme: Wenn die Jahreszahl durch 100 teilbar ist, ist das Jahr kein Schaltjahr. + \item Ausnahme von der Ausnahme: Wenn die Jahreszahl durch 400 teilbar ist,\\ + ist das Jahr doch wieder ein Schaltjahr. + \end{itemize} + + \exercise{Multiplikationstabelle} + + Geben Sie mit Hilfe einer Schleife ein "`Einmaleins"' aus.\\ + Dabei sollen die Faktoren und Ergebnisse rechtsbündig untereinander stehen: + \begin{lstlisting}[style=terminal] + 1 * 7 = 7 + 2 * 7 = 14 + ... + 10 * 7 = 70 + \end{lstlisting} + \textbf{Hinweis:} Verwenden Sie Formatspezifikationen wie z.\,B.\ \lstinline{%3d}\\ + (siehe dazu die Dokumentation zu \lstinline{printf()}, + z.\,B.\ \,\lstinline[style=cmd]{man 3 printf}\,) + + \exercise{Fibonacci-Zahlen} + + Die Folge der Fibonacci-Zahlen ist definiert durch: + \begin{quote} + 1.\ Zahl: 0\\ + 2.\ Zahl: 1\\ + nächste Zahl = Summe der beiden vorherigen + \end{quote} + Schreiben Sie ein Programm, das die ersten 50 Fibonacci-Zahlen ausgibt. + + Falls Ihnen dabei irgendwelche Besonderheiten auffallen + und/oder Sie irgendwelche besondere Maßnahmen treffen, + dokumentieren Sie diese. + + (Wem dies zu einfach ist, kann auch gerne + die ersten 100 Fibonacci-Zahlen ausgeben.) + + \exercise{Fehlerhaftes Programm} + + \begin{minipage}[t]{0.65\textwidth} + Wir betrachten das nebenstehende C-Programm + (Datei: \gitfile{hp}{2023ws/20231012}{aufgabe-4.c}). + \begin{itemize} + \item[(a)] + Was bewirkt dieses Programm? Begründen Sie Ihre Antwort. + + Schreiben Sie Ihre Begründung so auf, + daß man sie auch dann versteht, + wenn man gerade nicht die Möglichkeit hat, + bei Ihnen persönlich nachzufragen + (z.\,B.\ weil man gerade eine Klausur korrigiert). + + Die Schwierigkeit dieser Aufgabe besteht + nicht allein darin, die Problematik zu verstehen, + sondern auch darin, dieses Verständnis für andere aufzuschreiben. + \item[(b)] + Ändern Sie das Programm so um, + daß es einen "`Countdown"' von 10 bis 0 ausgibt. + \end{itemize} + \end{minipage}\hfill + \begin{minipage}[t]{0.3\textwidth} + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + int main (void) + { + for (int i = 10; i = 0; i - 1) + printf ("%d\n", i); + return 0; + } + \end{lstlisting} + \end{minipage} + +\end{document} diff --git a/20231012/loesung-1-1.c b/20231012/loesung-1-1.c new file mode 100644 index 0000000000000000000000000000000000000000..ea41b8ea35bf871389c8be15779a58c293c81049 --- /dev/null +++ b/20231012/loesung-1-1.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +int is_leap_year (int year) +{ + int leap_year = 0; + if (year % 4 == 0) + { + leap_year = 1; + if (year % 100 == 0) + { + leap_year = 0; + if (year % 400 == 0) + leap_year = 1; + } + } + return leap_year; +} + +int main (void) +{ + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (is_leap_year (year)) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; +} diff --git a/20231012/loesung-1-2.c b/20231012/loesung-1-2.c new file mode 100644 index 0000000000000000000000000000000000000000..d9e4df8a36238875e0b46398b21f93e7f4f98792 --- /dev/null +++ b/20231012/loesung-1-2.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +int is_leap_year (int year) +{ + if (year % 4 == 0) + { + if (year % 100 == 0) + { + if (year % 400 == 0) + return 1; + else + return 0; + } + else + return 1; + } + else + return 0; +} + +int main (void) +{ + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (is_leap_year (year)) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; +} diff --git a/20231012/loesung-1-3.c b/20231012/loesung-1-3.c new file mode 100644 index 0000000000000000000000000000000000000000..97051b3bb5e6620ff771d6b3f5949687130abea8 --- /dev/null +++ b/20231012/loesung-1-3.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +int is_leap_year (int year) +{ + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + return 1; + else + return 0; +} + +int main (void) +{ + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (is_leap_year (year)) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; +} diff --git a/20231012/loesung-1-4.c b/20231012/loesung-1-4.c new file mode 100644 index 0000000000000000000000000000000000000000..3a0d218e6c03f326ad73bad55ad3e29ea882eb2d --- /dev/null +++ b/20231012/loesung-1-4.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int main (void) +{ + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (year % 4 == 0) + { + if (year % 100 == 0) + { + if (year % 400 == 0) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + } + else + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + } + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; +} diff --git a/20231012/loesung-1-5.c b/20231012/loesung-1-5.c new file mode 100644 index 0000000000000000000000000000000000000000..999d9f2030e29f1c961b12d647d2e82592b637bf --- /dev/null +++ b/20231012/loesung-1-5.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main (void) +{ + int year; + printf ("Bitte geben Sie eine Jahreszahl ein: "); + scanf ("%d", &year); + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + printf ("Das Jahr %d ist ein Schaltjahr.\n", year); + else + printf ("Das Jahr %d ist kein Schaltjahr.\n", year); + return 0; +} diff --git a/20231012/loesung-2-1.c b/20231012/loesung-2-1.c new file mode 100644 index 0000000000000000000000000000000000000000..5ec9dd3caf6639d22bf770ef4b6bb779a3d7c714 --- /dev/null +++ b/20231012/loesung-2-1.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main (void) +{ + int a = 1; + int b = 7; + while (a <= 10) + { + printf ("%2d * %d = %2d\n", a, b, a * b); + a++; + } + return 0; +} diff --git a/20231012/loesung-2-1a.c b/20231012/loesung-2-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..8acad25cdce91f7f1dec5e2d8607a08f709cff2a --- /dev/null +++ b/20231012/loesung-2-1a.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main (void) +{ + int a = 1; + int b = 7; + while (a <= 10) + { + printf ("%*d * %d = %*d\n", a, 2, b, a * b, 2); + a++; + } + return 0; +} diff --git a/20231012/loesung-2-1b.c b/20231012/loesung-2-1b.c new file mode 100644 index 0000000000000000000000000000000000000000..b739818db5ea8ecb13a18a1700bce9d9d4f85140 --- /dev/null +++ b/20231012/loesung-2-1b.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main (void) +{ + int a = 1; + int b = 7; + while (a <= 10) + { + printf ("%*d * %d = %*d\n", 2, a, b, 2, a * b); + a++; + } + return 0; +} diff --git a/20231012/loesung-2-1c.c b/20231012/loesung-2-1c.c new file mode 100644 index 0000000000000000000000000000000000000000..f09dc57abf6efa82ab7918590126e721e1c7dd4d --- /dev/null +++ b/20231012/loesung-2-1c.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +int main (void) +{ + int a = 1; + int b = 7; + char format[42]; + sprintf (format, "%%%dd * %%d = %%%dd\n", 2, 2); + printf ("format = %s\n", format); + while (a <= 10) + { + printf (format, a, b, a * b); + a++; + } + return 0; +} diff --git a/20231012/loesung-2-1d.c b/20231012/loesung-2-1d.c new file mode 100644 index 0000000000000000000000000000000000000000..8d4aa51b4ad32fd32bb2a62e31255f089bd3991e --- /dev/null +++ b/20231012/loesung-2-1d.c @@ -0,0 +1,17 @@ +#include <stdio.h> + +int main (void) +{ + int a = 1; + int b = 7; + char format[42]; + snprintf (format, 42, "%%%dd * %%d = %%%dd\n", 2, 2); + format[41] = 0; + printf ("format = %s\n", format); + while (a <= 10) + { + printf (format, a, b, a * b); + a++; + } + return 0; +} diff --git a/20231012/loesung-2-2.c b/20231012/loesung-2-2.c new file mode 100644 index 0000000000000000000000000000000000000000..8f9319ee596a52f38531a2cefb376b54e7ec3ec0 --- /dev/null +++ b/20231012/loesung-2-2.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main (void) +{ + int x = 7; + for (int i = 1; i <= 10; i++) + printf ("%2d *%2d =%3d\n", i, x, i * x); + return 0; +} diff --git a/20231012/loesung-2-3.c b/20231012/loesung-2-3.c new file mode 100644 index 0000000000000000000000000000000000000000..71fca2538b991397ac5f046a33ff0f9130b2980f --- /dev/null +++ b/20231012/loesung-2-3.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +int main (void) +{ + int x = 7; + for (int i = 1; i <= 10; i++) + { + if (i >= 10) + printf ("%d", i); + else + printf (" %d", i); + printf (" * %d = ", x); + int y = i * x; + if (y >= 10) + printf ("%d", y); + else + printf (" %d", y); + printf ("\n"); + } + return 0; +} diff --git a/20231012/loesung-2-f4.c b/20231012/loesung-2-f4.c new file mode 100644 index 0000000000000000000000000000000000000000..8520d438f654856a74c22ffd01b9c5815741efbc --- /dev/null +++ b/20231012/loesung-2-f4.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main (void) +{ + int x = 7; + for (int i = 1; i <= 10; i++) + printf ("%d * %d = %d\n", i, x, i * x); + return 0; +} diff --git a/20231012/loesung-3-1.c b/20231012/loesung-3-1.c new file mode 100644 index 0000000000000000000000000000000000000000..ec5f4d9f93985577246eefccdd0f6003403795ab --- /dev/null +++ b/20231012/loesung-3-1.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + int f0 = 0; + int f1 = 1; + for (int i = 0; i < 50; i++) + { + printf ("f[%d] = %d\n", i, f0); + int f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-10f.c b/20231012/loesung-3-10f.c new file mode 100644 index 0000000000000000000000000000000000000000..7bb464e8490084edbe8f388e1e6cf2bfd0b5c4be --- /dev/null +++ b/20231012/loesung-3-10f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 200; i++) + { + printf ("f[%d] = %.0Lf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-11f.c b/20231012/loesung-3-11f.c new file mode 100644 index 0000000000000000000000000000000000000000..def1256c1aaa78d07234bf73be485077e5e68270 --- /dev/null +++ b/20231012/loesung-3-11f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 200; i++) + { + printf ("f[%d] = %30.0Lf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-12f.c b/20231012/loesung-3-12f.c new file mode 100644 index 0000000000000000000000000000000000000000..7ca898622fd72bac0cf074b947f0e0f8810d3689 --- /dev/null +++ b/20231012/loesung-3-12f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 200; i++) + { + printf ("f[%3d] = %60.0Lf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-2.c b/20231012/loesung-3-2.c new file mode 100644 index 0000000000000000000000000000000000000000..7043f66e6b73894be9547a5562de8cad43cf0e71 --- /dev/null +++ b/20231012/loesung-3-2.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int main (void) +{ + int f[50]; + f[0] = 0; + f[1] = 1; + for (int i = 2; i < 50; i++) + f[i] = f[i - 2] + f[i - 1]; + for (int i = 0; i < 50; i++) + printf ("f[%d] = %d\n", i, f[i]); + return 0; +} diff --git a/20231012/loesung-3-3.c b/20231012/loesung-3-3.c new file mode 100644 index 0000000000000000000000000000000000000000..c27f4717a14c244d609c4c2d86a21c1026679d3e --- /dev/null +++ b/20231012/loesung-3-3.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long int f0 = 0; + long int f1 = 1; + for (int i = 0; i < 50; i++) + { + printf ("f[%d] = %d\n", i, f0); + long int f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-4.c b/20231012/loesung-3-4.c new file mode 100644 index 0000000000000000000000000000000000000000..c3c0dbe62bffce9adfc49cadb1c747e2e932c04e --- /dev/null +++ b/20231012/loesung-3-4.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long int f0 = 0; + long int f1 = 1; + for (int i = 0; i < 50; i++) + { + printf ("f[%d] = %ld\n", i, f0); + long int f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-5.c b/20231012/loesung-3-5.c new file mode 100644 index 0000000000000000000000000000000000000000..119125542cf5fdca1289cf5918428f1634c4f2ee --- /dev/null +++ b/20231012/loesung-3-5.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long f0 = 0; + long f1 = 1; + for (int i = 0; i < 50; i++) + { + printf ("f[%d] = %ld\n", i, f0); + long f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-6.c b/20231012/loesung-3-6.c new file mode 100644 index 0000000000000000000000000000000000000000..339217e92fb44da2e667dc15c5bbd839b9f579c2 --- /dev/null +++ b/20231012/loesung-3-6.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +int main (void) +{ + uint64_t f0 = 0; + uint64_t f1 = 1; + for (int i = 0; i < 100; i++) + { + printf ("f[%d] = %lu\n", i, f0); + uint64_t f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-6a.c b/20231012/loesung-3-6a.c new file mode 100644 index 0000000000000000000000000000000000000000..685ba9744a1fe3104459a75c9f534887b8ad4cd9 --- /dev/null +++ b/20231012/loesung-3-6a.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +int main (void) +{ + uint128_t f0 = 0; + uint128_t f1 = 1; + for (int i = 0; i < 100; i++) + { + printf ("f[%d] = %lu\n", i, f0); + uint128_t f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-7f.c b/20231012/loesung-3-7f.c new file mode 100644 index 0000000000000000000000000000000000000000..11a9e200a07e1e7c368b014f659f3c5f55196ff3 --- /dev/null +++ b/20231012/loesung-3-7f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 100; i++) + { + printf ("f[%d] = %llf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-8f.c b/20231012/loesung-3-8f.c new file mode 100644 index 0000000000000000000000000000000000000000..d6aff68d21c4aa754fa94a434f5bb17235d6535e --- /dev/null +++ b/20231012/loesung-3-8f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 100; i++) + { + printf ("f[%d] = %Lf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-3-9f.c b/20231012/loesung-3-9f.c new file mode 100644 index 0000000000000000000000000000000000000000..e1d95dc56e72da5a203548ff0907c6c128004a28 --- /dev/null +++ b/20231012/loesung-3-9f.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main (void) +{ + long double f0 = 0; + long double f1 = 1; + for (int i = 0; i < 100; i++) + { + printf ("f[%d] = %.0Lf\n", i, f0); + long double f2 = f0 + f1; + f0 = f1; + f1 = f2; + } + return 0; +} diff --git a/20231012/loesung-4.c b/20231012/loesung-4.c new file mode 100644 index 0000000000000000000000000000000000000000..f8481e994c02ac8e581244713756c9e9be7c7fd6 --- /dev/null +++ b/20231012/loesung-4.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main (void) +{ + for (int i = 10; i >= 0; i--) + printf ("%d\n", i); + return 0; +} diff --git a/20231012/logo-hochschule-bochum-cvh-text-v2.pdf b/20231012/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20231012/logo-hochschule-bochum-cvh-text-v2.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum-cvh-text-v2.pdf \ No newline at end of file diff --git a/20231012/logo-hochschule-bochum.pdf b/20231012/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20231012/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20231012/pgscript.sty b/20231012/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20231012/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/20231012/pgslides.sty b/20231012/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20231012/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file diff --git a/README.md b/README.md index b1eee3dbc2a731dea96f2a312c6e91912b9d32e1..2ace90665843143326fe32d0d9e0df95a38a549a 100644 --- a/README.md +++ b/README.md @@ -17,20 +17,22 @@ Für Details siehe [common/README](https://gitlab.cvh-server.de/pgerwinski/hp/ra Vortragsfolien und Beispiele: ----------------------------- - * [05.10.2023: Einführung, Einführung in C (bis Seiteneffekte)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231005/hp-20231005.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/2023ws/20231005/) + * [05.10.2023: Einführung, Einführung in C (bis 2.7: Strukturierte Programmierung)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231005/hp-20231005.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/2023ws/20231005/) + * [12.10.2023: Einführung in C (ab 2.8: Seiteneffekte)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231012/hp-20231012.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/2023ws/20231012/) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/hp-slides-2023ws.pdf) Übungsaufgaben: --------------- -(kommen noch) + * [05.10.2023: Hello-World-Programme, Schaltjahr ermitteln, Maximum berechnen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231005/hp-uebung-20231005.pdf) + * [12.10.2023: Schaltjahr ermitteln, Multiplikationstabelle, Fibonacci-Zahlen, fehlerhaftes Programm](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231012/hp-uebung-20231012.pdf) Musterlösungen: --------------- -(kommen noch) + * [12.10.2023: Schaltjahr ermitteln, Multiplikationstabelle, Fibonacci-Zahlen, fehlerhaftes Programm](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231012/hp-musterloesung-20231012.pdf) Praktikumsunterlagen: --------------------- -(kommen noch) + * [Versuch 1: RSA-Verschlüsselung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/2023ws/20231012/hp-2023ws-p1.pdf) Alte Klausuren: --------------- diff --git a/hp-slides-2023ws.pdf b/hp-slides-2023ws.pdf index 8f8a0d808345b3f03d9fe6505e28bde6ec3eb42c..4d5db7364d2cc82f44196bd6b647a252f14b182c 100644 Binary files a/hp-slides-2023ws.pdf and b/hp-slides-2023ws.pdf differ diff --git a/hp-slides-2023ws.tex b/hp-slides-2023ws.tex index bb92eb9cc664dbbfdf1fb0bf85789c086062882e..884af13404add23b3074b799135a477c7e4c6317 100644 --- a/hp-slides-2023ws.tex +++ b/hp-slides-2023ws.tex @@ -11,6 +11,8 @@ \includepdf[pages=1]{script/hp-slides-title-2023ws.pdf} \pdfbookmark[1]{Wichtiger Hinweis}{Hinweis} \includepdf[pages=2-]{script/hp-slides-title-2023ws.pdf} - \pdfbookmark[1]{05.10.2023: Einführung, Einführung in C (bis Seiteneffekte)}{20231005} + \pdfbookmark[1]{05.10.2023: Einführung, Einführung in C (bis 2.7: Strukturierte Programmierung)}{20231005} \includepdf[pages=-]{20231005/hp-20231005.pdf} + \pdfbookmark[1]{12.10.2023: Einführung in C (ab 2.8: Seiteneffekte)}{20231012} + \includepdf[pages=-]{20231012/hp-20231012.pdf} \end{document}