lab07
by
typing:
mkdir lab07Change to this directory by typing:
cd lab07
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 palindromeHint: 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.
1511 style palindrome.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest palindrome
give cs1511 lab07_palindrome palindrome.cNote, 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.
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:
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
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
give cs1511 lab07_pet_stats pet_stats.cNote, 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.
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.txtHint: 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:
1511 style orca.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest orca
give cs1511 lab07_orca orca.cNote, 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.
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
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
give cs1511 lab07_species_count species_count.cNote, 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.
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 palindromeHint: you might find C library functions in
#include <ctype.h>
useful.
You can assume lines contain at most 4096 characters.
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
give cs1511 lab07_punctuated_palindrome punctuated_palindrome.cYou 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.
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 whalesYou 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:
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
give cs1511 lab07_whale_summary whale_summary.cYou 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.
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
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 -sturecThe 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.