COMP1511 17s1 Introduction to Programming
  1. Your tutor has asked a lab pair to present their week 8 work.

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

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

  2. Who is your new lab partner?
  3. Did you blog last week? What is this week's blogging theme?
  4. Consider a Trader Bot world with these 5 locations and a game lasting 4 turns:
    NameTypeCommodityQuantityPrice
    CSEStart
    Royal Australian MintBuyerGold240000
    BHPSellerGold830000
    CaltexPetrol stationFuel684215
    DumpDump
    Assume there is a single bot named Deckard who starts the game with $100000 and 48 fuel. Also assume Deckard has a maximum cargo weight of 70 and a maximum cargo volume of 90. And that the Gold commodity has weight 20 and volume 10.

    Deckard makes these actions in the 4 turns the game lasts.

    1. Move 2
    2. Buy 6
    3. Move -1
    4. Sell 6

    List all the changes which occur in the Trader Bot world each turn

  5. Write a C function with this prototype:
    int cargo_mars_bars(struct bot *b)
    
    which given a pointer to a bot returns how many Mars Bars the bot's cargo contains.

  6. Write a C function with this prototype:
    int mars_bars_for_sale(struct bot *b)
    
    which given a pointer to a bot returns how Mars Bars are for sale at the bot's current location

  7. How do the struct used to represent locations in the Trader Bot world differ from the linked lists you've seen in lectures?

  8. What is tricky about circular lists ?

  9. How can the lab exercise help with the assignment ?

  10. C's sizeof operator is a prefix unary operator (precedes its 1 operand) - what are examples of other C unary operators?

  11. Why is C's sizeof operator different to other C unary & binary operators?

  12. Discuss errors in this code:
        struct node *a, *b, *c, *d;
        a = NULL:
        b = malloc(sizeof b);
        c = malloc(sizeof struct node);
        d = malloc(8);
        c = a;
        d.data = 42;
        c->data = 42;
    

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

    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. Implement a function list_append which appends its first argument to its second. 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.

  15. What does this code print
        int i;
        struct node *a[100];
        struct node *b = malloc(sizeof (struct node));
        b->data = 0;
        for (i = 0; i < 100; i = i + 1) {
            a[i] = b;
        }
        for (i = 0; i < 100; i = i + 1) {
            a[i]->data++;
        }
        printf("%d %d\n", a[0]->data, a[99]->data);
    

  16. Name one advantage and one disadvantage of using a linked list compared to an array. Also state an example of when you would use an array over a linked list.