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

Übungsaufgaben 12.12.2019

parent 61eb3519
No related branches found
No related tags found
No related merge requests found
#include <gtk/gtk.h>
#define WIDTH 320
#define HEIGHT 240
double t = 0.0;
double dt = 0.2;
int r = 5;
double x = 10;
double y = 200;
double vx = 20;
double vy = -60;
double g = 9.81;
gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data)
{
GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 };
gdk_cairo_set_source_rgba (c, &blue);
cairo_arc (c, x, y, r, 0, 2 * G_PI);
cairo_fill (c);
return FALSE;
}
gboolean timer (GtkWidget *widget)
{
t += dt;
x += vx * dt;
y += vy * dt;
vx = vx;
vy = 0.5 * g * (t * t);
if (y + r >= HEIGHT)
vy = -vy * 0.9;
if (x + r >= WIDTH)
vx = -vx * 0.9;
if (x - r <= 0)
vx = -vx * 0.9;
gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT);
g_timeout_add (50, (GSourceFunc) timer, widget);
return FALSE;
}
int main (int argc, char **argv)
{
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new ();
gtk_widget_show (drawing_area);
gtk_container_add (GTK_CONTAINER (window), drawing_area);
gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT);
gtk_main ();
return 0;
}
File added
% hp-uebung-20191212.pdf - Exercises on Low-Level Programming
% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019 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: Kondensator, hüpfender Ball
\documentclass[a4paper]{article}
\usepackage{pgscript}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung\\
Übungsaufgaben -- 12.\ Dezember 2019}
Diese Übung enthält Punkteangaben wie in einer Klausur.
Um zu "`bestehen"', müssen Sie innerhalb von 70 Minuten
unter Verwendung ausschließlich zugelassener Hilfsmittel
12 Punkte (von insgesamt \totalpoints) erreichen.
\exercise{Kondensator}
Ein Kondensator der Kapazität $C = 100\,\mu{\rm F}$
ist auf die Spannung $U_0 = 5\,{\rm V}$ aufgeladen
und wird über einen Widerstand $R = 33\,{\rm k}\Omega$ entladen.
\begin{enumerate}[(a)]
\item
Schreiben Sie ein C-Programm, das
den zeitlichen Spannungsverlauf in einer Tabelle darstellt.
\points{5}
\item
Schreiben Sie ein C-Programm, das ermittelt,
wie lange es dauert, bis die Spannung unter $0.1\,{\rm V}$ gefallen ist.
\points{4}
\item
Vergleichen Sie die berechneten Werte mit der exakten theoretischen Entladekurve:
\begin{math}
U(t) = U_0 \cdot e^{-\frac{t}{RC}}
\end{math}\\
\points{3}
\end{enumerate}
Hinweise:
\begin{itemize}
\item
Für die Simulation zerlegen wir den Entladevorgang in kurze Zeitintervalle $dt$.
Innerhalb jedes Zeitintervalls betrachten wir den Strom $I$ als konstant
und berechnen, wieviel Ladung $Q$ innerhalb des Zeitintervalls
aus dem Kondensator herausfließt.
Aus der neuen Ladung berechnen wir die Spannung am Ende des Zeitintervalls.
\item
Für den Vergleich mit der exakten theoretischen Entladekurve
benötigen Sie die Exponentialfunktion \lstinline{exp()}.
Diese finden Sie in der Mathematik-Bibliothek:
\lstinline{#include <math.h>} im Quelltext,
beim \lstinline[style=cmd]{gcc}-Aufruf \lstinline[style=cmd]{-lm} mit angeben.
\item
$Q = C \cdot U$,\quad $U = R \cdot I$,\quad $I = \frac{dQ}{dt}$
\end{itemize}
\exercise{Fehlerhaftes Programm: Hüpfender Ball}
Das auf der nächsten Seite abgedruckte GTK+-Programm
(Datei: \gitfile{hp}{20191212}{aufgabe-2.c}) soll einen
hüpfenden Ball darstellen, ist jedoch fehlerhaft.
\begin{enumerate}[\quad(a)]
\item
Warum sieht man lediglich ein leeres Fenster?
Welchen Befehl muß man ergänzen, um diesen Fehler zu beheben?
\points{3}
\item
Nach der Fehlerbehebung in Aufgabenteil (a)
zeigt das Programm einen unbeweglichen Ball.
Welchen Befehl muß man ergänzen, um diesen Fehler zu beheben, und warum?
\points{2}
\item
Erklären Sie das merkwürdige Hüpfverhalten des Balls.
Wie kommt es zustande?
Was an diesem Verhalten ist korrekt, und was ist fehlerhaft? \points{5}
\item
Welche Befehle muß man in welcher Weise ändern,
um ein realistischeres Hüpf-Verhalten zu bekommen? \points{2}
\end{enumerate}
Hinweis: Das Hinzuziehen von Beispiel-Programmen aus der Vorlesung
ist ausdrücklich erlaubt -- auch in der Klausur.
Allgemeiner Hinweis:
Wenn Sie die Übungsaufgaben zu dieser Lehrveranstaltung
als PDF-Datei betrachten und darin auf die Dateinamen klicken,
können Sie die Beispiel-Programme direkt herunterladen.
Dadurch vermeiden Sie Übertragungsfehler.
\bigskip
\begin{flushright}
\textit{Viel Erfolg!}
\end{flushright}
\clearpage
\vbox to \textheight{\vspace*{-0.5cm}\begin{lstlisting}
#include <gtk/gtk.h>
#define WIDTH 320
#define HEIGHT 240
double t = 0.0;
double dt = 0.2;
int r = 5;
double x = 10;
double y = 200;
double vx = 20;
double vy = -60;
double g = 9.81;
gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data)
{
GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 };
gdk_cairo_set_source_rgba (c, &blue);
cairo_arc (c, x, y, r, 0, 2 * G_PI);
cairo_fill (c);
return FALSE;
}
gboolean timer (GtkWidget *widget)
{
t += dt;
x += vx * dt;
y += vy * dt;
vx = vx;
vy = 0.5 * g * (t * t);
if (y + r >= HEIGHT)
vy = -vy * 0.9;
if (x + r >= WIDTH)
vx = -vx * 0.9;
if (x - r <= 0)
vx = -vx * 0.9;
gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT);
g_timeout_add (50, (GSourceFunc) timer, widget);
return FALSE;
}
int main (int argc, char **argv)
{
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new ();
gtk_widget_show (drawing_area);
gtk_container_add (GTK_CONTAINER (window), drawing_area);
gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT);
gtk_main ();
return 0;
}
\end{lstlisting}\vss}
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}}
\makeatother
\end{document}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment