Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • pgerwinski/ainf
  • bwildenhain/ainf
2 results
Select Git revision
  • master
1 result
Show changes

Commits on Source 15

Showing
with 1428 additions and 0 deletions
20160107/portb.jpg

477 KiB

../common/portb.jpg
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
__attribute__ ((noreturn)) void fatal_error (char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (1);
}
int main (void)
{
fatal_error ("error not found");
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main (int argc, char **argv)
{
FILE *dev_random = fopen ("/dev/random", "rb");
if (!dev_random)
{
fprintf (stderr, "%s: cannot open /dev/random\n", argv[0]);
exit (1);
}
else
{
uint16_t x;
fread (&x, sizeof (x), 1, dev_random);
printf ("%d\n", x);
fclose (dev_random);
}
return 0;
}
20160114/20160114_001.jpg

896 KiB

20160114/20160114_002.jpg

889 KiB

File added
This diff is collapsed.
File added
% ainf-uebung-20160114.pdf - Exercises on Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{enumerate}
\usepackage{ifthen}
\usepackage{gensymb}
\newcounter{exercise}
\newcommand{\exercise}[1]{\addtocounter{exercise}{1}\subsection*{Aufgabe \arabic{exercise}: #1}}
\newcounter{points}
\newcommand{\points}[1]{\ifthenelse{#1=1}{(1 Punkt)}{(#1 Punkte)}\addtocounter{points}{#1}}
\newcommand{\ItwoC}{I\raisebox{0.5ex}{\footnotesize 2}C}
\newcommand{\ITWOC}{I\raisebox{0.5ex}{\normalsize 2}C}
\begin{document}
\thispagestyle{empty}
\section*{Angewandte Informatik\\Übungsaufgaben -- 14.\ Januar 2016}
\exercise{Trickprogrammierung}
Wir betrachten das folgende Programm \file{hallo.c}:
\begin{lstlisting}
#include <stdio.h>
#include <stdint.h>
int main (void)
{
uint64_t x = 4262939000843297096;
char *s = &x;
printf ("%s\n", s);
return 0;
}
\end{lstlisting}
Das Programm wird compiliert und auf einem 64-Bit-Little-Endian-Computer ausgeführt:
\begin{lstlisting}[style=terminal]
$ ¡gcc -Wall -O hallo.c -o hallo¿
hallo.c: In function `main':
hallo.c:7:13: warning: initialization from incompatible pointer type [...]
$ ¡./hallo¿
Hallo
\end{lstlisting}
\begin{itemize}
\item[(a)]
Erklären Sie die Warnung beim Compilieren. \points{2}
\item[(b)]
Erklären Sie die Ausgabe des Programms. \points{4}
\item[(c)]
Wie würde die Ausgabe des Programms auf einem 64-Bit-Big-Endian-Computer lauten? \points{5}\\
Hinweis: Es kann hilfreich sein, für die Umrechnung ein Programm zu schreiben.
\end{itemize}
\exercise{Daten im Speicher}
Das folgende C-Programm \file{test.c} gibt den Speicherbereich,
in dem sich seine Variablen befinden,
als eine Folge von 8-Bit-Zahlen aus:
\begin{lstlisting}
#include <stdio.h>
#include <stdint.h>
int16_t a = -1;
int32_t b = 8320;
int main (void)
{
uint8_t *p = &a;
for (int i = 0; i < 8; i++)
printf (" %d", p[i]);
printf ("\n");
}
\end{lstlisting}
Das Programm wird auf einem 32-Bit-Rechner ohne Optimierung compiliert (mit Warnung)
und ge\-star\-tet:
\begin{lstlisting}[style=terminal]
$ ¡gcc -Wall -O0 -std=c99 test.c -o test¿
test.c: In function `main':
test.c:9:16: warning: initialization from incompatible pointer type [...]
$ ¡./test¿
255 255 0 0 128 32 0 0
\end{lstlisting}
\begin{itemize}
\item[(a)]
Erklären Sie die ausgegebenen Zahlen.
\points{4}
\item[(b)]
Wie würde die Ausgabe auf einem 8-Bit-Rechner lauten und warum?
\points{3}
\end{itemize}
\clearpage
\exercise{Thermometer-Baustein an \ITWOC-Bus}
Eine Firma stellt einen elektronischen Thermometer-Baustein her,
den man über die serielle Schnittstelle (RS-232) an einen PC anschließen kann,
um die Temperatur auszulesen.
Nun wird eine Variante des Thermo"-meter-Bausteins entwickelt,
die die Temperatur zusätzlich über einen \ItwoC-Bus bereitstellt.
Um das neue Thermometer zu testen, wird es in ein Gefäß mit heißem Wasser gelegt,
das langsam auf Zimmertemperatur abkühlt.
Alle 10 Minuten liest ein Programm, das auf dem PC läuft,
die gemessene Temperatur über beide Schnittstellen aus
und erzeugt daraus die folgende Tabelle:
\begin{center}
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{|c|c|c|}\hline
Zeit /\,min. & Temperatur per RS-232 /\,\degree C & Temperatur per \ItwoC\ /\,\degree C \\\hline\hline
\phantom{0}0 & 94 & 122 \\\hline
10 & 47 & 244 \\\hline
20 & 30 & 120 \\\hline
30 & 24 & \phantom{0}24 \\\hline
40 & 21 & 168 \\\hline
\end{tabular}
\end{center}
\begin{itemize}
\item[(a)]
Aus dem Vergleich der Meßdaten läßt sich
auf einen Fehler bei der \ItwoC-Übertragung schließen.\\
Um welchen Fehler handelt es sich,
und wie ergibt sich dies aus den Meßdaten?
\points{5}
\item[(b)]
Schreiben Sie eine C-Funktion \lstinline{uint8_t repair (uint8_t data)},
die eine über den \ItwoC-Bus empfangene fehlerhafte Temperatur \lstinline{data} korrigiert.
\points{5}
\end{itemize}
\end{document}
#include<stdio.h>
#include<stdlib.h>
char *array = NULL;
int size;
int numberOfBits = 0;
void bit_array_init(int n){
numberOfBits = n;
if(n%8==0){
size = n/8;
}else{
size = (n/8) + 1;
}
array = malloc(size * sizeof(char));
for(int i=0;i<size;i++){
array[i] = 0;
}
}
void bit_array_set(int i, int value){
if((value==0||value==1)&&(i<numberOfBits)&&(i>=0)){
int index = i/8;
int bit = i%8;
char potenz = 1;
/*for(int i=0;i<bit;i++){
potenz*=2;
}*/
potenz = 1<<bit;
if(value){
array[index] |= potenz;
}else{
array[index] &= ~potenz;
}
}else{
printf("\nUngültige Werte\n");
}
}
int bit_array_get(int i){
if((i<numberOfBits)&&(i>=0)){
int index = i/8;
int bit = i%8;
int help = array[index] >> bit;
if((help%2)==0){ //Binärzahlen mit 0 als letztes Bit sind durch 2 teilbar!
return 0;
}else{
return 1;
}
}else{
printf("nö");
}
}
void bit_array_flip(int i){
if(bit_array_get(i)){
bit_array_set(i,0);
}else{
bit_array_set(i,1);
}
}
void bit_array_done(void){
free(array);
}
void bit_array_resize(int new_n){
int oldSize = size;
char *help = malloc(size*sizeof(char));
for(int i=0;i<size;i++){
help[i] = array[i];
}
bit_array_done();
bit_array_init(new_n);
if(size<=oldSize){
for(int i=0;i<size;i++){
array[i] = help[i];
}
}else{
for(int i=0;i<oldSize;i++){
array[i] = help[i];
}
}
free(help);
}
void print_array(){
printf("\n");
for(int i=0;i<numberOfBits;i++){
printf("%d ",bit_array_get(i));
}
}
int main(void){
int bits = 10;
bit_array_init(bits);
bit_array_set(7,1);
bit_array_set(0,1);
bit_array_flip(6);
print_array();
//bit_array_flip(7);
print_array();
bit_array_resize(12);
print_array();
bit_array_done();
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <stdint.h>
int main (void)
{
uint64_t x = 4262939000843297096;
char *s = &x;
printf ("%s\n", s);
return 0;
}
File added
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { &i, &s };
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", object[i]->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", object[i]->content);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", (t_integer *) object[i]->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", (t_string *) object[i]->content);
return 0;
}
#include <stdio.h>
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
int main (void)
{
t_integer i = { 1, 42 };
t_string s = { 2, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s };
for (int i = 0; i < 2; i++)
if (object[i]->type == 1)
printf ("Integer: %d\n", ((t_integer *) object[i])->content);
else if (object[i]->type == 2)
printf ("String: \"%s\"\n", ((t_string *) object[i])->content);
return 0;
}