COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)
  1. Your tutor has asked a lab pair to present their week 11 work.

    Discuss the good, the bad and the ugly aspects of their code.

    Please be gentle in any criticism - we are all learning!

  2. How are you progressing with the assignment?

    Do you have any questions? Have you learned anything that would be useful to share with the rest of the tutorial?

  3. What is a use after free error? Give an example.

    Discuss why these are extremely dangerous, one of the worst causes of bugs in C programs and a major source of security vulnerabilities.

  4. What is a memory leak?

    What does dcc --leak-check do?

  5. Implement a function list_append which appends its second argument to its first. It should have this prototype:
    struct node *list_append(struct node *list1, struct node *list2);
    
    As usual, struct node has this definition:
    struct node {
        struct node *next;
        int          data;
    };
    
    It should not create (malloc) any new list elements.

    It should just change the appropriate next field in the first list.

    1. Write a function strings_to_list which takes an array of pointers to strings and converts it to a linked list. It should have this prototype:
      struct node *strings_to_list(int len, char *strings[]);
      
      Assume the strings contain only digit characters,

      It might be called like this:

          char *powers[] = {"2", "4", "8", 16"};
          struct node *head = strings_to_list(4, powers);
      

    2. How would you use strings_to_list to convert a program's command line arguments to a linked list?

    3. How would you use strings_to_list to convert a program's command line arguments to two linked lists?

      Assume, a command line argument of "-" separates the arguments to be converted.

  • The function merge_sorted is used to merge two ordered lists. It will combine two sorted list into a new sorted list (non-decreasing). It has this prototype:
    struct node *merge_sorted(struct node *list1, struct node *list2);
    

    It should not create (malloc) any list elements. It should change the appropriate next fields to combined the lists.

    Implement this function both iteratively (using a while/for loop) and recursively.

    Revision questions

    The remaining tutorial questions are primarily intended for revision - either this week or later in session.

    Your tutor may still choose to cover some of the questions time permitting.

  • The function insert_ordered is used to construct ordered lists. It will insert the supplied value at the appropriate point in the list remains sorted (non-decreasing). It has this prototype:
    struct node *insert_ordered(int item, struct node *list);
    
    It should create (malloc) just 1 list element and change the appropriate next field in the list to insert it.

    Implement this function.

  • Consider:

    double ff[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
    double *fp = &ff[0];
    

    What are the similarities between ff and fp? What are the differences?

  • Consider:

    char s[] = "Hello World!";
    char *cp = s;
    char *cp2 = &s[8];
    

    What is the output when the following statements are executed?

    printf("%s\n", cp);
    printf("%c\n", *cp);
    printf("%c\n", cp[6]);
    printf("%s\n",cp2);
    printf("%c\n",*cp2);
    

  • We have student fines in a file named fines.txt this format:
    Linus Torvalds fined $98 for not attending lectures.
    Denis Ritchie fined $50 for eating in labs.
    Ken Thompson fined $150 for attending lecture in his underpants.
    
    Write a program student_fine.c which reads this file and prints the student with biggest fine including the amount and reason in this format.
    a.out
    Biggest fine was $150 given to Ken Thompson for 'attending lecture in his underpants'.
    

  • Write a function
    int non_decreasing(int n, int a[n])
    
    which checks whether items in an array are sorted in non-decreasing order. (i.e. a[i] ≥ a[i-1], for 0<i<N). Your function should returns 1 if the items are in non-decreasing order, 0 otherwise.

  • Write a function
    int find_index(int x, int n, int a[n])
    
    which takes two integers x and n together with an array a[] of n integers and searches for the specified value within the array. Your function should return the smallest index k such that a[k] is equal to x (or -1 if x does not occur in the array).

  • Write a function, prototype below, that mirrors the behaviour of the library function strrchr. This function takes a string and a character as arguments, and returns a pointer to the last occurrence of the character c in the string s. It returns NULL if the character cannot be found.
    char *strrchr(char s[], char c)
    

  • Write a function to calculate the Manhattan distance between two points.

    Use this function from the math.h library:

    double fabs(double x);
    

  • Write a program multiply.c that performs addition or multiplication of integers, as follows. Your program should continually read integers from the user until end-of-input is encountered. Then it should print either the sum or the product of the input numbers. The program behaviour is controlled by either of the following command-line arguments:
    -add, -multiply. If the wrong command-line argument(s) are supplied your program should do nothing.