COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)
  1. Consider a Fruit Bot world with these 5 locations and a simulation lasting 4 turns:

    NameTypeFruitQuantityPrice
    CSEOther
    Drum LabBuyerPears240
    Vice Chancellors GardenSellerPears830
    Power PlusSellerElectricity6842
    Fruit BinAnything10001
    Assume there is a single bot named Deckard who starts the simulation with $100 and battery level 48. Also assume Deckard has a maximum fruit kg of 7.

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

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

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

  • How many times is fruit_bot.c run during each simulation.

    What is its input?

    What is its output (give examples)

    How does a bot indicate whether it wishes to move East/West

  • The input to fruit_bot.c looks very complex, e.g.:
    *** Fruit Bot Parameters ***
    battery_capacity=74
    maximum_fruit_kg=21
    maximum_move=7
    
    *** Turn 1 of 19 *** ***
    
    CSE: other
    Mathews A: will buy 1 kg of Apples for $51/kg
    Quadrangle: other
    Campus Charging: will sell 100 kJ of Electricity for $4/kJ
    Kensington Apple Farm: will sell 3 kg of Apples for $18/kg
    Campus Compost Heap: will buy 1000 kg of Anything for $1/kg
    CLB 7: will buy 3 kg of Apples for $43/kg
    
    "Botty McBotbot" is at "CSE" with $117, battery level: 64, 5 kg of Apples
    "Buffalo McBuff" is at "Kensington Apple Farm" with $217, battery level: 74
    "COMP1511 Student" is at "Campus Compost Heap" with $1, battery level: 24, 11 kg of Apples
    *** You are "Botty McBotbot"
    
    how do I read that in with scanf?

  • fruit_bot_input returns a pointer to this struct declared in fruit_bot.h
    struct bot {
        char            *name;
        struct location *location;
        int             cash;
        int             battery_level;
        char            *fruit;
        int             fruit_kg;
        int             turns_left;
        int             battery_capacity;
        int             maximum_move;
        int             maximum_fruit_kg;
    };
    
    What do the fields mean.

    What value can the fields take? If int can they be negative?, zero? If pointer can they be NULL?

  • A struct bot includes a pointer to this struct declared in fruit_bot.h
    struct location {
        char                *name;
        char                *fruit;
        int                 price;
        int                 quantity;
        struct location     *east;
        struct location     *west;
        struct bot_list     *bots;
    };
    
    What do the fields mean.

    What value can the fields take? If int can they be negative?, zero? If pointer can they be NULL?

    What "non-fruit" values can the field fruit take?

  • Why is there a bots field in location structs?

  • Write a C function with this prototype:
    int mars_bars_on_board(struct bot *b)
    
    which given a pointer to a bot returns how many Mars Bars the bot has on board (yes Mars Bars are fruit)

  • 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

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

  • What is tricky about circular lists ?

  • How can the lab exercises help with the assignment ?

    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.

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

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

  • 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;
    

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

  • 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);
    

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