COMP1511 19T2
COMP1511 19T2
  1. The tutorial will start with a code review.

    Your tutor has asked a lab pair to present their week 7 work.

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

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

  2. New lab partners this week! Your tutor will assign new lab pairs for the final three weeks of the term.
  3. Assignment 2 has been released - Castle Defense. What is Castle Defense? What are the different features of the Castle Defense game?

    Try out the reference Castle Defense implementation: 1511 castle_defense.

  4. What is a Realm and a Location? How are they used and how do they relate to each other?

  5. Have you learnt anything you think would be useful about Castle Defense to share with the tutorial?

    Do you have questions that others in the tutorial may be able to answer?

  6. Given a linked list with the following structures:
    
    struct overall {
        struct node *start;
        struct node *end;
    };
    
    
    struct node {
        int data;
        struct node *next;
    };
    
    where start points at the first element of the list and end points at the last element of the list:

    1. What does the overall struct look like in memory? What is its purpose?
    2. What does the node struct look like in memory? What is its purpose?
    3. What does the malloc function do? How could you use it to allocate memory for a single struct node?
    4. Write a function called new_node that creates a new node with the specified data value:
      
      struct node *new_node(int data) {
      }
      
    5. Write a function called insert_at_head that uses your new_node function to create a node, and then inserts that node at the front of the linked list.
      
      void insert_at_front(struct overall *o, int data) {
      }
      
    6. Write a function called insert_before_end that inserts an element just before the end element.
      
      void insert_before_end(struct overall *o, int data) {
      }
      
    1. My program works when I run it, autotest says I have an uninitialized variable. What is an uninitialised variable and and how can it affect my program?
    2. How does dcc help you find errors?
    3. What is dcc --leakcheck?
  7. Last week's lab used an array of this struct to store a whale sighting:
    #define MAX_SPECIES_NAME_LENGTH 128
    struct pod {
        struct date when;
        int         how_many;
        char        species[MAX_SPECIES_NAME_LENGTH];
    };
    
    What if we use a linked list of this struct to store a whale sighting?
    struct pod {
        struct pod  *next;
        struct date *when;
        int         how_many;
        char        *species;
    };
    
    And a date is still represented by the same struct:
    struct date {
        int year;
        int month;
        int day;
    };
    
    Sketch out how these two data structures would be laid out in memory.

    Discuss the differences and how that will affect the use of this data

    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.

    • Write a C function split_string which takes a string read by fgets containing substrings separated by commas, places the substrings in an char[][] array and returns the number of strings.

      You can assume there are at most 128 words on the line and no word is more than 32 characters long.

    • Write a C function print_substrings which prints one per line the substrings in the char[][] array created in the previous question.
    • Write a C function substrings_sorted which given the char[][] array in the previous question returns 1 if the substrings are increasing order and 0 otherwise.
    • Write a C function search_substrings which given the char[][] array in the previous question returns 1 if the array contains a specified string and 0 otherwise.
    • Write a program substring.c which reads lines using fgets and calls the above functions.
  8. Write a function that is given two struct date pointers and compares returns 0 if they are equal, -1 if the first is less than the second and 1 if the first is larger than the second.
    int compare_date(struct date *d1, struct date *d2);