-
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!
-
Who is your new lab partner?
-
Did you blog last week? What is this week's blogging theme?
-
Consider a Trader Bot world with these 5 locations and a game lasting 4 turns:
Name | Type | Commodity | Quantity | Price
|
---|
CSE | Start | | |
|
Royal Australian Mint | Buyer | Gold | 2 | 40000
|
BHP | Seller | Gold | 8 | 30000
|
Caltex | Petrol station | Fuel | 684 | 215
|
Dump | Dump | | |
|
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.
- Move 2
- Buy 6
- Move -1
- Sell 6
List all the changes which occur in the Trader Bot world each turn
-
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.
-
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 Trader Bot world differ from
the linked lists you've seen in lectures?
-
What is tricky about circular lists ?
-
How can the lab exercise help with the assignment ?
-
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.
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.
-
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.
-
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.