Week 08 Tutorial Sample Answers

  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. Assignment 2 has been released - Beats by CSE. What is Beats by CSE and what can it do?

    Try out the reference Beats by CSE implementation: 1511 cs_beats.

    In Assignment 2, you will be implementing a way to create music. You will use linked lists to create tracks that contain beats, and beats that contain notes.
  3. What are Tracks, Beats, and Notes? How are they used and how do they relate to each other?

    Tracks, Beats, and Notes are all structs that you will use to create music.

    Tracks contain multiple beats, and beats contain multiple notes. You should consider how you can use linked lists here...

  4. Have you learnt anything you think would be useful about CS bEats to share with the tutorial?

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

  5. What is a struct?

    How does it differ from an array?

    Arrays and struct are both compound data types (collections), formed from other data types.

    Array are homogeneous - formed from a single data type.

    Structs can be heterogeneous - formed from multiple data types.

    Array elements are accessed with integer array indices.

    Struct members are accessed by name.

  6. How could we represent a student and their assignment 1 mark as a struct? What fields might we want to include?

    We'd want to declare a struct in our code before any of the functions that would use it.

    Some of the fields we might use are:

    • string - student name
    • int - zid
    • double - assignment 1 mark

    The code might look something like:

    #define MAX_LENGTH 100
    
    struct student {
        char name[MAX_LENGTH];
        int zid;
        double ass1_mark;
    };
    
  7. Create a

    struct student
    with the name "Gareth", zid of 5151515, assignment 1 mark of 60.2 in the main function (hint, you do not need functions or malloc).
    int main(void) {
        struct student gareth;
        strcpy(gareth.name, "Gareth");
        gareth.zid = 5151515;
        gareth.ass1_mark = 60.2;
    }
    
  8. Create a struct student * with the name "Sally", zid of 5252525, assignment 1 mark of 71 using a function (hint, you will need to use malloc)

    struct student *create_student(char name[MAX_LENGTH], int zid, double a1_mark);
    
    int main(void) {
        struct student *sally = create_student("Sally", 5252525, 71);
    }
    
    struct student *create_student(char name[MAX_LENGTH], int zid, double a1_mark) {
        struct student *this_student = malloc(sizeof(struct student));
        strcpy(this_student->name, name);
        this_student->zid = zid;
        this_student->ass1_mark = a1_mark;
        
        return this_student;
    }
    
  9. Which of these structs is currently causing a memory leak and why?

    Use dcc --leakcheck to confirm this

    Gareth's struct student variable has been created as a normal variable, so it will be cleaned up automatically when the main function ends.

    Sally's struct student * is a pointer that accesses allocated memory. Since the memory was allocated using malloc, it exists outside of any functions so it won't be cleaned up automatically.

    This memory will need to be freed. At the moment, since we're not using free() to release the memory, it will stay allocated to Sally's struct, and cause a memory leak if we leave it there after we're finished with it.

  10. We have a struct student and a struct student *

    If we want to access the fields of these, when do we use . and when do we use ->?

  11. The . is used to access the fields in a struct (struct student).

    The -> is used to access the fields when we have a pointer to a struct (struct student *).

  12. What is a linked list? How does it compare to an array? Which will you be using in assignment 2?

    A linked list is made up of separate, identical type structs that are usually in allocated memory. These are usually called list "nodes". These are linked together by a field of their struct that is a pointer to another node.

    An array is a single block of memory large enough to hold an exact number of an identical type of variable. These are accessed via an index which shows how far from the initial memory address the particular element is.

    Assignment 2 will be based on Linked Lists.

  13. Edit the existing struct student, to be able to point to another struct student.'

    Update your function which creates an instance of a student to account for this new pointer.

    struct student {
        char name[MAX_LENGTH];
        int zid;
        double ass1_mark;
        struct student *next;
    };
    
    struct student *create_student(char name[MAX_LENGTH], int zid, double a1_mark, struct student *next) {
        struct student *this_student = malloc(sizeof(struct student));
        strcpy(this_student->name, name);
        this_student->zid = zid;
        this_student->ass1_mark = a1_mark;
        this_student->next = next;
        
        return this_student;
    }
    
  14. Make a linked list to represent a class of students

    We can build a list by continually adding elements at the start of the list.

    This means that each new element becomes the head of the list and its next pointer will aim at whatever had previously been the head of the list.

    int main(void) {
        struct student *head = create_student("Sally", 5252525, 71, NULL);
        head = create_student("Gareth", 5151515, 60.2, head);
    }
    
  15. Create another struct definition and instance to represent the class.

    It should contain the name (i.e. M13A), the number of students and a pointer to the linked list you just made.

    struct class {
        char class_name[MAX_LENGTH];
        int num_students;
        struct student *head;
    };
    
  16. Write a function to compare the marks of two students. It should return 1 if student a has higher marks than student b, -1 if student b has higher marks than student a, and 0 if they have the same mark. It should have the prototype

    int compare_marks(struct student student_a, struct student student_b) 
    int compare_marks(struct student *student_a, struct student *student_b) {
        if (student_a->ass1_mark > student_b->ass1_mark) {
            return 1;
        } else {
            return 0;
        }
    }