Week 11 Tutorial Questions

  1. It's week 11 - revision week!
    So far in the course we have covered:
    • variables
    • if
    • looping
    • functions
    • arrays
    • linked lists
    • characters
    • strings
    • pointers
    • structs
    • memory allocation
    • ADTs
    • Multi file programs
  2. Which topics are you struggling with? If you are struggling with any particular topic, perhaps your tutor could revisit that week.
    The revision activities can be found here, but there are also some available below:

    Command line arguments

  3. In the following program, what are argc and argv? The following program prints number of command-line arguments and each command-line argument on a separate line.
    // print command line argument
    // Andrew Taylor - andrewt@unsw.edu.au
    // 24/4/18
    #include < stdio.h>
    #include < stdlib.h>
    int main(int argc, char *argv[]) {
        int i;
        printf("argc=%d\n", argc);
        i = 0;
        while (i < argc) {
            printf("argv[%d]=%s\n", i, argv[i]);
            i = i + 1;
        }
        return 0;
    }
    
    What will be the output of the following commands?
    % dcc -o print_arguments print_arguments.c
    %
    % print_arguments Sydney Olympic 2000
    
  4. The following program sums up command-line arguments. Why do we need the function atoi in the following program? The program assumes that command-line arguments are integers. What if they are not integer values? See strol for a more powerful library function which would allow checking.
    #include < stdio.h>
    #include < stdlib.h>
    int main(int argc, char *argv[]) {
    int sum = 0;
        int argument = 1;
        while (argument < argc) {
            sum = sum + atoi(argv[argument]);
            argument= argument + 1;
        }
        printf("sum of command-line arguments = %d\n", sum);
    return 0;
    }
    

    Arrays

  5. Write a function that takes in a 2d array of ints and multiplies every value in the array by a given int.
    It will have this prototype:
    void scalar_multiply(int rows, int columns, int matrix[rows][columns],  int scalar)
    

    Linked Lists

  6. // Linked list revision
    // Base code: Elizabeth Willer 
    
    #include < stdio.h>
    #include < stdlib.h>
    #include 
    
    struct node {
        struct node *next;
        int          data;
    };
    
    
    struct node *strings_to_list(int len, char *strings[]);
    void print_list(struct node *head);
    int sum_list(struct node *head);
    struct node *find_item(struct node *head, int item);
    int find_last(struct node *head);
    int find_nth(struct node *head, int nth);
    struct node *add_last(struct node *head, int item);
    struct node *remove_first(struct node *head);
    struct node *remove_last(struct node *head) ;
    struct node *remove_item(struct node *head, int item);
    void free_list(struct node *head);
    
    
    
    int main(int argc, char *argv[]) {
       // TODO: Write some test cases for your functions  
       char *list1[] = {"2", "4", "8", "16"};
       struct node *head1 = strings_to_list(4, list1);
       print_list(head1);
       char *list2[] = {"1", "2", "3", "4"};
       struct node *head2 = strings_to_list(4, list2);
       print_list(head2);
       
    
       return 0;
    }
    
    
    
    
    // create linked list from array of strings
    struct node *strings_to_list(int len, char *strings[]) {
        struct node *head = NULL;
        int i = len - 1;
        while (i >= 0) {
            struct node *n = malloc(sizeof (struct node));
            assert(n != NULL);
            n->next = head;
            n->data = atoi(strings[i]);
            head = n;
            i = i-1;
        }
        return head;
    }
    
    
    // print linked list in python style (i.e. [1, 2, 3, 4])
    void print_list(struct node *head) {
        // TODO: Write this function first!
        // This will help you write your tests!
    }
    
    
    //calculate sum of a list
    int sum_list(struct node *head) {
        // TODO: complete this function
        return 42;
    }
    
    
    //find an item (say 25) in a list (return pointer to that item)
    struct node *find_item(struct node *head, int item) {
        // TODO: complete this function
        return NULL;
    }
    
    //find last item in a list (return the value of that item)
    int find_last(struct node *head) {
        // TODO: complete this function
        return 42;
    }
    
    //find nth item in a list (return the value of that item)
    int find_nth(struct node *head, int nth) {
        // TODO: complete this function
        return 42;
    }
    
    //add item at the end of the list (return a pointer to the head of the list)
    struct node *add_last(struct node *head, int item) {
        // TODO: complete this function
        return NULL;
    
    }
    //remove first item from a list (return a pointer to the head of the list)
    struct node *remove_first(struct node *head) {
        // TODO: complete this function
        return NULL;
    }
    
    //remove last item from a list (return a pointer to the head of the list)
    struct node *remove_last(struct node *head) {
        // TODO: complete this function
        return NULL;
    }
    
    //remove a given item (say 25) from a list (return a pointer to the head of the list)
    struct node *remove_item(struct node *head, int item) {
        // TODO: complete this function
        return NULL;
    
    }
    
    // free the memory used by the linked list
    void free_list(struct node *head) {
        // TODO: complete this function
        
    }
    

    During the "lab" time please work through the revision exercises. You can find a link to the arrays revision here, and that will link you to the other revision materials: here

    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.