The first 10 minutes of the lab is set aside for you to complete the myExperience survey for COMP1511. Your tutors will leave the room to ensure your answers stay confidential.
The first two lab exercises must be completed and submitted in the exam environment.
If you don't get time to finish them in the exam environment, we will release the questions as normal lab exercises after all of this week's labs are completed.
Log out of your regular account after completing the myExperience survey.
Your tutors will log your lab machines into the exam environment. You will then log in using your normal username and password and will be taken to the same environment that your exam will be run in.
This section of the lab will not be run under full exam conditions or time limits but it will be a good chance for you to familiarise yourself with what software is available during the exam.
First enter answers to Part 1
You run "Enter Part 1 Answers" from the right mouse button menu to enter the answers to part 1.
In the final exam you will have 30 minutes to complete part 1 and during that time you are not permitted to run any code editors or terminals (xterms, shells, editors, etc). You can view online documentation.
The real exam will have many more part 1 questions!
Your answers for part 1 questions in today's practice exam will not be marked.
Two of this weeks lab exercises are the Part 2 questions for the practice exam.
You run "View Part 2 Questions" from the right mouse button menu to see part 2 questions.
Part 2 answers are entered into the file specified by each question and submitted using the "give" command inside the exam environment. The questions include submission instructions.
We are not enforcing exam conditions but try to do the questions under simulated exam conditions.
If you can't complete the questions, you can talk to your tutors and your lab partner.
Your two answers will be automarked in the usual way for lab exercises.
If you don't end up finishing the questions in the exam environment, they'll be made available as standard lab questions after the week's labs are over.
When you have finished the two part 2 questions log out of the exam environment and log into your account.
The remaining lab exercises are completed the usual way.
cp -n /web/cs1511/19T2/activities/count_small/count_small.c .Your task is to add code to this function in count_small.c:
// Return the number of "small" values in an array.
// A "small" value is between -9 and 9 inclusive.
int count_small(int length, int array[]) {
// PUT YOUR CODE HERE (you must change the next line!)
return 42;
}
Add code so that count_small returns the number of "small"
values in the array. Note: a "small" value is between -9 and +9
inclusive.
For example if the array contains these 9 elements:
16, 7, 8, 12, 13, -9, -3, 12, -9
Your function should return 5, because these 5 elements are small (between -9 and 9 inclusive):
7, 8, -9, -3, -9
Your count_small function will be called directly in marking. The main function is only to let you test your count_small function
count_small should return a single integer.
count_small should not change the array it is given.
count_small should not call scanf (or getchar or fgets).
count_small can assume the array contains at least one integer.
count_small function should not print anything. It should not call printf.
Your submitted file may contain a main function. It will not be tested or marked.
1511 style count_small.c accept count_small_exam.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest count_smallWhen you are finished working on this exercise you must submit your work by running give:
give cs1511 lab10_count_small count_small.c accept count_small_exam.cYou must run give before Monday 12 August 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/list_count_even/list_count_even.c .Your task is to add code to this function in list_count_even.c:
// return the number of even values in a linked list
int count_even(struct node *head) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
Note list_count_even.c uses the following familiar data type:
struct node { struct node *next; int data; };count_even is given one argument, head, which is the pointer to the first node in a linked list.
Add code to count_even so that its returns the number of even values in the linked list.
For example if the linked list contains these 8 elements:
16, 7, 8, 12, 13, 19, 21, 12
count_even should return 4, because these 4 elements are even:
16, 8, 12, 12
This main function:
Do not change this main function. If you want to change it, you have misread the question.
Your count_even function will be called directly in marking. The main function is only to let you test your count_even function
Here is how you use main function allows you to test count_even:
cp -n /web/cs1511/19T2/activities/list_count_even/list_count_even.c . dcc list_count_even.c -o list_count_even ./list_count_even 16 7 8 12 13 19 21 12 4 ./list_count_even 2 4 6 2 4 6 6 ./list_count_even 3 5 7 11 13 15 17 19 23 29 0 ./list_count_even 2 4 8 16 32 64 128 256 8 ./list_count_even 0
count_even should return a single integer.
count_even should not change the linked list it is given. Your function should not change the next or data fields of list nodes.
count_even should not use arrays.
count_even should not call malloc.
count_even should not call scanf (or getchar or fgets).
You can assume the linked list only contains positive integers.
count_even should not print anything. It should not call printf.
Do not change the supplied main function. It will not be tested or marked.
1511 style list_count_even.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest list_count_even
give cs1511 lab10_list_count_even list_count_even.cYou must run give before Monday 12 August 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/list_count_matches/list_count_matches.c .Your task is to add code to this function in list_count_matches.c:
// Return the number of matches in the two lists, i.e. the number of
// values which occur at the same position in both linked lists.
int count_matches(struct node *head1, struct node *head2) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
Note list_count_matches.c uses the following familiar data
type:
struct node { int data; struct node *next; };Your task is to add code to this function:
// Return the number of matches in the two lists, i.e. the number of
// values which occur at the same position in both linked lists.
int count_matches(struct node *head1, struct node *head2) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
count_matches is given two arguments, head1 and
head2, which are pointers to the first node of linked lists.
Add code to count_matches so that returns a count of how many places the two lists have the same value in the same position.
For example, if the two lists contain these values:
1, 4, 1, 5, 9, 2, 1, 8 1, 1, 8, 2, 9, 5
count_matches should return 2 because both lists have the same value (1) at position 0 and the same value (9) at position 4.
Note: the lists may be any length and the two lengths may be unequal.
This main function:
Do not change this main function. If you want to change it, you have misread the question.
Your count_matches function will be called directly in marking. The main function is only to let you test your count_matches function
Here is how the main function allows you to test count_matches:
dcc -o list_count_matches list_count_matches.c ./list_count_matches 3 1 4 - 2 7 1 8 3 0 ./list_count_matches 1 2 3 4 - 2 1 3 8 1 ./list_count_matches 5 5 6 5 - 6 5 5 5 2 ./list_count_matches 3 5 7 - 3 5 19 7 23 29 2 ./list_count_matches 1 2 3 4 5 6 - 3 2 1 1 ./list_count_matches - 1 2 3 4 0 ./list_count_matches 4 3 2 1 - 0 ./list_count_matches - 0
The linked lists may be of unequal lengths.
The linked lists may be any length.
Either or both linked lists may be empty (contain no elements).
count_matches should not change the linked lists it is given. Your function should not change the next or data fields of list nodes.
count_matches should not use arrays.
count_matches should not call malloc.
count_matches should not call scanf (or getchar or fgets).
count_matches should not print anything. It should not call printf.
Do not change the supplied main function. It will not be tested or marked.
1511 style list_count_matches.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest list_count_matches
give cs1511 lab10_list_count_matches list_count_matches.cYou must run give before Monday 12 August 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/padding_left/padding_left.c .Your task is to add code to this function in padding_left.c:
// Given a list of characters, move each character
// along one, putting "pad_character" in the first place,
// and ignoring the last character.
//
// Given the list 'c' 'e' 'l' 'l' 'o'; calling left_pad
// once with pad_character = 'x' results in the list:
// 'x' 'c' 'e' 'l' 'l'. Calling it again with pad_character = 'e'
// results in the list: 'e' 'x' 'c' 'e' 'l'
//
// Note that you can't malloc, or modify the lists' nodes
// You can only move data around the list.
void pad_left(struct character_node *characters, char pad_character) {
// TODO: COMPLETE THIS FUNCTION
}
padding_left is written using the following structs that cannot be changed:
// A node in a linked list of characters. struct character_node { char data; struct character_node *next; };
The character_node struct holds a character, as part of a linked list of characters.
pad_left is given a pointer to a character_node, which is the first element in a list of characters. It is also given a character to "pad" with.
When you "pad" a character list, you iterate over the list and "push" every character along one. This means that the second character becomes whatever the first character was. The third becomes whatever the second was, and so on. The first character becomes pad_character, and what was previously the last character is "forgotten".
For example if a list of characters called characters looks like this:
abcdef
Then the following function is called:
pad_left(characters, 't');
The list characters would be:
tabcde
dcc .c -o padding_left padding_left ./padding_left lights a alight m maligh > >malig > >>mali
dcc .c -o padding_left padding_left ./padding_left cello x xcell e excel
struct character_node cannot be edited. It must be used as is.
The string_to_characters, print_characters, and free_characters functions will help you test and run your program. They cannot be edited and must be used as it is. You should not use them yourself in pad_left
pad_left cannot return a new head, so you cannot add to the head of the list. You should complete this task solely by moving around data - you will not need to malloc yourself.
Your submitted file may contain a main function. It will not be tested or marked.
1511 style padding_left.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest padding_left
give cs1511 lab10_padding_left padding_left.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 12 August 17:00 to obtain the marks for this lab exercise.
Write a C program knight_moves.c which prints sequences of moves which takes a knight from a specified square on a chessboard to another specified square on the chessboard.
A chessboard is an 8x8 square matrix. We label each square as below:
a8 b8 c8 d8 e8 f8 g8 h8 a7 b7 c7 d7 e7 f7 g7 h7 a6 b6 c6 d6 e6 f6 g6 h6 a5 b5 c5 d5 e5 f5 g5 h5 a4 b4 c4 d4 e4 f4 g4 h4 a3 b3 c3 d3 e3 f3 g3 h3 a2 b2 c2 d2 e2 f2 g2 h2 a1 b1 c1 d1 e1 f1 g1 h1
A knight makes an L-shaped move. It moves either two squares horizontally and one square vertically or two squares vertically and one square horizontally.
For example, a knight at d4 can move to one of eight squares: c2, e2, b3, b5, c6, e6, f3 or f5.
A move can not take a knight off the chessboard. Hence, a knight on square near the edge of the board will have fewer possible moves.
For example, a knight at a1 can move only to c2 and b3.
Write a C program which takes two arguments: a starting square and and a finishing square.
It should print a sequence of moves which take a knight from the starting square to the finishing square. This sequence of moves should be as short as possible.
In many cases there are multiple shortest sequences, your program must print all of the sequences in alphabetic order
For example:
dcc -o knight_moves knight_moves.c ./knight_moves d4 b3 d4 b3 ./knight_moves d4 a1 d4 b3 a1 d4 c2 a1 ./knight_moves g2 b2 g2 e1 d3 b2 g2 e3 c4 b2 g2 e3 d1 b2 g2 f4 d3 b2
Assumptions/Restrictions/Clarifications
Your program must complete in 60 seconds when executed with dcc --valgrind
Your can assume there are two command-line arguments and they correctly identify squares on the board.
You can assume the starting and finishing squares are different.
No error checking required
1511 style knight_moves.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest knight_moves
give cs1511 lab10_knight_moves knight_moves.cYou must run give before Monday 12 August 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 12 August 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.