2CHEL FSST Test (Group A)

2025-01-21

Max. 100 points

Name:

Task Max. Achieved
1 10
2 20
3 35
4 35
Sum 100
Grading: >= 88: 1, >=76: 2, >=64 : 3, >50: 4, <=50: 5
  1. Answer the following statements indicating whether they are True or False.
    0-2 correct: 0 points, 3 correct: 5 points, 4 correct: 10 points.
    Statement True False
    Auf einem modernen Betriebssystem benötigt ein Pointer 4 Bytes.
    Ein Pointer kann auf einen anderen Pointer zeigen.
    Ein C-Struct ist das gleiche wie ein C-Array.
    Variablen, welche in einem C-Struct enthalten sind, können unterschiedliche Datentypen haben.
  2. Welcher Text wird durch die folgenden Code Snippets in der C-Programmiersprache auf Standard Output (Terminal) geschrieben? Schreibe den genauen Output des Snippets. Die Snippets werden als Teil von int main() ausgeführt und alle notwendigen #includes sind vorhanden. Wenn das Snippet nicht kompilierbar ist oder zu einem Absturz führen würde, so ist das Wort “ERROR” als Antwort zu geben. Erzeugt das Snippet keinen Output (wenn beispielsweise kein printf oder dergleichen aufgerufen wird), so ist "-" als richtige Antwort zu verwenden. (je 5 Punkte)
    1. int a = 5;
      int* ptr = &a;
      *ptr *= 5;
      printf("%d\n", a);
      25
    2. int a = 5;
      int* ptr = &a;
      int** p = &ptr;
      **p /= **p;
      printf("%d\n", a);
      1
    3. int array[] = {1, 2, 3, 4, 5};
      int* array_ptr = array;
      int count = sizeof(array) / sizeof(int);
      printf("%d\n", count);
      5
    4. typedef struct {int year; int month; int day;} IsoDate;
      IsoDate date = {2025, 1, 21};
      printf("%s\n", date);
      ERROR
  3. 35 Punkte
    Schreibe eine Funktion void capitalize(char* string) welche den übergebenen String so verändert, dass jedes Wort mit einem Großbuchstaben anfängt. Ein Anfangsbuchstabe ist dadurch gekennzeichnet, dass direkt davor ein Leerzeichen (' ') steht. Der erste Buchstabe im String ist auch ein Anfangsbuchstabe. Alle anderen Buchstaben sollen unverändert bleiben.
    Die Umwandlung zu Großbuchstaben kann mittels ASCII Codes berechnet werden. Die Verwendung der Funktion int toupper(int c) (aus ctype.h) ist auch erlaubt. stdbool.h darf verwendet werden. Andere Funktionen der Standardbibliothek dürfen nicht verwendet werden.
    Zum Beispiel:
    char text[] = "The quick brown fox jumps over the lazy dog.";
    capitalize(text);  // text becomes "The Quick Brown Fox Jumps Over The Lazy Dog."
    5 points for the correct function signature
    5 points for initializing a char to keep track of preceding spaces
    5 points for a correct loop
    5 points for a condition testing for previous spaces
    5 points for capitalizing initials
    5 points for correctly keeping track of previous spaces
    5 points for leaving all other chars unchanged
    void capitalize(char* string) {
      bool is_initial = true;
      while (*string) {
        if (is_initial) {
          *string = toupper(*string);
          is_initial = false;
        }
        if (*string == ' ') {
          is_initial = true;
        }
        string++;
      }
    }
  4. 35 Punkte
    Die Datenstruktur IsoDate ist definiert als
    typedef struct {int year; int month; int day;} IsoDate;
    Schreibe eine Funktion int compareDate(IsoDate* d1, IsoDate* d2) welche die übergebenen IsoDates vergleicht. compareDate(.) gibt einen int zurück gibt, der das Resultat des Vergleichs wie folgt beschreibt: Zum Beispiel:
    IsoDate first = { .year = 2025, .month = 1, .day = 21 };
    IsoDate second = { .year = 2025, .month = 1, .day = 22 };
    IsoDate third = { .year = 2024, .month = 12, .day = 24 };
    compareDate(&first, &first);  // -> 0
    compareDate(&first, &second);  // -> -1
    compareDate(&first, &third); // -> 1
    Der Code muss eine konsistente, übersichtliche Einrückung haben!
    5 points for the correct function signature
    5 points for correctly de-referencing struct pointers (-> operator)
    5 points for returning the correct value if the year differs
    5 points for returning the correct value if the month differs
    5 points for returning the correct value if the day differs
    5 points for returning 0 if all members are equal
    5 points for consistent indentation
    int compareDate(IsoDate* d1, IsoDate* d2) {
      if (d1->year < d2->year) {
        return -1;
      } else if (d1->year > d2->year) {
        return 1;
      }
      if (d1->month < d2->month) {
        return -1;
      } else if (d1->month > d2->month) {
        return 1;
      }
      if (d1->day < d2->day) {
        return -1;
      } else if (d1->day > d2->day) {
        return 1;
      }
      return 0;
    }