From 3b583218039b4da2279ea7da7542366637d109ad Mon Sep 17 00:00:00 2001
From: Peter Gerwinski <peter.gerwinski@hs-bochum.de>
Date: Tue, 28 Jan 2020 15:46:15 +0100
Subject: [PATCH] Klausurtutorium 28.1.2020

---
 20200128/aufgabe-4.xbm           |   6 ++++
 20200128/hp-klausur-20190129.txt |  58 +++++++++++++++++++++++++++++++
 20200128/loesung-1e-0.c          |  38 ++++++++++++++++++++
 20200128/loesung-1e-1.c          |  38 ++++++++++++++++++++
 20200128/loesung-1e-2.c          |  39 +++++++++++++++++++++
 20200128/loesung-1e-3.c          |  38 ++++++++++++++++++++
 20200128/loesung-1e-4.c          |  30 ++++++++++++++++
 20200128/loesung-1e-5.c          |  30 ++++++++++++++++
 20200128/loesung-1g-0.c          |  48 +++++++++++++++++++++++++
 20200128/loesung-1g-1.c          |  52 +++++++++++++++++++++++++++
 20200128/loesung-3-0.c           |  31 +++++++++++++++++
 20200128/loesung-3-1.c           |  31 +++++++++++++++++
 20200128/loesung-4-0.c           |  15 ++++++++
 20200128/loesung-4-1.c           |  15 ++++++++
 20200128/loesung-4-2.c           |  17 +++++++++
 20200128/loesung-4-3.c           |  23 ++++++++++++
 20200128/loesung-4-4.c           |  23 ++++++++++++
 20200128/test.pbm                | Bin 0 -> 37 bytes
 18 files changed, 532 insertions(+)
 create mode 100644 20200128/aufgabe-4.xbm
 create mode 100644 20200128/hp-klausur-20190129.txt
 create mode 100644 20200128/loesung-1e-0.c
 create mode 100644 20200128/loesung-1e-1.c
 create mode 100644 20200128/loesung-1e-2.c
 create mode 100644 20200128/loesung-1e-3.c
 create mode 100644 20200128/loesung-1e-4.c
 create mode 100644 20200128/loesung-1e-5.c
 create mode 100755 20200128/loesung-1g-0.c
 create mode 100755 20200128/loesung-1g-1.c
 create mode 100644 20200128/loesung-3-0.c
 create mode 100644 20200128/loesung-3-1.c
 create mode 100644 20200128/loesung-4-0.c
 create mode 100644 20200128/loesung-4-1.c
 create mode 100644 20200128/loesung-4-2.c
 create mode 100644 20200128/loesung-4-3.c
 create mode 100644 20200128/loesung-4-4.c
 create mode 100644 20200128/test.pbm

diff --git a/20200128/aufgabe-4.xbm b/20200128/aufgabe-4.xbm
new file mode 100644
index 0000000..cf5404b
--- /dev/null
+++ b/20200128/aufgabe-4.xbm
@@ -0,0 +1,6 @@
+#define aufgabe_4_width 14
+#define aufgabe_4_height 14
+static unsigned char aufgabe_4_bits[] = {
+   0x00, 0x00, 0xf0, 0x03, 0x08, 0x04, 0x04, 0x08, 0x02, 0x10, 0x32, 0x13,
+   0x22, 0x12, 0x02, 0x10, 0x0a, 0x14, 0x12, 0x12, 0xe4, 0x09, 0x08, 0x04,
+   0xf0, 0x03, 0x00, 0x00 };
diff --git a/20200128/hp-klausur-20190129.txt b/20200128/hp-klausur-20190129.txt
new file mode 100644
index 0000000..29bcadb
--- /dev/null
+++ b/20200128/hp-klausur-20190129.txt
@@ -0,0 +1,58 @@
+Inhalte, 27.01.2020, 12:46:35
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ - Seiteneffekte
+ - Arrays, Zeiger, Speicherzellen
+ - Bit-Manipulation, Endianness, Alignment
+ - Algorithmen, Landau-Symbol
+ - Objekte
+
+Klausurtutorium, 28.01.2020, 13:10:52
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ - Klausur von 2018ws durchgehen
+
+  zu Aufgabe 1b:
+
+    for (int i = len; i >= pos; i--)
+      target[i + 1] = target[i];
+
+    Diese Lösung ist korrekt, weil auch das Null-Symbol verschoben wird.
+
+ - darin: Übung vom 14.11.2019: Fehler in der Musterlösung
+ - darin: XBM, PBM
+
+ * Musterlösung zu Aufgabe 2:
+   Wintersemester 2018/19, 7.1.2019
+   https://gitlab.cvh-server.de/pgerwinski/hp/blob/2018ws/20190107/hp-musterloesung-20190107.pdf
+
+:) char buffer[100] = "kürzerer Text";
+   --> Null-Symbol am Ende, danach undefiniert
+   char buffer[100] = { 'T', 'e', 's', 't' };
+   --> kein Null-Symbol am Ende, danach ebenfalls undefiniert
+:) Wenn man in einen String etwas einfügt und Buchstaben verschiebt,
+   muß man das Null-Symbol mit verschieben.
+:) 32-Bit-Variable als 64-Bit-Variable ausgeben
+   --> 2. Hälfte ist zufällig
+
+:) Quicksort: im schlechtesten Fall O(n²)
+   Um vorher die Länge des Arrays herauszufinden, braucht man eine Schleife.
+   --> O(n² + n) = O(n²)
+
+:) Unsinnige Parameterwerte von Funktionen abfangen:
+   Nur dann erforderlich, wenn es in der Aufgabenstellung steht.
+
+
+@@@@@ Verlängerungskabel/Mehrfachstecker aus dem Labor holen!
+
+@@ Problem mit Rechner lösen
+
+Test des Klausur-WLAN, 28.01.2020, 15:14:23
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ESSID = klausur2
+Passwort = klausurklausur
+
+URL = https://klausur.cvh-server.de
+
+Benutzername = 00
+Passwort = gIghbg4mJAAV
+
+ZIP-Dateien herunter- und hochladen
diff --git a/20200128/loesung-1e-0.c b/20200128/loesung-1e-0.c
new file mode 100644
index 0000000..8d46b68
--- /dev/null
+++ b/20200128/loesung-1e-0.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  if (len == 0)
+    {
+      target[0] = src;
+      return 0;
+    }
+  for (int i = 0; i < len; i++)
+    if (target[i] >= src)
+      {
+        insert_into_string (src, target, i);
+        return 0;
+      }
+  insert_into_string (src, target, len);
+}
+
+int main (void)
+{
+  char test[100] = "";
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1e-1.c b/20200128/loesung-1e-1.c
new file mode 100644
index 0000000..2a16752
--- /dev/null
+++ b/20200128/loesung-1e-1.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  if (len == 0)
+    {
+      target[0] = src;
+      return;
+    }
+  for (int i = 0; i < len; i++)
+    if (target[i] >= src)
+      {
+        insert_into_string (src, target, i);
+        return;
+      }
+  insert_into_string (src, target, len);
+}
+
+int main (void)
+{
+  char test[100] = "";
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1e-2.c b/20200128/loesung-1e-2.c
new file mode 100644
index 0000000..6d37362
--- /dev/null
+++ b/20200128/loesung-1e-2.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  if (len == 0)
+    {
+      target[0] = src;
+      target[1] = 0;
+      return;
+    }
+  for (int i = 0; i < len; i++)
+    if (target[i] >= src)
+      {
+        insert_into_string (src, target, i);
+        return;
+      }
+  insert_into_string (src, target, len);
+}
+
+int main (void)
+{
+  char test[100] = "";
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1e-3.c b/20200128/loesung-1e-3.c
new file mode 100644
index 0000000..896ea03
--- /dev/null
+++ b/20200128/loesung-1e-3.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  if (len == 0)
+    {
+      insert_into_string (src, target, 0);
+      return;
+    }
+  for (int i = 0; i < len; i++)
+    if (target[i] >= src)
+      {
+        insert_into_string (src, target, i);
+        return;
+      }
+  insert_into_string (src, target, len);
+}
+
+int main (void)
+{
+  char test[100] = "";  /* Initialisierung wichtig, damit das Null-Symbol vorhanden ist */
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1e-4.c b/20200128/loesung-1e-4.c
new file mode 100644
index 0000000..08aebb0
--- /dev/null
+++ b/20200128/loesung-1e-4.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  int pos = 0;
+  while (target[pos] >= src && pos < len)
+    pos++;
+  insert_into_string (src, target, pos);
+}
+
+int main (void)
+{
+  char test[100] = "";  /* Initialisierung wichtig, damit das Null-Symbol vorhanden ist */
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1e-5.c b/20200128/loesung-1e-5.c
new file mode 100644
index 0000000..5668ee2
--- /dev/null
+++ b/20200128/loesung-1e-5.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <string.h>
+
+void insert_into_string (char src, char *target, int pos)
+{
+  int len = strlen (target);
+  for (int i = len; i >= pos; i--)
+    target[i + 1] = target[i];
+  target[pos] = src;
+}
+
+void insert_into_string_sorted (char src, char *target)
+{
+  int len = strlen (target);
+  int pos = 0;
+  while (target[pos] <= src && pos < len)
+    pos++;
+  insert_into_string (src, target, pos);
+}
+
+int main (void)
+{
+  char test[100] = "";  /* Initialisierung wichtig, damit das Null-Symbol vorhanden ist */
+  insert_into_string_sorted ('c', test);
+  insert_into_string_sorted ('a', test);
+  insert_into_string_sorted ('d', test);
+  insert_into_string_sorted ('b', test);
+  printf ("test = \"%s\"\n", test);
+  return 0;
+}
diff --git a/20200128/loesung-1g-0.c b/20200128/loesung-1g-0.c
new file mode 100755
index 0000000..8276d4a
--- /dev/null
+++ b/20200128/loesung-1g-0.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#define STACK_SIZE 10
+
+int stack[STACK_SIZE];
+int stackzw[STACK_SIZE];
+int stack_pointer = 0;
+
+void push (int x)
+{
+  stack[stack_pointer++] = x;
+}
+
+
+
+int binSearch( int Suchwert) { 
+	int low = 0;
+	int high = stack_pointer;
+	int mid;
+
+	while (low <= high) {
+		mid = (low+high)/2;
+		if (Suchwert < stack[mid]) high = mid-1;
+		else if (Suchwert > stack[mid]) low = mid+1;
+		else return mid; // gefunden
+	}
+	return (-1); // nicht gefunden
+}
+int binSearchR(int lo, int hi, int x) 
+    {
+        if (lo>hi)
+            return -1;
+        int m=lo+(hi-lo)/2;
+        if (x<stack[m])
+            return binSearchR(lo, m-1, x);
+        if (x>stack[m])
+            return binSearchR(m+1, hi, x);
+        return m;
+    }
+
+    int main (void)
+{
+
+  printf("%d \n",binSearch(7));
+  printf("%d \n",binSearchR(0, stack_pointer,7));
+  return 0;
+}
\ No newline at end of file
diff --git a/20200128/loesung-1g-1.c b/20200128/loesung-1g-1.c
new file mode 100755
index 0000000..0db3a72
--- /dev/null
+++ b/20200128/loesung-1g-1.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#define STACK_SIZE 10
+
+int stack[STACK_SIZE];
+int stackzw[STACK_SIZE];
+int stack_pointer = 0;
+
+void push (int x)
+{
+  stack[stack_pointer++] = x;
+}
+
+
+
+int binSearch( int Suchwert) { 
+	int low = 0;
+	int high = stack_pointer;
+	int mid;
+
+	while (low <= high) {
+		mid = (low+high)/2;
+		if (Suchwert < stack[mid]) high = mid-1;
+		else if (Suchwert > stack[mid]) low = mid+1;
+		else return mid; // gefunden
+	}
+	return (-1); // nicht gefunden
+}
+int binSearchR(int lo, int hi, int x) 
+    {
+        if (lo>hi)
+            return -1;
+        int m=lo+(hi-lo)/2;
+        if (x<stack[m])
+            return binSearchR(lo, m-1, x);
+        if (x>stack[m])
+            return binSearchR(m+1, hi, x);
+        return m;
+    }
+
+int main (void)
+{
+  push (1);
+  push (3);
+  push (5);
+  push (7);
+  push (11);
+  printf("%d \n",binSearch(7));
+  printf("%d \n",binSearchR(0, stack_pointer,7));
+  return 0;
+}
diff --git a/20200128/loesung-3-0.c b/20200128/loesung-3-0.c
new file mode 100644
index 0000000..70e3399
--- /dev/null
+++ b/20200128/loesung-3-0.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+int sum;
+
+void foreach (int *a, void (*fun) (int x))
+{
+  for (int *p = a; *p >= 0; p++)
+    fun (*p);
+}
+
+void sum_of_positives (int x)
+{
+  if (x > 0)
+    sum += x;
+}
+
+void even_or_odd (int x)
+{
+  if (x % 2)
+    printf ("%d ist ungerade.\n", x);
+  else
+    printf ("%d ist gerade.\n", x);
+}
+
+int main (void)
+{
+  int numbers[] = { 12, 17, 32, 1, 3, 16, 19, 18, -1 };
+  foreach (numbers, sum_of_positives);
+  printf ("%d\n", sum);
+  return 0;
+}
diff --git a/20200128/loesung-3-1.c b/20200128/loesung-3-1.c
new file mode 100644
index 0000000..0a1be72
--- /dev/null
+++ b/20200128/loesung-3-1.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+int sum = 0;
+
+void foreach (int *a, void (*fun) (int x))
+{
+  for (int *p = a; *p >= 0; p++)
+    fun (*p);
+}
+
+void sum_of_positives (int x)
+{
+  if (x > 0)
+    sum += x;
+}
+
+void even_or_odd (int x)
+{
+  if (x % 2)
+    printf ("%d ist ungerade.\n", x);
+  else
+    printf ("%d ist gerade.\n", x);
+}
+
+int main (void)
+{
+  int numbers[] = { 12, 17, 32, 1, 3, 16, 19, 18, -1 };
+  foreach (numbers, sum_of_positives);
+  printf ("%d\n", sum);
+  return 0;
+}
diff --git a/20200128/loesung-4-0.c b/20200128/loesung-4-0.c
new file mode 100644
index 0000000..a7c3270
--- /dev/null
+++ b/20200128/loesung-4-0.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+#include "aufgabe-4.xbm"
+
+int main (void)
+{
+  printf ("width = %d\n", aufgabe_4_width);
+  printf ("height = %d\n", aufgabe_4_height);
+  printf ("bits =");
+  int bytes_per_line = 2;  // 14 Bit aufgerundet --> 16 Bit
+  for (int i = 0; i < bytes_per_line * aufgabe_4_height; i++)
+    printf (" %d", aufgabe_4_bits[i]);
+  printf ("\n");
+  return 0;
+}
diff --git a/20200128/loesung-4-1.c b/20200128/loesung-4-1.c
new file mode 100644
index 0000000..246f3d4
--- /dev/null
+++ b/20200128/loesung-4-1.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+#include "aufgabe-4.xbm"
+
+int main (void)
+{
+  printf ("width = %d\n", aufgabe_4_width);
+  printf ("height = %d\n", aufgabe_4_height);
+  printf ("bits =");
+  int bytes_per_line = 2;  // 14 Bit aufgerundet --> 16 Bit
+  for (int i = 0; i < bytes_per_line * aufgabe_4_height; i++)
+    printf (" %x", aufgabe_4_bits[i]);
+  printf ("\n");
+  return 0;
+}
diff --git a/20200128/loesung-4-2.c b/20200128/loesung-4-2.c
new file mode 100644
index 0000000..5d9f8e9
--- /dev/null
+++ b/20200128/loesung-4-2.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+#include "aufgabe-4.xbm"
+
+int main (void)
+{
+  int bytes_per_line = 2;  // 14 Bit aufgerundet --> 16 Bit
+
+  FILE *f = fopen ("test.pbm", "w");
+  fprintf (f, "P4\n");
+  fprintf (f, "%d %d\n", aufgabe_4_width, aufgabe_4_height);
+  for (int i = 0; i < bytes_per_line * aufgabe_4_height; i++)
+    fprintf (f, "%c", aufgabe_4_bits[i]);
+
+  fclose (f);
+  return 0;
+}
diff --git a/20200128/loesung-4-3.c b/20200128/loesung-4-3.c
new file mode 100644
index 0000000..e0ac291
--- /dev/null
+++ b/20200128/loesung-4-3.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+#include "aufgabe-4.xbm"
+
+int main (void)
+{
+  int bytes_per_line = 2;  // 14 Bit aufgerundet --> 16 Bit
+
+  FILE *f = fopen ("test.pbm", "w");
+  fprintf (f, "P4\n");
+  fprintf (f, "%d %d\n", aufgabe_4_width, aufgabe_4_height);
+  for (int i = 0; i < bytes_per_line * aufgabe_4_height; i++)
+    {
+      char byte = 0;
+      for (int k = 0; k < 8; k++)
+        if (aufgabe_4_bits[i] & (1 << k))
+          byte |= 1 << (7 - k);
+      fprintf (f, "%c", byte);
+    }
+
+  fclose (f);
+  return 0;
+}
diff --git a/20200128/loesung-4-4.c b/20200128/loesung-4-4.c
new file mode 100644
index 0000000..dfcd72a
--- /dev/null
+++ b/20200128/loesung-4-4.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+#include "aufgabe-4.xbm"
+
+int main (void)
+{
+  int bytes_per_line = (aufgabe_4_width + 7) / 8;  // 14 Bit aufgerundet --> 16 Bit
+
+  FILE *f = fopen ("test.pbm", "w");
+  fprintf (f, "P4\n");
+  fprintf (f, "%d %d\n", aufgabe_4_width, aufgabe_4_height);
+  for (int i = 0; i < bytes_per_line * aufgabe_4_height; i++)
+    {
+      char byte = 0;
+      for (int k = 0; k < 8; k++)
+        if (aufgabe_4_bits[i] & (1 << k))
+          byte |= 1 << (7 - k);
+      fprintf (f, "%c", byte);
+    }
+
+  fclose (f);
+  return 0;
+}
diff --git a/20200128/test.pbm b/20200128/test.pbm
new file mode 100644
index 0000000000000000000000000000000000000000..bc5b70b1ad6f7236ec9469afac311f05fb056946
GIT binary patch
literal 37
scmWGA;W9K)Ff`#}VBkL>pr9b&z~OVk#lwLkK*PgBeS&}j{{aRD0EdJJpa1{>

literal 0
HcmV?d00001

-- 
GitLab