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 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.1511 autotest-prac prac_qX
system()
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 feels 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.
The starter files for this exam will be automatically placed in your home directory.
Programs (including your terminal and text editor) are available via the desktop menu
(accessible by right-clicking the desktop).
A copy of the course website has been made available. You can access this via the desktop menu also (accessible by right-clicking the desktop).
To test your code (for example, for question 1), run the command:
1511 autotest-prac prac_q1
To submit your code (for example, for question 1), run the command:
submit prac_q1 prac_q1.c
1511 fetch-prac prac_q1
Note prac_q1.c
uses the following familiar data type:
struct node { struct node *next; int data; };
sum_divisible
is given two arguments, head1
and
head2
, which are pointers to the first node of linked lists.
sum_divisible
should return the sum of the elements in the first
list that are divisible by their corresponding element in the second list. In
other words, if the ith element of list1
is divisible by the ith
element of list2
, then you should add the ith element of
list1
to the sum.
If one list is longer than the other, the extra elements should be ignored.
You can assume all values in the lists will be greater than 0.
For example, if the two lists contain these values:
list1
: 3, 1, 4, 11, 6, 9
list2
: 3, 7, 2
sum_divisible
should return 7, because
For example, if the two lists contain these values:
list1
: 2, 4, 6
list2
: 1, 2, 3
sum_divisible
should return 12, because
2 is divisible by 1, 4 is divisible by 2, 6 is divisible by 3, and 2 + 4 +
6 = 12.
list_sum_disible.c
also contains a main
function which
allows you to test your sum_divisible function.
This main function:
head1
head2
sum_divisible(head1, head2)
Do not change this main function. If you want to change it, you have misread the question.
Your sum_divisible
function will be called directly in marking. The
main function is only to let you test your sum_divisible
function
dcc prac_q1.c -o sum_divisible ./prac_q1 2 4 6 - 1 2 3 12 ./prac_q1 3 1 2 - 2 7 9 0 ./prac_q1 16 7 8 12 - 13 19 21 12 12 ./prac_q1 2 4 6 - 3 2 3 10 ./prac_q1 - 1 2 3 4 0 ./prac_q1 4 3 2 1 - 0 ./prac_q1 - 0
sum_divisible
should return only a single integer.sum_divisible
should not change the linked lists it is given.
sum_divisible
should not change the next or data fields of list
nodes.
sum_divisible
should not use arrays.sum_divisible
should not call malloc.sum_divisible
should not call scanf (or getchar or fgets).
sum_divisible
should not print anything. It should not call printf.
struct node
.main
function. It will not be tested or
marked.
1511 autotest-prac prac_q1
1511 fetch-prac prac_q2
min_lowercase
will be passed in a character array with exactly
size
columns. Add code so that the
min_lowercase
function returns the lowercase letter with the lowest
ASCII value.
If the array contains these elements:
{'c', 'A', 'e', 'P', '!'}
Your function should return the character 'c' because it is lowercase and has the lowest ASCII value compared to all other characters in the array.
For example if the array contains these elements:
{'O', '@', 'g'}
Your function should return the character 'g' because it is lowercase and has the lowest ASCII value compared to all other characters in the array.
Your min_lowercase
function will be called directly in marking.
The main function is only to let you test your
min_lowercase
function.
min_lowercase
should not modify the array it is provided.
min_lowercase
should not call scanf (or getchar or fgets).
min_lowercase
should not print anything, i.e. It should
not call printf
.
1511 autotest-prac prac_q2
1511 fetch-prac prac_q3
The function insert_alphabet_after
is given two arguments,
ch
and head
where head
is a pointer to
the first node in a linked list.
insert_alphabet_after
should find every node in the given linked
list which contains the letter directly before ch
in the
alphabet. It should create new node(s) (using malloc) containing
ch
and insert it after every node found.
insert_alphabet_after
should return a pointer to the head of the
list.
In the case that head
is empty, you should return a pointer to
the new node with the given ch
.
For example if ch
is c and the linked list contains these
3 elements:
b d e
The character before ch
in the alphabet in the list is b,
and so the c is inserted after the b, resulting in the list now
having these elements:
b c d e
If the ch
is e and the linked list contains these 4
elements:
d d d d
The e occurs right after d in the alphabet, and so the e is inserted once after every d in the list, resulting in the list now having these elements:
d e d e d e d e
prac_q3.c
also contains a main
function which
allows you to test your insert_alphabet_after
function.
This main function:
head
value
insert_alphabet_after(value, head)
Do not change this main function. If you want to change it, you have misread the question.
Your insert_alphabet_after
function will be called directly in
marking. The main function is only to let you test your
insert_alphabet_after function.
dcc prac_q3.c -o prac_q3 ./prac_q3 a b [a, b] ./prac_q3 d c b a e [d, e, c, b, a] ./prac_q3 a [a] ./prac_q3 h e l l o a [h, e, l, l, o] ./prac_q3 h [h] ./prac_q3 a a b b b [a, b, a, b, b, b]
insert_alphabet_after
should not use arrays.insert_alphabet_after
should not call scanf
(or
getchar
or fgets
).
insert_alphabet_after
should not print anything. It should not
call printf
.
ch
in the linked list, insert_alphabet_after
should insert after every one of those nodes in the list.
ch
is a, it should only be added if we are
adding to an empty list as no alphabet characters come before a.
main
function. It will not be tested
or marked.
1511 autotest-prac prac_q3
1511 fetch-prac prac_q4
vowels_exactly_balanced
will be passed an array of strings, with
exactly size
strings. Each row contains a string terminated by the
null terminator ('\0'
) character. Write the
vowels_exactly_balanced
function which returns 1 if each string
contains exactly the same number of vowels, or 0 if they do not.
If the array contains these elements:
{'a', 'a', 'a', 'a', '\0'}, {'c', 'o', 'm', 'p', '\0'}, {'o', 'o', 'o', 'h', '\0'}, {'y', 'a', 'y', 'a', '\0'},
Your function should return false (0), since each string does not have the same number of vowels.
For example if the array contains these elements:
{'a', 'b', 'c', '\0'}, {'!', 'e', '\0'}, {'a', 'c', '\0'},
Your function should return true (1), since each string contains 1 vowel.
For example if the array contains these elements:
{'f', 'g', 'h', '\0'}, {'j', '\0'}, {'z', 'x', 'c', 'v', 'b', 'n', 'm', '\0'},
Your function should return true (1), since each string contains 0 vowels.
prac_q4.c
also contains a simple
main
function which allows you to test your
vowels_exactly_balanced
function.
Your vowels_exactly_balanced
function will be called directly in
marking. The main function is only to let you test your
vowels_exactly_balanced
function.
vowels_exactly_balanced
can assume there will be at least one
string
vowels_exactly_balanced
will always have a null
terminator character at the final position
vowels_exactly_balanced
contains at most 100
characters
vowels_exactly_balanced
should not print anything. It should
not call printf.
1511 autotest-prac prac_q4
1511 fetch-prac prac_q5
prac_q5.c
is meant to do the following:
Once fixed, your program should match the following example exactly:
dcc prac_q5.c -o prac_q5 ./prac_q5 Enter the month number (1-12): 0 Invalid month entered. ./prac_q5 Enter the month number (1-12): 12 It's summer in Australia. ./prac_q5 Enter the month number (1-12): 5 It's autumn in Australia.
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_q5
1511 fetch-prac prac_q6
compare_tens
is given two arguments, head1
and
head2
, which are pointers to the first node of linked lists.
compare_tens
is meant to return -1 if the amount of two digit
positive numbers in the first list is less than the number of two digit
positive numbers in the second list.
compare_tens
is meant to return 0 if the amount of two digit
positive numbers is the same in both lists.
compare_tens
is meant to return 1 if the amount of two digit
positive numbers in the first list is more than the number of two digit
positive numbers in the second list.
And for example, if the two lists contain these values:
list1
: 13, 1, 4list2
: 2, 71, 1, 18, 3compare_tens
is meant to return -1, because there is 1 two
digit positive number in the first list and 2 in the second list, which means
there are less two digit positive numbers in the first list than there are in
the second.
For example, if the two lists contain these values:
list1
: 30, 11list2
: 19, 71
compare_tens
is meant to return 0, because the first list
has the same number of two digit positive numbers as the second list.
Once fixed, your program should match the following example exactly:
prac_q6.c
also contains a
main
function which allows you to test your
compare_tens
function.
This main function:
head1
head2
compare_tens(head1, head2)
Yourcompare_tens
function will be called directly in marking. The
main function is only to let you test your compare_tens
function.
dcc prac_q6.c -o prac_q6 ./prac_q6 3 1 4 - 2 7 1 8 3 0 ./prac_q6 10 16 21 12 - 16 7 8 12 1 ./prac_q6 2 4 6 - 42 -1 ./prac_q6 - 0
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.
You can change any code except for the functions that specifically state that they cannot be changed. You can assume the functions you should not change are correct.
compare_tens
should return only a single integer and this integer
must one of three values: -1, 1 or 0.
compare_tens
should not change the linked lists it is given.
compare_tens
should not change the next or data fields of list
nodes.
compare_tens
should not use arrays.compare_tens
should not call malloc.compare_tens
should not call scanf (or getchar or fgets).compare_tens
should not print anything. It should not call
printf.
main
function. It will not be tested
or marked.
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 until the user enters Ctrl-D
:
Ctrl-D
which will end the program.
dcc prac_q7.c -o prac_q7 ./prac_q7 Enter a number: 2 The factorial of 2 is 2 Enter a number: 5 The factorial of 5 is 120 Enter a number: 7 The factorial of 7 is 5040 Enter a number:
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.
compute_factorial
function should not print anything.
1511 autotest-prac prac_q7
1511 fetch-prac prac_q8
prac_q8.c
is meant to do the following:
This program should create and scan in the elements of a 3x3 2d array, then multiply all the odd numbers in the 2d array by 100.
However, it has some issues that you need to fix.Once fixed, your program should match the following example exactly:
dcc prac_q8.c -o prac_q8 ./prac_q8 Enter your numbers: 1 2 3 4 5 6 7 8 9 Converted: 100 2 300 4 500 6 700 8 900 ./prac_q8 Enter your numbers: 1 2 3 1 2 3 1 2 3 Converted: 100 2 300 100 2 300 100 2 300 ./prac_q8 Enter your numbers: 1 1 1 2 2 2 3 3 3 Converted: 100 100 100 2 2 2 300 300 300
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_q8
1511 fetch-prac prac_q9
In this question, you will implement the function rotation_shift_amount
.
int rotation_shift_amount(char *str1, char *str2)
checks if str2
is a rotation of str1
and returns
the amount str2
is rotated relative to str1
.
If str2
is not a rotation of str1
, the function should
return -1.
Rotation in this context means shifting the characters of str1
to the left.
For example, if str1
is "hello":
"hello"
"elloh"
"llohe"
"lohel"
"ohell"
For example, if two strings "hello" and "llohe" are given, then the function should return 2 as "llohe" is a rotation of "hello" by 2 characters.
result = rotation_shift_amount("hello", "llohe") // result = 2 result = rotation_shift_amount("hello", "olelh") // result = -1 result = rotation_shift_amount("", "") // result = 0 result = rotation_shift_amount("A really long string with many words", "Hey! Another string :D") // result = -1
string.h
library. If you do so, you
will receive a 0 in this question.'\0'
character.'\0'
).1511 autotest-prac prac_q9
1511 fetch-prac prac_q10
In this question, we will be writing a system to simulate orders to buy and sell
items being sent in a market! Each order is of type "BUY" or "SELL" and contains
information about the price, quantity, and a link to the next order. The starter
code provided scans in a sequence of orders, and creates a linked list of
struct order
s, which you can traverse to access the orders.
We have two types of orders: BUY and SELL, `b` and `s` respectively. A BUY order represents the buyer's willingness to pay up to a given price for a certain quantity of goods, and a SELL order represents the seller's desire to sell a certain quantity of goods at a minimum price.
The objective is to match BUY and SELL orders based on their prices and quantities, or store them for later matching if no immediate match is found, so that a trade can occur!
The matching process is as follows:
SELL 7 items for $6 BUY 6 items for $4
b
or s
.
The following diagrams are worked examples, showing the operation of the system step-by-step.
Note: The format in which orders are entered is
[order_type] [quantity] [price]
.
dcc prac_q10.c -o prac_q10 ./prac_q10 b 5 8 b 7 9 s 7 10 s 5 9 SELL 7 items for $10 BUY 2 items for $9 BUY 5 items for $8 ./prac_q10 s 12 10 s 7 9 b 7 8 b 11 11 s 6 5 b 8 10 BUY 1 items for $8 ./prac_q10 b 10 3 b 6 4 s 7 6 SELL 7 items for $6 BUY 6 items for $4 BUY 10 items for $3 ./prac_q10 b 10 5 s 10 5 ./prac_q10 b 15 7 b 10 6 s 7 4 s 12 5 BUY 6 items for $6 ./prac_q10 s 10 12 s 9 7 b 8 7 b 9 11 s 6 2 b 5 10 SELL 10 items for $12 BUY 2 items for $11 BUY 5 items for $10
1511 autotest-prac prac_q10
1511 fetch-prac prac_q11
In this question, we will write a program to help us validate the format of our dcc commands, and print information about them!
The input for this question will be a series of commands entered via standard input, representing either:
If the command is valid, then it will produce an executable. This is explained in more detail later.
./[executable_name]
. If a previously run dcc command
successfully created the given executable, then "running the executable"
should output information about the files used to compile that executable.
For example, if the executable my_program
was compiled with the
command dcc my_program.c other_file.c -o my_program
, then
running my_program
should produce the following output:
Executable 'my_program' was compiled using the following files: - my_program.c - other_file.c
If the executable name does not match any produced by previous dcc commands, then the message "Executable [executable_name] not recognised!" should be printed.
The rules for validating a given dcc command are as follows:
If any of the above rules are not satisfied, the program should print "Invalid command!". If all the rules are met, then the program should print "Executable [executable_name] compiled!", and store information about the executable.
If a dcc
command creates an executable with the same name as an
existing executable, then the original executable should be overwritten and,
replaced with the new one.
dcc prac_q11.c -o prac_q11 ./prac_q11 # dcc main.c -o main Executable main compiled! # ./prac_q11 # dcc main.c test.c -o main Executable main compiled! # dcc main.c main Invalid command! # dcc main.c -o test Invalid command! # dcc main.c Invalid command! # dcc main.c -o main.c Invalid command! # dcc main.c.c -o main.c Invalid command! # dcc main.py -o main Invalid command! # ./prac_q11 # dcc main.c test.c -o test Executable test compiled! # ./test Executable 'test' was compiled with the following files: - main.c - test.c # dcc other_file.c test.c -o test Executable test compiled! # ./test Executable 'test' was compiled with the following files: - other_file.c - test.c # dcc broken_command.c -o test Invalid command! # ./test Executable 'test' was compiled with the following files: - other_file.c - test.c # ./prac_q11 # dcc main.c -o main Executable main compiled! # dcc test.c -o test Executable test compiled! # dcc file.c -o file Executable file compiled! # ./main Executable 'main' was compiled with the following files: - main.c # ./test Executable 'test' was compiled with the following files: - test.c # ./file Executable 'file' was compiled with the following files: - file.c #
dcc
(meaning
that it will be a dcc command), or the first argument will begin with
./
(meaning that it will be a command to run an executable).
malloc
before terminating.
1511 autotest-prac prac_q11