COMP1511 19T2
COMP1511 19T2

Objectives

  • Become familiar with the environment used for the final exam
  • practice coding under exam conditions

Getting Started

MyExperience

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.

Exam Environment Practice

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.

Logging Into the Exam environment

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.

Practice Exam Part 1

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.

Practice Exam Part 2

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.

Logging Out Of The Exam environment

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.

Exercise: Practice Exam Q1 - Count the number of small values in an array (individual)

This is an individual exercise to complete by yourself.
Download count_small.c here, or copy it to your CSE account using the following command:
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

Testing

count_small.c also contains a simple main function which allows you to test your count_small function.

Your count_small function will be called directly in marking. The main function is only to let you test your count_small function

Assumptions/Restrictions/Clarifications.

An small number is > -10 and < 10

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.

New! You can run an automated code style checker using the following command:
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_small
When 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.c
You 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.

Exercise: Practice Exam Q2 - Count the number of even numbers in a linked list (individual)

This is an individual exercise to complete by yourself.
Download list_count_even.c here, or copy it to your CSE account using the following command:
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

Testing

list_count_even.c also contains a main function which allows you to test your count_even function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls count_even(head)
  • prints the result.

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

Assumptions/Restrictions/Clarifications.

An even number is divisible by 2.

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.

New! You can run an automated code style checker using the following command:
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

Autotest Results

98% of 171 students who have autotested list_count_even.c so far, passed all autotest tests.
  • 98% passed test 1
  • 98% passed test 10
  • 99% passed test 2
  • 98% passed test 3
  • 98% passed test 4
  • 99% passed test 5
  • 98% passed test 6
  • 98% passed test 7
  • 98% passed test 8
  • 98% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab10_list_count_even list_count_even.c
You 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.

Exercise: Practice Exam Q4 - Count the number of times the same number is in the same position in a pair of linked lists (individual)

This is an individual exercise to complete by yourself.
Download list_count_matches.c here, or copy it to your CSE account using the following command:
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.

Testing

list_count_matches.c also contains a main function which allows you to test your count_matches function.

This main function:

  • uses a command line argument of "-" to separate the values for two linked lists.
  • converts the command-line arguments before the "-" to a linked list
  • assigns a pointer to the first node in the linked list to head1
  • converts the command-line arguments after the "-" to a linked list
  • assigns a pointer to the first node in the linked list to head2
  • calls count_matches(head1, head2)
  • prints the result.

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

Assumptions/Restrictions/Clarifications.

count_matches should return a single integer.

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.

New! You can run an automated code style checker using the following command:
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

Autotest Results

96% of 165 students who have autotested list_count_matches.c so far, passed all autotest tests.
  • 99% passed test 1
  • 96% passed test 10
  • 99% passed test 11
  • 99% passed test 12
  • 96% passed test 13
  • 99% passed test 14
  • 96% passed test 15
  • 96% passed test 16
  • 99% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
  • 99% passed test 7
  • 99% passed test 8
  • 96% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab10_list_count_matches list_count_matches.c
You 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.

Exercise: Padding (moving all elements) a character list (pair)

This is a pair exercise to complete with your lab partner.
Download padding_left.c here, or copy it to your CSE account using the following command:
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
Hint: You will need to use multiple temporary character variables to complete this exercise!

Examples

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

Assumptions/Restrictions/Clarifications.

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.

New! You can run an automated code style checker using the following command:
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

Autotest Results

89% of 311 students who have autotested padding_left.c so far, passed all autotest tests.
  • 92% passed test 0
  • 93% passed test 1
  • 91% passed test 2
  • 91% passed test 3
  • 91% passed test 4
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab10_padding_left padding_left.c
Note, 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.

Challenge Exercise: Hard Challenge - Knight Moves, the hardest question from a previous exam (individual)

This is an individual exercise to complete by yourself.
This question was taken from a past exam paper. It is an example of the hardest question at the end of the paper and it is usually expected that less than 5% of students can complete this question within the time constraints.

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

New! You can run an automated code style checker using the following command:
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

Autotest Results

79% of 14 students who have autotested knight_moves.c so far, passed all autotest tests.
  • 93% passed test 0
  • 93% passed test 1
  • 79% passed test 2
  • 79% passed test 3
  • 79% passed test 4
  • 79% passed test 5
  • 79% passed test 6
  • 79% passed test 7
  • 79% passed test 8
  • 79% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab10_knight_moves knight_moves.c
You 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.

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 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

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.