The distribution of marks is as follows:
You should keep this paper confidential. Sharing it, DURING or AFTER the exam is prohibited.
man pages) during the exam.The course has ONE hurdle in the final exam that you must meet to pass the course:
Please note, you are NOT required to pass the Linked list hurdle to pass COMP1911.
The course has TWO hurdles in the final exam that you must meet to pass the course:
submit command: submit prac_qX (where X is the question number)..c file. Multi-file programs will not be accepted in submission.autotest prac_qXsystem()By sitting or submitting an assessment on the scheduled assessment date, a student is declaring that they are fit to do so and cannot later apply for Special Consideration.
If, during an exam you feel unwell to the point that you cannot continue with the exam, you should raise you hand and inform an invigilator, who will provide advice as to the best course of action.
If you experience a technical issue, you should raise your hand and wait for an invigilator to assist.
1511 fetch-prac prac_q1
prac_q1.c uses the following
familiar data type:
struct node {
struct node *next;
int data;
};
Your task is to add code to the function
count_in_range.
count_in_range is given one argument,
head, which is a pointer to the first node of a linked list.
Add code to count_in_range so that it returns
the count of how many nodes in the list have a value
strictly between the first and the last nodes in the list.
For example, if the linked list contains these values:
2 -> 10 -> 5 -> 2 -> 7 -> 12 -> X
count_in_range should return 3,
because:
For example, if the linked list contains these values:
12 -> 10 -> 11 -> 13 -> 14 -> X
count_in_range should return 1,
because:
If the list has fewer than three nodes, the function should return 0, as there are no nodes in a range. If the list is empty, it should return 0.
prac_q1.c also contains a
main function which allows you to test your
count_in_range function.
This main function:
head.
count_in_range(head).Do not change this main function. If you want to change it, you have misread the question.
Your count_in_range function will be called
directly in marking. The main function is only to let you test your
count_in_range function.
Here is how the main function allows you to test
count_in_range:
dcc -o prac_q1 prac_q1.c ./prac_q1 2 10 5 2 7 12 3 ./prac_q1 10 2 5 3 0 ./prac_q1 5 7 10 1 ./prac_q1 1 10 1 10 1 0 ./prac_q1 10 1 10 1 10 0 ./prac_q1 5 10 0 ./prac_q1 7 0 ./prac_q1 0
count_in_range should return a single
integer.
count_in_range should return
0.
count_in_range should not change the linked
list it is given.
count_in_range should not use arrays.count_in_range should not call malloc.count_in_range should not call scanf (or
getchar or fgets).
count_in_range should not print anything. It
should not call printf.
main function. It
will not be tested or marked.
1511 autotest-prac prac_q1
1511 fetch-prac prac_q2
sum_after_even, given a two-dimensional array of integers, should
return a total sum calculated based on the position of the
first even number in each row.
sum_after_even will be passed a two-dimensional array with:
num_rows rows and,NUM_COLS (#define'd to 5) columns.For each row, your function must:
For example, if the 2D array contains these elements:
[1, 3, 2, 5, 7], [1, 1, 1, 1, 1]
Your function should return 12:
For example, if the 2D array contains these elements:
[4, 1, 1, 1, 1], [3, 5, 7, 9, 2]
Your function should return 4:
sum_after_even should return a single
integer.
sum_after_even should not change the array it
is given.
sum_after_even should not call scanf (or
getchar or fgets).
sum_after_even can assume the array contains
at least one row.
sum_after_even function should not print
anything. It should not call printf.
1511 autotest-prac prac_q2
1511 fetch-prac prac_q3
prac_q3.c uses the following
familiar data type:
struct node {
int data;
struct node *next;
};
The function insert_between_diff is given two
arguments, value and
head, where
head is a pointer to the first node in a
linked list.
Add code to insert_between_diff to create a new node (using
malloc) containing value and insert it after the
first occurrence of two consecutive nodes where the absolute difference
between their data is 6 or 7.
insert_between_diff should return a pointer to the head of the
list.
In the case that the list is empty, insert_between_diff should
return a pointer to the new node containing value.
If no two consecutive nodes have a difference of 6 or 7, the new node
containing value should be inserted at the tail of the
list.
For example, if value is 99 and the linked list contains
these 4 elements:
1 -> 8 -> 2 -> 3 -> X
The first occurrence where the difference is 6 or 7 is between 1 and 8 (∣1 − 8∣ = 7). The value 99 is inserted between them, resulting in:
1 -> 99 -> 8 -> 2 -> 3 -> X
If the value is 19 and the linked list contains these 4
elements:
1 -> 2 -> 1 -> 2 -> X
There are no consecutive nodes with a difference of 6 or 7, so the 19 is inserted at the tail of the list, resulting in:
1 -> 2 -> 1 -> 2 -> 19 -> X
If the value is 8 and the linked list contains these 4
elements:
1 -> 3 -> 9 -> 16 -> X
The first occurrence where the difference is 6 or 7 is between 3 and 9 (∣3 − 9∣ = 6). The value 8 is inserted between them, resulting in:
1 -> 3 -> 8 -> 9 -> 16 -> X
prac_q3.c also contains a
main function which allows you to test your
insert_between_diff function.
This main function:
head.
value.
insert_between_diff(value, head).Do not change this main function. If you want to change it, you have misread the question.
Your insert_between_diff function will be called directly in
marking. The main function is only to let you test your
insert_between_diff function.
dcc prac_q3.c -o prac_q3 ./prac_q3 1 8 2 3 99 [1, 99, 8, 2, 3] ./prac_q3 10 6 1 8 9 [10, 6, 1, 9, 8] ./prac_q3 1 2 1 2 19 [1, 2, 1, 2, 19] ./prac_q3 5 3 [5, 3]
insert_between_diff should not use arrays.insert_between_diff should not call scanf (or
getchar or fgets).
insert_between_diff should not print anything. It should not
call printf.
abs() function from stdlib.h to
calculate differences.
main function. It will not be tested
or marked.
1511 autotest-prac prac_q3
1511 fetch-prac prac_q4
Your first_sorted_row function should determine if there exists a
row in a 2D array where the elements are in strictly increasing order.
first_sorted_row will be passed:
num_rows: the number of rows in the 2D arrayarray: the two-dimensional array of integers (with
NUM_COLS columns)
Add code so that the first_sorted_row function searches for the
first row where every element is strictly greater than the element to
its left. If such a row exists, return the index of that row. If no
such row exists, return -1.
For example, if the 2D array has 4 rows and contains the following elements:
[10, 5, 8, 2, 1], [ 1, 2, 3, 4, 5], [ 2, 4, 6, 8, 10], [ 5, 5, 5, 5, 5],
the function should return 1, because row 1 (1, 2, 3, 4, 5) is the first row where each element is strictly increasing.
For example, if the 2D array has 3 rows and contains the following elements:
[1, 2, 2, 3, 4], [5, 4, 3, 2, 1], [9, 8, 7, 6, 5],
the function should return -1, because no row is strictly increasing. Note that row 0 is not strictly increasing because 2 is not strictly greater than 2.
For example, if the 2D array has 2 rows and contains the following elements:
[10, 20, 30, 40, 50], [ 1, 2, 3, 4, 5],
the function should return 0, because the very first row is strictly increasing.
prac_q4.c also contains a simple
main function which allows you to test your
first_sorted_row function.
Your first_sorted_row function will be called directly in
marking. The main function is only to let you test your
first_sorted_row
function.
first_sorted_row should return a single
integer (the index or -1).
first_sorted_row should not change the array
it is given.
first_sorted_row should not call
scanf (or getchar or fgets).
first_sorted_row should not print anything.
It should not call printf.
num_rows will always be
at least 1.
NUM_COLS.1511 autotest-prac prac_q4
1511 fetch-prac prac_q5
You have been provided with a file called
prac_q5.c.
The code in prac_q5.c is meant to do the
following:
struct bus.
Unfortunately, there are some problems with the provided program. Once fixed, your program should match the following examples exactly:
dcc prac_q5.c -o prac_q5 ./prac_q5 Enter 3 buses and their information: Bus 1: Bus number: 370 Maximum capacity: 80 Current number of passengers: 35 Bus 2: Bus number: 380 Maximum capacity: 50 Current number of passengers: 10 Bus 3: Bus number: 390 Maximum capacity: 75 Current number of passengers: 60 How many passengers are waiting for the bus: 40 Checking bus capacity... Buses that have enough space: 370 380 ./prac_q5 Enter 3 buses and their information: Bus 1: Bus number: 120 Maximum capacity: 20 Current number of passengers: 15 Bus 2: Bus number: 150 Maximum capacity: 50 Current number of passengers: 50 Bus 3: Bus number: 180 Maximum capacity: 30 Current number of passengers: 29 How many passengers are waiting for the bus: 6 Checking bus capacity... Oh no! None of the 3 buses have enough space!
There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, moving lines, or removing lines. Submit your working version of the code.
1511 autotest-prac prac_q5
1511 fetch-prac prac_q6
You have been provided with a file called
prac_q6.c.
The code in prac_q6.c is
meant to do the following:
However, it has some issues that you need to fix.
Once fixed, your program should match the following example exactly:
dcc prac_q6.c -o prac_q6 ./prac_q6 Enter temperature in Kelvin: 273.15 273.15 Kelvin is equal to 0.00 Celsius. ./prac_q6 Enter temperature in Kelvin: 300 300.00 Kelvin is equal to 26.85 Celsius. ./prac_q6 Enter temperature in Kelvin: 373.15 373.15 Kelvin is equal to 100.00 Celsius. ./prac_q6 Enter temperature in Kelvin: 0 0.00 Kelvin is equal to -273.15 Celsius.
There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, adding lines, or removing lines. Submit your working version of the code.
void kelvin_to_celsius(double kelvin, double *celsius).
1511 autotest-prac prac_q6
1511 fetch-prac prac_q7
You have been provided with a file called prac_q7.c.
The code in prac_q7.c is meant to do the following:
Once fixed, your program should match the following example exactly:
dcc prac_q7.c -o prac_q7 ./prac_q7 Enter a string: abcd Enter a position to remove: 1 Result: bcd ./prac_q7 Enter a string: abcd Enter a position to remove: 2 Result: acd ./prac_q7 Enter a string: abcd Enter a position to remove: 3 Result: abd ./prac_q7 Enter a string: abcd Enter a position to remove: 4 Result: abc ./prac_q7 Enter a string: The quick brown fox jumped over the lazy dog. Enter a position to remove: 4 Result: Thequick brown fox jumped over the lazy dog.
There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, adding lines, or removing lines. Submit your working version of the code.
1511 autotest-prac prac_q7
1511 fetch-prac prac_q8
You have been provided with a file,
prac_q8.c.
The code in list_delete_min is given one
argument, head, which is a pointer to the first
node of a linked list, and uses the following familiar data structure:
struct node {
struct node *next;
int data;
};
list_delete_min is intended to modify a linked
list such that all instances of the minimum value in the list are removed.
Thus, the program should find the minimum value in the linked list and then
remove all instances of the minimum value in the list (deleted from the list
and its memory freed). Once all instances are removed,
list_delete_min
should return a pointer to the head of the list.
For example, if the linked list contained the following values:
1 -> 2 -> 3 -> 1 -> 4 -> 5 -> 6 -> NULL
list_delete_min should return a pointer to the
head of the following list:
2 -> 3 -> 4 -> 5 -> 6 -> NULL
This occurs because 1 is the smallest value in the list so the
nodes containing 1 at positions 1 and 4 are removed.
prac_q8.c also contains a
main function which allows you to test your
list_delete_min function.
This main function:
head,
list_delete_min(head),Do not change this main function. This main function does not contain any bugs.
Your list_delete_min function will be called
directly in marking. The main function is only to let you test your
list_delete_min function.
dcc prac_q8.c --leak-check -o prac_q8 ./prac_q8 1 2 3 4 5 BEFORE: 1 -> 2 -> 3 -> 4 -> 5 -> NULL AFTER: 2 -> 3 -> 4 -> 5 -> NULL ./prac_q8 8 3 -2 -2 0 5 -2 BEFORE: 8 -> 3 -> -2 -> -2 -> 0 -> 5 -> -2 -> NULL AFTER: 8 -> 3 -> 0 -> 5 -> NULL ./prac_q8 3 3 3 3 BEFORE: 3 -> 3 -> 3 -> 3 ->NULL AFTER: NULL ./prac_q8 8 BEFORE: 8 -> NULL AFTER: NULL ./prac_q8 BEFORE: NULL AFTER: NULL
There are currently a number of issues in the code that you must fix for the code to work correctly, and produce the desired output. This may include changing lines, adding lines, or removing lines. Submit your working version of the code.
struct node.
list_delete_min should call
free to free the memory of any nodes it
deletes and must pass --leak-check.
list_delete_min should return the new head of
the list (it may change if the original head is deleted).
head may be
NULL).
list_delete_min function. The function should
not call printf.
scanf,
getchar,
fgets or otherwise read input inside your
function.
list_delete_min should not use arrays.main function. It
will not be tested or marked.
1511 autotest-prac prac_q8
1511 fetch-prac prac_q9
Write a C program prac_q9.c that scans a
target word and a string, then removes every occurrence of that
target word from the string.
A word is defined as a contiguous sequence of non-whitespace characters (the sequence of non-whitespace characters are all next to each other). The comparison is case-sensitive.
The program should output the remaining words in their original order, separated by a single space, with no leading or trailing whitespace.
For example, if the target word is hello and the input string is
hello world hello, the program will output world.
"hello", which matches the
target, so it is removed.
"world", which does
not match the target, thus it is kept.
"hello", which matches the
target, so it is removed.
In another example, if the target word is a and the input string
is a b a c b, the program will output b c b.
"a", which matches the
target, so it is removed.
"b", which does
not match the target, thus it is kept.
"a", which matches the
target, so it is removed.
"c", which does
not match the target, thus it is kept.
"b", which does
not match the target, thus it is kept.
In another example, if the target word is the and the input
string is The cat and the Cat, the program will output
The cat and Cat.
"The", which does
not match the target "the"
(capital T vs lowercase t), thus it is kept.
"cat", which does
not match the target, thus it is kept.
"and", which does
not match the target, thus it is kept.
"the", which
exactly matches the target, so it is removed.
"Cat", which does
not match the target (capital C vs lowercase c), thus it
is kept.
dcc prac_q9.c -o prac_q9 ./prac_q9 hello hello world hello world ./prac_q9 a a a a ./prac_q9 the The cat and the Cat The cat and Cat ./prac_q9 hello hello hhelloo hello hhelloo ./prac_q9 COMP1511 x y COMP1511 comp1511 COMP1511 x y comp1511
string.h library. If
you do so, you will receive a 0 in this question.
1511 autotest-prac prac_q9
1511 fetch-prac prac_q10
You are planning your course progression at university. There are a total of
num_courses courses you need to complete, labelled from 0 to
num_courses - 1. Some courses have prerequisites, meaning you must
complete certain courses before you can take others. Your task is to determine
a valid ordering in which to complete all courses.
The first command line argument (after the program name) is
num_courses, the total number of courses. The remaining
arguments are provided as pairs in the format:
a1 b1 a2 b2 ...
[ai, bi] indicates that you
must complete course bi before you can take course
ai.
Your program should print a valid ordering of all courses in the format
Order: [c1, c2, ...]. If there are multiple
valid orderings, always take the
lowest-numbered available course first. If it is impossible to complete
all courses, your program should print Order: [].
For example if you were provided the following input:
4 1 0 2 0 3 1 3 2
This input has num_courses = 4. The courses that need
to be taken are course 0, course 1, course 2 and course 3. The pairs then
provide you with the prerequisite requirements:
Course 0 has no prerequisites, so it is taken first. Then courses 1 and 2 both become available, and since we always take the lowest-numbered course first, we take course 1 next, then course 2. Finally, course 3 can be taken since both of its prerequisites are complete.
The resulting ordering is: [0, 1, 2, 3].
dcc prac_q10.c --leak-check -o prac_q10 ./prac_q10 4 1 0 2 0 3 1 3 2 Order: [0, 1, 2, 3] ./prac_q10 4 1 0 3 0 2 1 Order: [0, 1, 2, 3] ./prac_q10 2 1 0 Order: [0, 1] ./prac_q10 1 Order: [0] ./prac_q10 2 1 0 0 1 Order: []
You may find the following interactive helpful for visualising course dependencies:
Order: [].
--leak-check with no memory leaks.
1511 autotest-prac prac_q10
1511 fetch-prac prac_q11
You are given a collection of boxes, where each box has a width and a height. Your task is to determine the maximum number of boxes that can fit inside each other, like Russian dolls.
A box can fit inside another box if and only if both the width and the height of the inner box are strictly less than the width and height of the outer box. You cannot rotate a box.
The dimensions of each box are provided as command line arguments in the
format:
w1 h1 w2 h2 ... wi hi ...
wi is the width of the ith box.
hi is the height of the ith box.
Your program should print the maximum number of boxes that can be nested in the format:
Max boxes: N
where N is the result.
For example, if you were provided the following input
5 4 6 4 6 7 2 3, you would interpret it as:
In this case:
Thus, the longest chain of nested boxes is: Box 4 [2, 3] -> Box 1 [5, 4] -> Box 3 [6, 7], giving a maximum of 3.
dcc prac_q11.c --leak-check -o prac_q11 ./prac_q11 5 4 6 4 6 7 2 3 Max boxes: 3 ./prac_q11 1 1 1 1 1 1 Max boxes: 1 ./prac_q11 1 2 3 4 5 6 7 8 Max boxes: 4
You may find the following interactive helpful for visualising nested boxes:
--leak-check with no memory leaks.
1511 autotest-prac prac_q11