COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Objectives

  • introducing assignments
  • introducing a complex data representation
  • practice working with pointers & structs

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

Getting Started

Create a new directory for this lab called lab11 by typing:
mkdir lab11
Change to this directory by typing:
cd lab11

Exercise: Traversing The Fruit Bot World (pair)

This is a pair exercise to complete with your lab partner.

Introduction

In this week's lab you will write functions useful for assignment 3.

You can use these functions for the assignment (make sure you include a comment acknowledging your lab partner's contribution). Solutions for these functions will also be made available after the lab deadline and you are permitted to use these for the assignment (again with a comment indicating the authorship of the functions).

First download a copy of fruit_bot.h (or use the cp command below to get it) and read through it carefully the types used to represent the Fruit Bot world. You will have discussed these in details in your tutorial.

You will also need to download a copy of fruit_bot_input.c . You do not need to read or understand fruit_bot_input.c

Printing the World

Download fruit_bot_world.c which contains the starting code for this exercise or use the cp command below. The main function in fruit_bot_world.c has this code:
struct bot *b = fruit_bot_input(stdin);
print_world(b);
Your task in this exercise is to complete this function:
void print_world(struct bot *b) {
    // Put your code here to:
    // print details of all locations in the Fruit Bot world
    // starting from bot's current location and going east
}
print_world should print a description of all locations in the bot's world.

Match the example below exactly.

You should start by printing the bot's location and then print locations going in an easterly direction (following east pointers).

You are not permitted to change location or bot structs. You can not, for example, change the east or west fields of locations, or the location field of the bot.

There are 6 test files (see the cp command below) which you can use to test your program, e.g. see world0.txt which contains a small world description and world1.txt which contains a larger world description.

There is a diagram here of world0 from above, which may help you understand the output below.

cp /web/cs1511/18s1/activities/fruit_bot_world/fruit_bot.h .
cp /web/cs1511/18s1/activities/fruit_bot_world/fruit_bot_input.c .
cp -n /web/cs1511/18s1/activities/fruit_bot_world/fruit_bot_world.c .
cp /web/cs1511/18s1/activities/fruit_bot_world/world*.txt .
dcc fruit_bot_world.c fruit_bot_input.c -o fruit_bot_world 
./fruit_bot_world  <world0.txt
CSE: other
Mathews A: will buy 1 kg of Apples for $51
Quadrangle: other
Campus Charging: will sell 100 kJ of Electricity for $4
Kensington Apple Farm: will sell 3 kg of Apples for $18
Campus Compost Heap: will buy 1000 kg of Anything for $1
CLB 7: will buy 3 kg of Apples for $43
./fruit_bot_world  <world1.txt
CSE: other
Western Watermelons: will sell 56 kg of Watermelons for $15
J17 G03: will buy 9 kg of Oranges for $69
CLB 7: will buy 45 kg of Apples for $31
Campus Citrus Centre: will sell 28 kg of Oranges for $35
Rosebery Orange Grove: will sell 34 kg of Oranges for $41
Smelly Fruits R Us: will sell 30 kg of Durian for $54
Bondi Banana Growers: will sell 7 kg of Bananas for $58
Physics Theatre: will buy 8 kg of Bananas for $89
Drone Servicing: will sell 100 kJ of Electricity for $4
Science Theatre: will buy 29 kg of Apples for $60
Kensington Apple Farm: will sell 31 kg of Apples for $17
Maroubra Melons: will sell 53 kg of Watermelons for $13
Sitar Lab: will buy 8 kg of Durian for $131
Clancy Auditorium: will buy 6 kg of Oranges for $60
Mathews B: will buy 22 kg of Watermelons for $34
Viola Lab: will buy 10 kg of Durian for $112
Wentworth Watermelons: will sell 62 kg of Watermelons for $17
Campus Charging: will sell 100 kJ of Electricity for $4
Randwick Apple Orchard: will sell 28 kg of Apples for $19
Campus Compost Heap: will buy 1000 kg of Anything for $1
Campus Computers: will sell 41 kg of Apples for $29
Paddington Banana Plantation: will sell 8 kg of Bananas for $64
Kingsford Oranges: will sell 29 kg of Oranges for $37
Physics Lawn: other
Quadrangle: other
Anzac Parade: other
Kora Lab: will buy 11 kg of Durian for $96
K17 Basement: will buy 14 kg of Bananas for $107
Mathews A: will buy 20 kg of Apples for $56

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest fruit_bot_world

Autotest Results

93% of 852 students who have autotested fruit_bot_world.c so far, passed all autotest tests.
  • 96% passed test 0
  • 96% passed test 1
  • 96% passed test 2
  • 96% passed test 3
  • 96% passed test 4
  • 96% passed test 5
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk11_fruit_bot_world fruit_bot_world.c
Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 22 May 23:59:59 to obtain the marks for this lab exercise.

Exercise: Go West Young Bot (pair)

This is a pair exercise to complete with your lab partner.
Download fruit_bot_go_west.c, the starting code for this exercise (or use the cp command below).

It is also the starting code for assignment 3 with a few things removed which we don't need for this lab exercise like unit tests.

Your task in this exercise is to write a bot that does this:

  1. If the bot is at a location that has electricity for sale, and bot's battery is not full. it should buy the number of kJ needed to fill its battery.

    Note you need to check that the location sells Electricity and that it has some kJ left to sell.

  2. If the bot is at a location that has apples for sale, and the bot has no fruit on board, it should buy as many kg of apples as it can carry.

    Note you need to check that the location sells Apples and that it has some left to sell.

  3. If the bot is at a location that buys apples, and the bot has apples on board, it should sell them all.

    Note you need to check that the location buys Apples and that it that still willing to buy some.

  4. Otherwise the bot should move west as many locations as it is permitted.
You don't need to check you have enough cash for the above operations. You will do this by changing this function:
void print_move(struct bot *b) {
    // ADD YOUR CODE HERE
}

to print the appropriate line.

14 test files containing world descriptions are available to help you test your bot (see the cp command below)

cp -n /web/cs1511/18s1/activities/fruit_bot_go_west/fruit_bot_go_west.c .
dcc fruit_bot_go_west.c fruit_bot_input.c -o fruit_bot_go_west 
cp /web/cs1511/18s1/activities/fruit_bot_go_west/westworld*.txt .
ls westworld*.txt
westworld0.txt   westworld11.txt  westworld2.txt  westworld5.txt  westworld8.txt
westworld1.txt   westworld12.txt  westworld3.txt  westworld6.txt  westworld9.txt
westworld10.txt  westworld13.txt  westworld4.txt  westworld7.txt
./fruit_bot_go_west <westworld0.txt
Move -10
./fruit_bot_go_west <westworld4.txt
Buy 13
./fruit_bot_go_west <westworld7.txt
Sell 10
When you have your bot passing autotests, try running it in a simulation with the fruit_bot_referee to see if it makes a profit. It will sometimes but you need to do more to get a b
1511 fruit_bot_referee fruit_bot_go_west.c
seeding with 7264617
dcc -o intensity fruit_bot_go_west.c -I/home/cs1511/public_html/18s1/private/activities/fruit_bot /home/cs1511/public_html/18s1/private/activities/fruit_bot/fruit_bot_input.c
dcc --valgrind -o intensity-valgrind fruit_bot_go_west.c -I/home/cs1511/public_html/18s1/private/activities/fruit_bot /home/cs1511/public_html/18s1/private/activities/fruit_bot/fruit_bot_input.c

*** Fruit Bot Parameters ***
battery_capacity=92
maximum_fruit_kg=22
maximum_move=8

*** Turn 1 of 30 *** ***

CSE: other
Kensington Apple Farm: will sell 2 kg of Apples for $28/kg
Mathews A: will buy 1 kg of Apples for $68/kg
CLB 7: will buy 1 kg of Apples for $99/kg
Quadrangle: other
Campus Charging: will sell 100 kJ of Electricity for $4/kJ
Campus Compost Heap: will buy 1000 kg of Anything for $1/kg

"Westy McWestWest" is at "CSE" with $55, battery level: 92: action = Move -8

*** Turn 2 of 30 *** ***

"Westy McWestWest" is at "Campus Compost Heap" with $55, battery level: 84: action = Move -8

*** Turn 3 of 30 *** ***

"Westy McWestWest" is at "Campus Charging" with $55, battery level: 76: action = Buy 16 (reduced to 13)

*** Turn 4 of 30 *** ***

...

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest fruit_bot_go_west

Autotest Results

90% of 790 students who have autotested fruit_bot_go_west.c so far, passed all autotest tests.
  • 97% passed test 0
  • 97% passed test 1
  • 96% passed test 10
  • 97% passed test 11
  • 96% passed test 12
  • 96% passed test 13
  • 94% passed test 2
  • 96% passed test 3
  • 97% passed test 4
  • 96% passed test 5
  • 95% passed test 6
  • 96% passed test 7
  • 96% passed test 8
  • 97% passed test 9
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk11_fruit_bot_go_west fruit_bot_go_west.c
Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 22 May 23:59:59 to obtain the marks for this lab exercise.

Exercise: Where Can I Charge? (pair)

This is a pair exercise to complete with your lab partner.
Download fruit_bot_charging.c, the starting code for this exercise or use the cp command below.

Your task in this exercise is to write a program that prints how many locations away the nearest available electricity is.

Note you need to check that the location sells Electricity and that it has some kJ left to sell.

  1. If the bot is at a location that has electricity for sale, it should print 0.
  2. If the nearest location eastwards. It should print a positive integer.
  3. If the nearest location is westwards. It should print a negative integer.
The main function in fruit_bot_charging.c has this code:
struct bot *b = fruit_bot_input(stdin);
int distance = nearest_electricity(me);
printf("Distance to nearest available electricity is %d\n", distance);
return 0;
You will change this function:
int nearest_electricity(struct bot *b) {
    // PUT YOUR CODE HERE
    return -42;
}

to return the appropriate value.

You are not permitted to change location or bot structs. You can not, for example, change to the east or west fields of locations, or the location field of the bot.

You can use the test files from the previous exercise.

cp -n /web/cs1511/18s1/activities/fruit_bot_charging/fruit_bot_charging.c .
dcc fruit_bot_charging.c fruit_bot_input.c -o fruit_bot_world 
./fruit_bot_world  <world0.txt
1511 fruit_bot_referee fruit_bot_go_west.c
Distance to nearest available electricity is 3
./fruit_bot_world  <world1.txt
Distance to nearest available electricity is 9

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest fruit_bot_charging

Autotest Results

96% of 748 students who have autotested fruit_bot_charging.c so far, passed all autotest tests.
  • 97% passed test 0
  • 97% passed test 1
  • 99% passed test 2
  • 97% passed test 3
  • 99% passed test 4
  • 97% passed test 5
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk11_fruit_bot_charging fruit_bot_charging.c
Note, even though this is a pair exercise, you both must run give from your own account before Tuesday 22 May 23:59:59 to obtain the marks for this lab exercise.

Challenge Exercise: Ordering Whales (individual)

This is an individual exercise to complete by yourself.
Download ordered_whales.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/ordered_whales/ordered_whales.c .

It already contains the functions which reads the file of whale sightings into a linked list of structs. You do not need need to change these functions.

The main function in ordered_whales.c has this code:

struct pod *first_pod = read_sightings_file(argv[1]);
struct pod *new_first_pod = order_whales(first_pod);
write_sightings(stdout, new_first_pod);
Your task in this exercise is to complete this function:
struct pod *order_whales(struct pod *first_pod) {
    // PUT YOUR CODE HERE
    return first_pod;  // CHANGE ME
}

Do not change any other function.

order_whales should rearrange the linked list of whale sightings and place them in increasing order.

It should return a pointer to the first whale sighting in the reordered list.

Ordering should be first be chronologically on date, then alphabetically on species, and finally numerically on number of whales seen.

You are only permitted to change the next field of sightings (struct pod). You can not change date, when or how_many fields,

You are not permitted to use arrays in this exercise.

You are not permitted to use malloc in this exercise, apart from its use in the supplied code.

You should add at least one new function.

You are not permitted to use functions from the C standard library in this exercise, apart from their use in the supplied code, with this exception:

You are permitted to use functions from <string.h> including strcmp.

When you have completed the function order_whales this is how ordered_whales should behave:

dcc -o ordered_whales ordered_whales.c
cp /web/cs1511/18s1/activities/ordered_whales/ten_whales.txt .
./ordered_whales ten_whales.txt
14/06/17 29 Southern right whale
20/06/17  3 Spinner dolphin
02/07/17  4 Common dolphin
22/07/17 34 Indo-Pacific humpbacked dolphin
01/09/17 21 Southern right whale
18/01/18  9 Pygmy right whale
16/02/18  4 Striped dolphin
19/02/18  4 Blue whale
21/02/18 38 Dwarf sperm whale
20/03/18  7 Long-finned pilot whale
cp /web/cs1511/18s1/activities/ordered_whales/pygmy_whales.txt .
./ordered_whales pygmy_whales.txt
29/04/17 18 Pygmy sperm whale
04/05/17 33 Pygmy right whale
08/05/17 21 Pygmy right whale
...
23/04/18 29 Pygmy right whale
24/04/18 21 Pygmy sperm whale
25/04/18 11 Pygmy sperm whale
./ordered_whales pygmy_whales.txt|head
29/04/17 18 Pygmy sperm whale
04/05/17 33 Pygmy right whale
08/05/17 21 Pygmy right whale
12/05/17 15 Pygmy sperm whale
15/05/17 18 Pygmy right whale
18/05/17  9 Pygmy right whale
19/05/17 28 Pygmy right whale
19/05/17 23 Pygmy sperm whale
25/05/17  2 Pygmy right whale
28/05/17 11 Pygmy right whale
./ordered_whales pygmy_whales.txt|tail
03/04/18 40 Pygmy sperm whale
04/04/18 29 Pygmy sperm whale
06/04/18 41 Pygmy sperm whale
10/04/18 11 Pygmy right whale
17/04/18 28 Pygmy right whale
18/04/18  1 Pygmy sperm whale
22/04/18  8 Pygmy sperm whale
23/04/18 29 Pygmy right whale
24/04/18 21 Pygmy sperm whale
25/04/18 11 Pygmy sperm whale
cp /web/cs1511/18s1/activities/ordered_whales/dolphins.txt .
./ordered_whales dolphins.txt
01/05/17 28 Indo-Pacific humpbacked dolphin
04/05/17 15 Common dolphin
04/05/17 33 Indo-Pacific humpbacked dolphin
...
22/04/18 13 Spinner dolphin
24/04/18 25 Striped dolphin
28/04/18 16 Coastal bottlenose dolphin

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest ordered_whales

Autotest Results

64% of 98 students who have autotested ordered_whales.c so far, passed all autotest tests.
  • 77% passed test 0
  • 76% passed test 1
  • 74% passed test 2
  • 72% passed test 3
  • 72% passed test 4
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk11_ordered_whales ordered_whales.c
You must run give before Tuesday 22 May 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Tuesday 22 May 23:59:59 to submit your work.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface

You can read more about lab assessment here