Week 10 Laboratory Exercises

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 answers are confidential and you should not show them to your tutor.

Take-Home Exam Practice

The first three lab exercises must be completed and submitted using the instructions for submission of the exam (the questions themselves will have these instructions).

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 how the questions will be structured as well as how auto testing and submission works.

Remember that in the exam you are allowed to look up resources, but you are not allowed to communicate with anyone (inside or outside the course) about the exam. If you like, you can try these exercises with those restrictions so that you have some familiarity with the format.

Practice Exam Part 1

Part 1 of the exam is a series of short questions that don't involve you writing code, but instead you interpreting code or understanding some key concepts.

You will be answering part 1 by editing a text file that you can test and submit. The test will only tell you if your answers are understood, not if they're correct.

The real exam will have more part 1 questions!

Your answers for part 1 questions in today's practice exam will not be marked.

Practice Exam Part 2

The first three of this week's lab exercises are the Part 2 questions for the practice exam.

Part 2 answers are entered into the file specified by each question and submitted using the "give" command. 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.

Your answers will be automarked in the usual way for lab exercises.

Accessing the Prac Exam Environment

To access the Prac Exam, go to: https://cgi.cse.unsw.edu.au/~cs1511/20T1/pracexam/papers/.

You will be asked to enter your zID and zPass. You will then be taken to your personal exam paper.

In the final exam, this paper will be personalized to you.

Returning to normal lab work

The remaining lab exercises are completed the usual way.

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

Your task is to add code to this function:
// Return the number of "small" values in an array.
// A "small" value is greater than -10 and less than +10.
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

q1.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 q1.c

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

1511 autotest exam_array_count_small

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab10_exam_array_count_small q1.c

You must run give before Friday 26 April 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

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

Note q2.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

q2.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/20T1/activities/exam_list_count_even/q2.c .
dcc q2.c -o q2
./q2 16 7 8 12 13 19 21 12
4
./q2 2 4 6 2 4 6
6
./q2 3 5 7 11 13 15 17 19 23 29
0
./q2 2 4 8 16 32 64 128 256
8
./q2
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 q2.c

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

1511 autotest exam_list_count_even

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab10_exam_list_count_even q2.c

You must run give before Friday 26 April 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

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

Note q4.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

q4.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 q4 q4.c
./q4 3 1 4 - 2 7 1 8 3
0
./q4 1 2 3 4 - 2 1 3 8
1
./q4 5 5 6 5 - 6 5 5 5
2
./q4 3 5 7 - 3 5 19 7 23 29
2
./q4 1 2 3 4 5 6 - 3 2 1
1
./q4 - 1 2 3 4
0
./q4 4 3 2 1 -
0
./q4 -
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 q4.c

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

1511 autotest exam_list_count_matches

When you are finished working on this exercise, you must submit your work by running give:

give cs1511 lab10_exam_list_count_matches q4.c

You must run give before Friday 26 April 20:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — in pairs:
Padding (moving all elements) a character list

Download padding_left.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/20T1/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 padding_left.c -o padding_left
./padding_left lights
a
alight
m
maligh
>
>malig
>
>>mali

dcc padding_left.c -o 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

When you are finished working 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 Friday 26 April 20:00 to obtain the marks for this lab exercise.

Challenge Exercise — individual:
Hard Challenge - Knight Moves, Final Question from a previous exam

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

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 Friday 26 April 20:00 to obtain the marks for this lab exercise. Note that 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 Week 10 Sunday 20: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