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

Vorbereitung 20.11.2017

parent 268d4dad
No related branches found
No related tags found
No related merge requests found
Showing
with 1607 additions and 0 deletions
#include <string.h>
int fun_1 (char *s)
{
int x = 0;
for (int i = 0; i < strlen (s); i++)
x += s[i];
return x;
}
int fun_2 (char *s)
{
int i = 0, x = 0;
int len = strlen (s);
while (i < len)
x += s[i++];
return x;
}
File added
This diff is collapsed.
File added
% hp-uebung-20171120.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/>.
% README: Datum-Bibliothek, Primzahlen, rotierender Würfel
\documentclass[a4paper]{article}
\usepackage{pgscript}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 20.\ November 2017}
\exercise{Zahlensysteme}
Wandeln Sie ohne Hilfsmittel
\begin{minipage}[t]{0.3\textwidth}
\begin{itemize}
\item
nach Dezimal:
\begin{itemize}
\item[(a)]
0010\,0000$_2$
\item[(b)]
42$_{16}$
\item[(c)]
17$_8$
\end{itemize}
\end{itemize}
\end{minipage}\hfill
\begin{minipage}[t]{0.3\textwidth}
\begin{itemize}
\item
nach Hexadezimal:
\begin{itemize}
\item[(d)]
0010\,0000$_2$
\item[(e)]
42$_{10}$
\item[(f)]
192.168.20.254$_{256}$
\end{itemize}
\end{itemize}
\end{minipage}\hfill
\begin{minipage}[t]{0.3\textwidth}
\begin{itemize}
\item
nach Binär:
\begin{itemize}
\item[(g)]
750$_8$
\item[(h)]
42$_{10}$
\item[(i)]
AFFE$_{16}$
\end{itemize}
\end{itemize}
\end{minipage}
\medskip
Berechnen Sie ohne Hilfsmittel:
\begin{itemize}
\item[(j)]
750$_8$ \& 666$_8$
\item[(k)]
A380$_{16}$ + B747$_{16}$
\item[(l)]
AFFE$_{16} >> 1$
\end{itemize}
(Die tiefgestellte Zahl steht für die Basis des Zahlensystems.)
\exercise{Mikro-Controller}
\begin{minipage}[t]{10cm}
An die vier Ports eines ATmega16-Mikro-Controllers sind Leuchtdioden angeschlossen:
\begin{itemize}
\item
von links nach rechts an die Ports A, B, C und D,
\item
von oben nach unten an die Bits Nr.\ 0 bis 7.
\end{itemize}
Wir betrachten das folgende Programm:
\begin{lstlisting}[gobble=6]
#include <avr/io.h>
int main (void)
{
DDRA = 0xff;
DDRB = 0xff;
DDRC = 0xff;
DDRD = 0xff;
PORTA = 0x1f;
PORTB = 0x10;
PORTD = 0x10;
PORTC = 0xfc;
while (1);
return 0;
}
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{3cm}
\strut\\[-\baselineskip]
\includegraphics[width=3cm]{leds.jpg}
\end{minipage}
\begin{itemize}
\item[(a)]
Was bewirkt dieses Programm? % (4 Punkte)
\item[(b)]
Wozu dienen die ersten vier Zeilen des Hauptprogramms? % (2 Punkte)
\item[(c)]
Was würde stattdessen die Zeile \lstinline{DDRA, DDRB, DDRC, DDRD = 0xff;} bewirken? % (2 Punkte)
\item[(d)]
Schreiben Sie das Programm so um,
daß die durch das Programm dargestellte Figur spiegelverkehrt erscheint. % (3 Punkte)
\item[(e)]
Wozu dient das \lstinline{while (1)}? % (2 Punkte)
\end{itemize}
\exercise{Länge von Strings}
Strings werden in der Programmiersprache C durch Zeiger auf \lstinline{char}-Variable realisiert.
Beispiel: \lstinline{char *hello_world = "Hello, world!\n"}
Die Systembibliothek stellt eine Funktion \lstinline{strlen()} zur Ermittlung der Länge von Strings\\
zur Verfügung (\lstinline{#include <string.h>}).
\begin{itemize}
\item[(a)]
Auf welche Weise ist die Länge eines Strings gekennzeichnet?
% \points 1
\item[(b)]
Wie lang ist die Beispiel-String-Konstante \lstinline{"Hello, world!\n"},
und wieviel Speicherplatz belegt sie?
% \points 2
\item[(c)]
Schreiben Sie eine eigene Funktion \lstinline{int strlen (char *s)},
die die Länge eines Strings zurückgibt.
% \points 3
\end{itemize}
Wir betrachten nun die folgenden Funktionen (Datei: \gitfile{hp}{20171120}{aufgabe-3.c}):
\begin{center}
\begin{minipage}{8cm}
\begin{lstlisting}[gobble=8]
int fun_1 (char *s)
{
int x = 0;
for (int i = 0; i < strlen (s); i++)
x += s[i];
return x;
}
\end{lstlisting}
\end{minipage}%
\begin{minipage}{6cm}
\vspace*{-1cm}
\begin{lstlisting}[gobble=8]
int fun_2 (char *s)
{
int i = 0, x = 0;
int len = strlen (s);
while (i < len)
x += s[i++];
return x;
}
\end{lstlisting}
\vspace*{-1cm}
\end{minipage}
\end{center}
\begin{itemize}
\item[(d)]
Was bewirken die beiden Funktionen?
% \points 2
\item[(e)]
% Von welcher Ordnung (Landau-Symbol) sind die beiden Funktionen
% hinsichtlich der Anzahl ihrer Zugriffe auf die Zeichen im String -- und warum?
% Sie dürfen für \lstinline{strlen()} Ihre eigene Version der Funktion voraussetzen.
% \points 3
% \item[(f)]
Schreiben Sie eine eigene Funktion,
die dieselbe Aufgabe erledigt wie \lstinline{fun_2()},\\
nur effizienter.
% \points 4
\end{itemize}
\end{document}
../common/io-ports-and-interrupts.pdf
\ No newline at end of file
../common/leds.jpg
\ No newline at end of file
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
#if defined(__APPLE__) || defined(MACOSX)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include "opengl-magic.h"
void init_opengl (int *argcp, char **argv, char *window_name)
{
glutInit (argcp, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (1024, 768);
glutCreateWindow (window_name);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (20.0, -1.33333, 3.0, 7.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
static GLfloat light0_position[] = { 1.0, 0.0, 1.0, 0.0 };
glLightfv (GL_LIGHT0, GL_POSITION, light0_position);
}
void set_material_color (float r, float g, float b)
{
GLfloat color[] = { r, g, b };
glMaterialfv (GL_FRONT, GL_AMBIENT, color);
glMaterialfv (GL_FRONT, GL_DIFFUSE, color);
}
#if defined(__APPLE__) || defined(MACOSX)
void glutSolidCylinder (double radius, double height, int slices, int stacks)
{
GLUquadricObj *q = gluNewQuadric ();
gluCylinder (q, radius, radius, height, slices, stacks);
gluDeleteQuadric (q);
}
#endif
#ifndef OPENGL_MAGIC_H
#define OPENGL_MAGIC_H
extern void init_opengl (int *argcp, char **argv, char *window_name);
extern void set_material_color (float r, float g, float b);
#ifdef __MACOSX__
extern void glutSolidCylinder (double radius, double height, int slices, int stacks);
#endif
#endif /* OPENGL_MAGIC_H */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * sin (phi);
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 1.0 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi = phi0;
double omega = omega0;
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (1.0, 1.0, 0.8);
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, l, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glFlush ();
glutSwapBuffers ();
}
void timer_handler (int value)
{
t += dt;
phi += dt * omega;
omega += dt * (-g / l) * phi;
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutMainLoop ();
return 0;
}
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#include "opengl-magic.h"
#define phi0 0.5 /* geg. Anfangswert */
#define omega0 0.0 /* geg. Anfangswert */
#define dt 0.05
#define g 9.81
#define l 1.0
double t = 0.0;
double phi_with_sin = phi0;
double omega_with_sin= omega0;
double phi_without_sin = phi0;
double omega_without_sin= omega0;
void draw_pendulum (double phi)
{
glPushMatrix ();
glRotatef (90, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.75);
glRotatef (180.0 + phi / M_PI * 180.0, 0.0, 1.0, 0.0);
glTranslatef (0.0, 0.0, -1.0);
glutSolidCylinder (0.01, 1.0, 13, 1);
glutSolidSphere (0.1, 31, 10);
glPopMatrix ();
glutSwapBuffers ();
}
void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
set_material_color (0.8, 0.8, 1.0);
draw_pendulum (phi_with_sin);
set_material_color (1.0, 0.8, 0.8);
draw_pendulum (phi_without_sin);
set_material_color (0.8, 1.0, 0.8);
double A = phi0;
double B = 0.5 * M_PI; /* 90° */
double phi_analytic = A * sin (sqrt (g / l) * t + B);
draw_pendulum (phi_analytic);
glFlush ();
}
void timer_handler (int value)
{
t += dt;
phi_with_sin += dt * omega_with_sin; /* geg. Differentialgleichung */
omega_with_sin += - dt * g / l * sin (phi_with_sin); /* geg. Differentialgleichung */
/* _ohne_ Kleinwinkelnäherung */
phi_without_sin += dt * omega_without_sin; /* geg. Differentialgleichung */
omega_without_sin += - dt * g / l * phi_without_sin; /* geg. Differentialgleichung */
/* _mit_ Kleinwinkelnäherung */
glutPostRedisplay ();
glutTimerFunc (50, timer_handler, 0);
}
int main (int argc, char **argv)
{
init_opengl (&argc, argv, "Pendulum");
glutDisplayFunc (draw);
glutTimerFunc (50, timer_handler, 0);
glutPostRedisplay ();
glutMainLoop ();
return 0;
}
../common/pendulum.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
../20171106/photo-20171106-170748.jpg
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <png.h>
#include "textured-spheres.h"
#ifndef __USE_POSIX
extern int fileno (FILE *stream);
#endif
static png_byte *load_png_file (char *filename, unsigned *w, unsigned *h)
{
FILE *f = fopen (filename, "rb");
if (!f)
return NULL;
/* is it a PNG file? */
png_byte buf[8];
if (fread (buf, 1, 8, f) != 8)
{
fclose (f);
return NULL;
}
if (!png_check_sig (buf, 8))
{
fclose (f);
return NULL;
}
png_struct *png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
{
fclose (f);
return NULL;
}
png_info *info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
fclose (f);
png_destroy_read_struct (&png_ptr, NULL, NULL);
return NULL;
}
if (setjmp (png_jmpbuf (png_ptr)))
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_init_io (png_ptr, f);
png_set_sig_bytes (png_ptr, 8);
png_read_info (png_ptr, info_ptr);
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
/* tell libpng to strip 16 bit/color files down to 8 bits/color */
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
/* expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand (png_ptr);
/* expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand (png_ptr);
/* expand paletted or RGB images with transparency to full alpha channels
so the data will be available as RGBA quartets. */
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand (png_ptr);
/* transform grayscale images into rgb */
if (color_type == PNG_COLOR_TYPE_GRAY
|| color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha (png_ptr);
png_read_update_info (png_ptr, info_ptr);
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
&color_type, NULL, NULL, NULL);
if (color_type != PNG_COLOR_TYPE_RGB
&& color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_uint_32 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
if (row_bytes & 0x01)
row_bytes++;
png_byte *png_pixels = malloc (row_bytes * height * sizeof (png_byte));
if (png_pixels == NULL)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return NULL;
}
png_byte **row_pointers = malloc (height * sizeof (png_bytep));
if (row_pointers == NULL)
{
fclose (f);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
free (png_pixels);
png_pixels = NULL;
return NULL;
}
int i;
for (i = 0; i < height; i++)
row_pointers[i] = png_pixels + i * row_bytes;
png_read_image (png_ptr, row_pointers);
png_read_end (png_ptr, info_ptr);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fclose (f);
if (w)
*w = width;
if (h)
*h = height;
free (row_pointers);
return png_pixels;
}
void init_texture (char *image_filename, GLuint *texture)
{
unsigned image_width, image_height;
png_byte *image = load_png_file (image_filename, &image_width, &image_height);
if (!image)
{
fprintf (stderr, "textured-spheres.c: cannot open texture file \"%s\"",
image_filename);
exit (1);
}
glGenTextures (1, texture);
glBindTexture (GL_TEXTURE_2D, *texture);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, image);
}
void draw_textured_sphere (GLuint texture, GLdouble radius, GLint slices, GLint stacks)
{
static GLfloat white_color[] = { 1.0, 1.0, 1.0 };
glMaterialfv (GL_FRONT, GL_AMBIENT, white_color);
glMaterialfv (GL_FRONT, GL_DIFFUSE, white_color);
glBindTexture (GL_TEXTURE_2D, texture);
glEnable (GL_TEXTURE_2D);
GLUquadric *sphere = gluNewQuadric ();
gluQuadricTexture (sphere, GL_TRUE);
gluSphere (sphere, radius, slices, stacks);
glDisable (GL_TEXTURE_2D);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment