COMP1511 19T2
COMP1511 19T2

Objectives

  • reading a line of input
  • introduction of pointers
  • introduction of structs

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started

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

Exercise: Is it a Palindrome? (pair)

This is a pair exercise to complete with your lab partner.
A palindrome is a sequence which is the same forwards as backwards.

Write a program, palindrome.c, which reads a string and tests if it is a palindrome.

For example:

./palindrome
Enter a string: kayak
String is a palindrome
./palindrome
Enter a string: canoe
String is not a palindrome
./palindrome
Enter a string: if if fi fi
String is a palindrome
./palindrome
Enter a string: if if if fi
String is not a palindrome
Hint: don't use scanf, use fgets to read the string.

Note, your program needs to read only one line - it doesn't have to read until the end of input.

You can assume the line contains at most 4096 characters.

New! You can run an automated code style checker using the following command:
1511 style palindrome.c

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

1511 autotest palindrome

Autotest Results

94% of 431 students who have autotested palindrome.c so far, passed all autotest tests.
  • 97% passed test 0
  • 97% passed test 1
  • 97% passed test 10
  • 96% passed test 11
  • 97% passed test 2
  • 97% passed test 3
  • 97% passed test 4
  • 97% passed test 5
  • 97% passed test 6
  • 97% 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 lab07_palindrome palindrome.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 22 July 17:00 to obtain the marks for this lab exercise.

Exercise: Pet Statistics (pair)

This is a pair exercise to complete with your lab partner.
In this exercise, you will read some pet information from standard input and relay it in a human readable format. Download pet_stats.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/pet_stats/pet_stats.c .

pet_stats.c already contains a struct for a pet and a main function. You cannot change either of these and can only make changes to the functions: setup_pet() and print_pet().

Your task in this exercise is first to complete the function setup_pet() so that it:

  • Reads in a line from standard input and stores it in the name field
  • Reads in a line from standard input and stores it in the type field
  • Reads in an int from standard input and stores it in the age field
  • Reads in an int from standard input and stores it in the weight feild

Hint: don't use scanf, use fgets to read the name and type strings.

You can assume each line contains at most 50 characters.

You will then need to complete the function print_pet() so that it writes out a single line of text describing the pet, like: "Mr Snuffle-uffle-kins is a cat who is 4 years old and weighs 6kg\n"

When you have completed the functions setup_pet() and print_pet() this is how pet_stats.c should behave:

dcc -o pet_stats pet_stats.c
./pet_stats 
Mr Snuffle-uffle-kins
cat
4
6
Mr Snuffle-uffle-kins is a cat who is 4 years old and weighs 6kg
./pet_stats 
007
British Short-hair
35 78
007 is a British Short-hair who is 35 years old and weighs 78kg
New! You can run an automated code style checker using the following command:
1511 style pet_stats.c

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

1511 autotest pet_stats

Autotest Results

96% of 396 students who have autotested pet_stats.c so far, passed all autotest tests.
  • 96% passed test 0
  • 96% passed test 1
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab07_pet_stats pet_stats.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 22 July 17:00 to obtain the marks for this lab exercise.

Exercise: How many Orca Pods (pair)

This is a pair exercise to complete with your lab partner.
A citizen science project monitoring whale populations has files of containing large numbers of whale observations. Each line in these files contains:
  • the date the observation was made
  • the number of whales in the pod
  • the species of whales
Here is a file of example data which you can download to test your program. The first 10 lines are:
head whales.txt
18/01/18  9 Pygmy right whale
01/09/17 21 Southern right whale
16/02/18  4 Striped dolphin
02/07/17  4 Common dolphin
19/02/18  4 Blue whale
21/02/18 38 Dwarf sperm whale
14/06/17 29 Southern right whale
20/06/17  3 Spinner dolphin
22/07/17 34 Indo-Pacific humpbacked dolphin
20/03/18  7 Long-finned pilot whale
...
Download orca.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/orca/orca.c .
Your task is to add code to this function in orca.c:
//
// return  the number of Orca pods in sightings
//
int count_orca_sightings(int n_sightings, struct pod sightings[n_sightings]) {
    // REPLACE THIS COMMENT WITH YOUR CODE
    // THIS FUNCTION SHOULD NOT CALL SCANF OR PRINTF
    // IT SHOULD JUST RETURN A VALUE
    return 42; // CHANGE ME
}

orca.c already contains the functions which you discussed in your tutorial which read the file of whale sightings into an array of structs. You do not need need to change these functions.

The main function in orca.c has this code:

int n_orca_pods = count_orca_sightings(n_sightings, whale_sightings);
printf("%d Orca sightings in %s\n", n_orca_pods, argv[1]);
Your task in this exercise is only to complete count_orca_sightings

Do not change any other function.

It should return a count of the number of sightings of Orca pods in the file.

Note, "pod" is the collective noun for a group of whales.

count_orca_sightings should return the number of Orca pods. It does not have have to sum the number of individual whales in these pods.

When you have completed the function count_orca_sightings this is how orca.c should behave:

dcc -o orca orca.c
./orca whales.txt
53 Orca sightings in whales.txt
Hint: hint use strcmp from string.h

Hint: most of the work for this exercise is figuring out what to do - only a short loop containing an if statement is needed.

Hint: if you are having trouble understanding structs COMP1511 tutors Dean Wunder and Ben Pieters-Hawke take you through the basics in this video:

New! You can run an automated code style checker using the following command:
1511 style orca.c

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

1511 autotest orca

Autotest Results

97% of 377 students who have autotested orca.c so far, passed all autotest tests.
  • 97% passed test 0
  • 97% passed test 1
  • 97% passed test 2
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab07_orca orca.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 22 July 17:00 to obtain the marks for this lab exercise.

Exercise: Counting A Whale Species (pair)

This is a pair exercise to complete with your lab partner.
Download species_count.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/species_count/species_count.c .
Your task is to add code to this function in species_count.c:
//
// number of pods of the specified species assigned to *n_pods
// total number of whales of the specified species assigned to *n_whales
//
void species_count(char species[], int n_sightings, struct pod sightings[n_sightings], int *n_pods, int *n_whales) {
    // REPLACE THIS COMMENT WITH YOUR CODE
    // THIS FUNCTION SHOULD NOT CALL SCANF OR PRINTF
    // IT SHOULD JUST ASSIGN VALUES to N_PODS AND N_WHALES
    *n_pods = 24; // CHANGE ME
    *n_whales = 42; // CHANGE ME
}

species_count.c already contains the functions which you discussed in your tutorial which reads the file of whale sightings into an array of structs. You do not need need to change these functions.

The main function in species_count.c has this code:

int pod_count;
int whale_count;
species_count(species, n_sightings, whale_sightings, &pod_count, &whale_count);
printf("%d %s pods containing %d whales in %s\n", pod_count, species, whale_count, filename);
Your task in this exercise is to complete species_count.

Do not change any other function.

species_count should assign the number of pods of the given species to *n_pods and the total number of whales of the given species to *n_whales

When you have completed the function species_count this is how species_count.c should behave:

dcc -o species_count species_count.c
./species_count whales.txt "Blue whale"
51 Blue whale pods containing 1171 whales in whales.txt
./species_count whales.txt "Indo-Pacific humpbacked dolphin"
43 Indo-Pacific humpbacked dolphin pods containing 897 whales in whales.txt
New! You can run an automated code style checker using the following command:
1511 style species_count.c

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

1511 autotest species_count

Autotest Results

99% of 365 students who have autotested species_count.c so far, passed all autotest tests.
  • 99% passed test 0
  • 99% passed test 1
  • 100% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
  • 99% passed test 7
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab07_species_count species_count.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 22 July 17:00 to obtain the marks for this lab exercise.

Challenge Exercise: Is it a Palindrome - the Sequel (individual)

This is an individual exercise to complete by yourself.
Write a program, punctuated_palindrome.c, which reads a string and tests if it is a palindrome.

Characters which are not letters should be ignored .

Differences between upper case and lower case are ignored. For example:

./punctuated_palindrome
Enter a string: Do geese see God?
String is a palindrome
./punctuated_palindrome
Enter a string: Do ducks see God?
String is not a palindrome
./punctuated_palindrome
Enter a string: Madam, I'm Adam
String is a palindrome
./punctuated_palindrome
Enter a string: Madam, I'm Andrew
String is not a palindrome
Hint: you might find C library functions in #include <ctype.h> useful.

You can assume lines contain at most 4096 characters.

New! You can run an automated code style checker using the following command:
1511 style punctuated_palindrome.c

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

1511 autotest punctuated_palindrome

Autotest Results

85% of 93 students who have autotested punctuated_palindrome.c so far, passed all autotest tests.
  • 86% passed test 0
  • 90% passed test 1
  • 85% passed test 2
  • 90% passed test 3
  • 85% passed test 4
  • 90% passed test 5
  • 85% passed test 6
  • 90% passed test 7
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab07_punctuated_palindrome punctuated_palindrome.c
You must run give before Monday 22 July 17:00 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.

Challenge Exercise: Printing A summary of All Whale Species (individual)

This is an individual exercise to complete by yourself.
Download whale_summary.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/whale_summary/whale_summary.c .
Your task is to add code to this function in whale_summary.c:
//
// print a summary of all whale sightings
//
void whale_summary(int n_sightings, struct pod sightings[n_sightings]) {

    // PUT YOUR CODE HERE

}

The main function in whale_summary.chas this code:

whale_summary(n_sightings, whale_sightings);
Your task in this exercise is to complete whale_summary

Do not change any other function.

whale_summary should print for every whale species how many pods were seen and how many total whales were seen of that species.

Match the example output form EXACTLY, except you may print the lines in any order.

When you have completed the function whale_summary this is how whale_summary.c should behave:

dcc -o whale_summary whale_summary.c
./whale_summary whales.txt
59 Pygmy right whale pods containing 1260 whales
55 Southern right whale pods containing 1252 whales
68 Striped dolphin pods containing 1337 whales
62 Common dolphin pods containing 1232 whales
51 Blue whale pods containing 1171 whales
58 Dwarf sperm whale pods containing 1272 whales
52 Spinner dolphin pods containing 1118 whales
43 Indo-Pacific humpbacked dolphin pods containing 897 whales
50 Long-finned pilot whale pods containing 996 whales
68 Short-finned pilot whale pods containing 1344 whales
49 Dwarf minke whale pods containing 1080 whales
53 Orca pods containing 1245 whales
57 Pygmy sperm whale pods containing 1346 whales
55 Humpback whale pods containing 1071 whales
65 Fin whale pods containing 1251 whales
53 Coastal bottlenose dolphin pods containing 1169 whales
55 Sei whale pods containing 929 whales
47 Bryde's whale pods containing 1023 whales
You may assume there are at most 256 different whale species.

If autotest gives you an uninitialized variable error, you can reproduce this by compiling and running with dcc --valgrind, e.g.:

dcc --valgrind -o whale_summary whale_summary.c
./whale_summary whales.txt
Runtime error: uninitialized variable accessed.

Execution stopped here in whale_summary(n_sightings=1000, sightings=0x1ffee9fbf0) in whale_summary.c at line 113:
....
Beware the error may reported later than you expect.

Look back through your code for a variable, such as an array element, that has not been assigned an initial value before it is used.

Hint: if you are having trouble debugging your code watch COMP1511 tutor Peter Kydd explain how to add debugging printfs:

Hint: if you are having trouble with an uninitialized variable errors maybe Peter's explanation here will help:

New! You can run an automated code style checker using the following command:
1511 style whale_summary.c

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

1511 autotest whale_summary

Autotest Results

76% of 38 students who have autotested whale_summary.c so far, passed all autotest tests.
  • 76% passed test 0
  • 76% passed test 1
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab07_whale_summary whale_summary.c
You must run give before Monday 22 July 17:00 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 Monday 22 July 17:00 to submit your work.

You cannot obtain marks by e-mailing lab work to tutors or lecturers.

You check the files you have submitted here

Automarking will be run by the lecturer several days after the submission deadline for the test, using test cases that you haven't seen: different to the test cases autotest runs for you.

(Hint: do your own testing as well as running autotest)

After automarking is run by the lecturer you can view it here the resulting mark will also be available via via give's web interface

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec
The lab exercises for each week are worth in total 2 marks.

The best 8 of your 9 lab marks for weeks 2-10 will be summed to give you a mark out of 13. If their sum exceeds 13 - your mark will be capped at 13.

  • You can obtain full marks for the labs without completing challenge exercises
  • You can miss 1 lab without affecting your mark.