COMP1511 17s1 Introduction to Programming
  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. Did you blog last week? What is this week's blogging theme?
  3. 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?

  4. See list.c and list.h for this weeks linked list tute questions, and testlist.c for autotests. Alternatively download all three files at tut_list_files.zip.

    The function member tests if a specified value is a found in a specified list, i.e. the function should return 1 if the value occurs in the list 0 otherwise. It has this prototype:

    int member(int value, struct node *list);
    
    Implement this function both iteratively (using a while/for loop) and recursively.

  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);
    
    It should not create (malloc) any new list elements.

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

  6. 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.

  7. 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.

  8. 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?

  9. 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);
    

  10. 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 bigesst 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'.
    

  11. 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.

  12. 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).

  13. 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)
    

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

    Use this function from the math.h library:

    double fabs(double x);
    

  15. 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.