2AKIFT POS Test (Group A)

2023-05-31

Max. 100 points

Name:

Task Max. Achieved
1 25
2 25
3 25
4 25
Sum 100
Grading: > 87.5: 1, >75: 2, >62.5 : 3, >50: 4, <=50: 5
  1. Answer the following statements indicating whether they are True or False.
    0-5 correct: 0 points, 6 correct: 5 points, 7 correct: 10 points, 8 correct: 15 points, 9 correct: 20 points, 10 correct: 25 points.
    Statement True False
    When recompiling a large project after a small change, make can drastically reduce the compile time.
    make can use multiple processor cores in parallel when compiling a project.
    make inspects a project's source code to automatically generate each target's dependencies.
    cmake is a platform dependend version of make that only works on Linux.
    Function pointers allow functions to be treated as first class citizen.
    Without function pointers, it is impossible to reserve heap memory.
    Function pointers are required for using callbacks in C.
    A function that is part of a C struct can automatically access all of the struct's data members.
    Command-line arguments are not used in modern programs.
    The C standard provides functions for parsing long command-line arguments and subcommands.
  2. The following C17 snippets are executed as a part of int main(.) (unless main is part of the snippet). Assume that all neccessary standard library #includes are present.
    What do these code snippets write to stdout? Write their exact output below each snippet.
    If a snippet cannot be compiled, would crash or its output is undefined, it is enough to write “ERROR”. If there is no visible output, write "-".
    5 points for each correct answer.
    1. char* names[] = {"Pat", "Chris", "Sue", "Steve"};
      qsort(names);
      ERROR
    2. char* names[] = {"Pat", "Chris", "Sue", "Steve"};
      FILE* f = fopen("data.txt", "w");
      for (size_t i = 0; i < sizeof(names) / sizeof(char*); ++i) {
        fprintf(f, "%s\n", names[i]);
      }
      -
    3. char name[] = "Sue";
      char* n = name;
      while (*n) {
        printf("%c", *n);
        n++;
      }
      Sue
    4. char str[] = "students";
      char* haystack = str;
      char needle = 'x';
      int occ = 0;
      while (*haystack) {
        if (*haystack == needle) { occ++; }
        haystack++;
      }
      printf("%d\n", occ);
      0
    5. unsigned int radicand = 36;
      for (unsigned int i = 0; i <= radicand; ++i) {
        if (i * i == radicand) { printf("%u", i); }
      }
      6
  3. Integer Array
    Given the following struct
    struct intarray {
      size_t reserved;  // available storage
      size_t elements;  // current number of elements
      int* array;
    };
    Create a type IntArray that can be used to create instances of struct intarray. Implement the function void ia_erase(IntArray* ia, size_t index) that removes the element at index. If index is beyond the end of the array, nothing should be done.
    4 points for typedef
    4 points for the correct function name and signature
    4 points for returning if index is beyond the array
    4 points for decrementing the number of elements if index is within the array
    5 points for iterating over the correct elements (-2 for missing the end by 1)
    4 points for moving the tail of the array to fill the gap at index
    typedef struct intarray IntArray;
    
    void ia_erase(IntArray* ia, size_t index) {
      if (index >= ia->elements) { return; }
      ia->elements--;
      for (size_t i = index; i < ia->elements; ++i) {
        ia->array[i] = ia->array[i + 1];
      }
    }
  4. Hamming Distance
    Implement a function int hamming(char* first, char* second) that returns the hamming distance between the two given DNA strings. If first and second do not have the same length, return -1. Otherwise, return the number of differences in the two strings. For example,
    CCTAAGTAA
    CGTAAGTGA
     ^     ^
    
    has two differences and therefore your function should return 2 in this case. You may (but do not have to) use standard library functions.
    4 points for the correct function name and signature
    4 points for returning -1 if the lengths do not match
    4 points for correctly initializing the number of differences
    5 points for iterating over all characters (-2 for a character)
    4 points for incrementing the number of differences
    4 points for correctly returning the number of differences
    int hamming(char *lhs, char *rhs) {
      if (strlen(lhs) != strlen(rhs)) return -1;
      int difference = 0;
      for (size_t i = 0; i < strlen(lhs); ++i) {
        if (lhs[i] != rhs[i]) difference += 1;
      }
      return difference;
    }