Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
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 |
Deckard makes these actions in the 4 turns the game lasts.
List all the changes which occur in the Trader Bot world each turn
int cargo_mars_bars(struct bot *b)which given a pointer to a bot returns how many Mars Bars the bot's cargo contains.
cargo_mars_bars.c
#include <stdio.h> #include <string.h> #include "trader_bot.h" // return how many "Mars Bars" the bot's cargo contains int cargo_mars_bars(struct bot *b) { for (struct cargo *c = b->cargo; c != NULL; c = c->next) { if (strcmp("Mars Bars", c->commodity->name) == 0) { // A commodity occur at most once in the cargo list return c->quantity; } } return 0; }
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
mars_bars_for_sale.c
#include <stdio.h> #include <string.h> #include "trader_bot.h" // return how many "Mars Bars" for sale at the current location int mars_bars_for_sale(struct bot *b) { struct location * l = b->location; if (l->type == LOCATION_SELLER && strcmp("Mars Bars", l->commodity->name) == 0) { return l->quantity; } else { return 0; } }
They form a circular list.
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;
sizeof b
should be sizeof *b
sizeof struct node
should be sizeof (struct node)
malloc(8)
is actually correct on CSE systems but is
non-portable, e.g. on a 64-bit OS sizeof (struct node) == 12
(or maybe 16)
d.data
is incorrect as d is not a struct its a pointer to a struct
c->data
is illegal as c will be NULL when its executed
int member(int value, struct node *list);Implement this function both iteratively (using a while/for loop) and recursively.
int member(int value, struct node *list) { return list != NULL && (list->data == value || member(value, list->next)); }Iterative:
// return true iff value occurs in list int member(int value, struct node *list) { struct node *n; for (n = list; n != NULL; n = n->next) if (n->data == value) return 1; return 0; }
Your tutor may still choose to cover some of the questions time permitting.
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.
struct node *list_append(struct node *list1, struct node *list2) { struct node *n; if (list1 == NULL) { return list2; } n = list1; while (n->next != NULL) { n = n->next; } n->next = list2; return list1; }
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);
100 100
There are several correct answers for this but some common ones are: You do not need to know the size of a linked list upfront; It is easier to remove the first element of a list. A disadvantage might be that you do not get random access. Random access means you can directly go to any element in the array. In contrast, you will need to step through all the nodes before to get to the 20th element in a linked list.