Week 08 Tutorial Questions

    Assignment 2 - CS Beats

  1. Assignment 2 has been released - CS Beats.

    Where can you find the important information you need to know before starting the assignment?

  2. Structs

  3. What is a struct?

    How does it differ from an array?

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

  5. Create a struct student with the name "Frankie", zid of 5151515, assignment 1 mark of 60.2 in the main function (hint, you do not need functions or malloc).

  6. Struct Pointers

  7. Use a function to create a struct student * with the name "Chicken", zid of 5252525, assignment 1 mark of 71 using a function (hint, you will need to use malloc)

  8. Which of these structs is currently causing a memory leak and why?

    Use dcc --leakcheck to confirm this.

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

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

  11. Build a linked list to represent a class of students by adding new students at the head of the list.

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

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

  14. Give a struct that might hold lab, assignmment and exam marks for a COMP1511 student. Remember, there is more than 1 lab and more than 1 assignment.

  15. What is wrong with this program? Fix these issues in the file below.

    #include <stdio.h>
    
    #define MAX_PLATE 10000
    
    struct parking_fine {
        double   amount;
        char     number_plate[MAX_PLATE];
    };
    
    
    int read_parking_fine(struct parking_fine *fine);
    
    int main(void) {
        struct parking_fine f;
    
        if (read_parking_fine(f)) {
            printf("%lf %s\n", f.amount, f.number_plate);
        }
    
        return 0;
    }
    
    // return 1 if a parking fine was successfully read, 0 otherwise
    
    int read_parking_fine(struct parking_fine *fine) {
        if (scanf("%lf", (fine.amount)) != 1) {
            return 0;
        }
        return fgets(fine->number_plate, MAX_PLATE, stdin) != NULL;
    }
    
/import/chopin/1/cs1511/public_html/21T1/public/tlb/questions/q-revision-week-8.html