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

Vorbereitung und Übungsaufgaben 23.10.2017

parent a46a684f
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -571,6 +571,7 @@
\color{red}
\item[2.8] Seiteneffekte
\item[2.9] Funktionen
\color{black}
\item[2.10] Zeiger
\item[2.11] Arrays und Strings
\item[2.12] Strukturen
......@@ -878,336 +879,6 @@
\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}
\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)}
\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";
int i = 0;
while (hello_world[i])
printf ("%c", 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}
\vspace{-1.7cm}\hspace{5cm}%
\begin{minipage}{7cm}
\begin{itemize}
\pause
\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}
\subsection{Strukturen}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
int main (void)
{
date today = { 30, 10, 2014 };
printf ("%d.%d.%d\n", today.day, today.month, today.year);
return 0;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\showsubsection
\vspace*{0.9mm}
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
void set_date (date *d)
{
(*d).day = 30;
(*d).month = 10;
(*d).year = 2014;
}¿
\end{lstlisting}
\end{minipage}%
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡int main (void)
{
date today;
set_date (&today);
printf ("%d.%d.%d\n", today.day,
today.month, today.year);
return 0;
}¿
\end{lstlisting}
\end{minipage}
\end{frame}
\begin{frame}[fragile]
\showsubsection
\vspace*{0.9mm}
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
void set_date (date *d)
{
d->day = 30;
d->month = 10;
d->year = 2014;
}¿
\end{lstlisting}
\end{minipage}%
\begin{minipage}[b]{6cm}
\hspace*{-1cm}%
\lstinline{foo->bar}
ist Abkürzung für
\lstinline{(*foo).bar}
\vspace{2cm}
\begin{lstlisting}[gobble=6]
¡int main (void)
{
date today;
set_date (&today);
printf ("%d.%d.%d\n", today.day,
today.month, today.year);
return 0;
}¿
\end{lstlisting}
\end{minipage}
\end{frame}
\nosectionnonumber{\inserttitle}
\begin{frame}
......@@ -1228,10 +899,10 @@
\color{medgreen}
\item[2.8] Seiteneffekte
\item[2.9] Funktionen
\color{red}
\item[2.10] Zeiger
\item[2.11] Arrays und Strings
\item[2.12] Strukturen
\color{red}
\item[2.13] Dateien und Fehlerbehandlung
\item[2.14] Parameter des Hauptprogramms
\item[2.15] String-Operationen
......
File added
This diff is collapsed.
File added
% hp-uebung-20171023.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016, 2017 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[a4paper]{article}
\usepackage{pgscript}
\begin{document}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 23.\ Oktober 2017}
\exercise{ROT13-Verschlüsselung}
Schreiben Sie ein C-Programm, das einen Text entgegennimmt,
jeden Buchstaben von A bis Z zyklisch um 13 Stellen verschiebt
und den auf diese Weise "`verschlüsselten"' Text wieder ausgibt.
Umlaute, Ziffern, Satz- und Sonderzeichen sollen nicht verändert werden.
\begin{tabular}{ccc}
A & $\longrightarrow$ & N \\
B & $\longrightarrow$ & O \\
& \dots & \\
M & $\longrightarrow$ & Z \\
N & $\longrightarrow$ & A \\
& \dots & \\
Y & $\longrightarrow$ & L \\
Z & $\longrightarrow$ & M
\end{tabular}
\qquad
\begin{tabular}{ccc}
a & $\longrightarrow$ & n \\
b & $\longrightarrow$ & o \\
& \dots & \\
m & $\longrightarrow$ & z \\
n & $\longrightarrow$ & a \\
& \dots & \\
y & $\longrightarrow$ & l \\
z & $\longrightarrow$ & m
\end{tabular}
Beispiel: Aus "`Apfelkuchen"' wird "`Ncsryxhpura"'.
\exercise{Programm analysieren}
Wir betrachten das folgende C-Programm (Datei: \gitfile{20161024}{aufgabe-2.c}):
\begin{lstlisting}
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
\end{lstlisting}
\vspace{-\medskipamount}
\begin{itemize}
\item[(a)]
Was bewirkt dieses Programm?
\item[(b)]
Wofür stehen die Zahlen?
\item[(c)]
Ergänzen Sie das Programm derart, daß seine \lstinline{main()}-Funktion
\lstinline{int main (void)} lautet und eine \lstinline{return}-Anweisung hat,
wobei die in Aufgabenteil (a) festgestellte Eigenschaft erhalten bleiben soll.
\end{itemize}
\exercise{Kalender-Berechnung}
Am 3.\,1.\,2009 meldete \emph{heise online\/}:
\begin{quote}
Kunden des ersten mobilen Media-Players von Microsoft
erlebten zum Jahresende eine böse Überraschung:
Am 31.\ Dezember 2008 fielen weltweit alle Zune-Geräte der ersten Generation aus.
Ursache war ein interner Fehler bei der Handhabung von Schaltjahren.
\strut\hfill\url{http://heise.de/-193332},
\end{quote}
Der Artikel verweist auf ein Quelltextfragment (Datei: \gitfile{20161024}{aufgabe-3.c}),
das für einen gegebenen Wert \lstinline{days}
das Jahr und den Tag innerhalb des Jahres
für den \lstinline{days}-ten Tag nach dem 1.\,1.\,1980 berechnen soll:
\begin{lstlisting}
year = ORIGINYEAR; /* = 1980 */
while (days > 365)
{
if (IsLeapYear (year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
\end{lstlisting}
\vspace*{-1cm}
\goodbreak
Dieses Quelltextfragment weist mehrere Code-Verdopplungen auf:
\begin{itemize}
\item
Die Anweisung \lstinline{year += 1} taucht an zwei Stellen auf.
\item
Es gibt zwei unabhängige Abfragen \lstinline{days > 365} und \lstinline{days > 366}:\\
eine in einer \lstinline{while}- und die andere in einer \lstinline{if}-Bedingung.
\item
Die Länge eines Jahres wird nicht durch eine Funktion berechnet oder in einer Variablen gespeichert;
stattdessen werden an mehreren Stellen die expliziten numerischen Konstanten 365 und 366 verwendet.
\end{itemize}
Diese Probleme führten am 31.\ Dezember 2008 zu einer Endlosschleife,
die sich -- z.\,B.\ durch eine Funktion \lstinline{DaysInYear()} -- leicht hätte vermeiden lassen.
Gut hingegen ist die Verwendung einer Konstanten \lstinline{ORIGINYEAR}
anstelle der Zahl 1980
sowie die Kapselung der Berechnung der Schaltjahr-Bedingung
in einer Funktion \lstinline{IsLeapYear()}.
\begin{itemize}
\item[(a)]
Erklären Sie das Zustandekommen der Endlosschleife.
\item[(b)]
Schreiben Sie das Quelltextfragment so um, daß es die beschriebenen Probleme
nicht mehr enthält.
\end{itemize}
\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
../common/pgscript.sty
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment