COMP281 Assignment 2 –
! The following 5 problems will be available as Assignment 2 on the online judging system available
! You need to write a valid C program that solves each of these problems – it must read the input, as
specified in the problem description then print the solution to the given problem for that input.
! Input is read from the standard input, in the same way that you read input from the keyboard as shown in
lectures (e.g., using scanf). Output is also printed to the standard output, as you have seen (e.g., using
! When you are satisfied that your programs work correctly, you must submit them through the
departmental submission system, as described below.
! You must also include a brief report describing your solutions to the problems. This should be up to one
side of A4 paper (although there is no penalty for a longer report) and should give a description of how
each of your solutions works. This should include describing the algorithm used to reach the solution,
describing your use of any C language features (that were not discussed in lectures), identifying any
resources that you have used to help you solve the problems and describing any joint work you may
have done with other students.
! This assignment is worth 30% of the total mark for COMP281
• 50% of the marks will be awarded for programs that correctly solve the problem for all test cases.
All problems are weighted equally and each counts for 20% of this total.
• 40% of the marks will be awarded depending on the style, comments and efficiency of the
solution. All problems are weighted equally and each counts for 20% of this total.
• 10% of the marks will be awarded for the quality and depth of the accompanying report
• See separate marking guidelines for more details.
• Name each of your c files according to the problem number; i.e.,
1029.c,1032.c,1041.c,1043.c and 1044.c. Place all five of your C files and your report
(in .pdf format) into a single zip file.
• All question have also been/will also be posted on www.csc.liv.ac.uk/JudgeOnline2
• Before you submit your solutions, please first test them using the online judge. You are
required to include the “Result” and the “RunID” in your C code as comments. The OJ
provides a RunID. RunIDs are not problem IDs.
o Example: the problem is 10xx . The solution has been accepted by the OJ, and
the runID is 2033. You add to your code: /* 2033 Accepted */
o Result is one of the following: Accepted, Wrong Answer, Presentation Error, Time
Limit Exceeded, Memory Limit Exceeded, Output Limit Exceeded, Runtime Error,
• Submit this zip file using the departmental submission system at :
The deadline for this assignment is Wednesday March 4th, 4:00pm
• Penalties for late submission apply in accordance with departmental policy as set out in the
student handbook, which can be found at:
Title: ROT R 3
ROT R 3 (“Rotate Right by 3”) is a simple substitution cipher to encode letters. Applying ROT R 3 to a
piece of text merely requires examining its alphabetic characters and replacing each one by the letter 3
places further or ‘right’ of it along in the alphabet, wrapping back to the beginning if necessary. A becomes
D, B becomes E, and so on up to W, which becomes Z, then the sequence continues at the beginning of the
alphabet: X becomes A, Y becomes B, and Z, becomes C. Only those letters, which occur in the English
alphabet, are affected; numbers, symbols, whitespace, and all other characters are left unchanged.
One line of characters (ends with EOF)
The result after applying ROT R 3 to the input
Title: String search
Input three strings s1, s2, and s3 (one for each line). They may contain whitespaces. Output the number of
times s3 and s2 occur as a substring of s1.
For example, if the input strings are “hello world”, “europe” and “wor”, then the output should be “0 1”. As
another example, if the input strings are “hello world hello”, “hello world” and “hel”, then the output should
be “1 2”. If the input strings are “hello world”, “hello” and “helo”, then the output should be “1 0”.
You are not allowed to use system functions defined in string.h.
s1, s2 and s3.
Hello World! Hello
Title: Dot Product of two matrices
Input three positive integers n, m, and p. Then input two matrices. The first one is a n (rows) by m (columns)
matrix. The second one is a m (rows) by p (columns) matrix. Output the matrix product (should be n rows
by p columns). You are required to store the matrices in heap memory.
n, m, p, and two matrices
Product of these two matrices
14 9 3
2 11 15
0 12 17
5 2 3
Title: Sort Strings
Input n and then followed by n strings, one string per line. Subsequently this is followed by input m, and
then followed by m strings (one per line). Strings consist of lowercase English letters only. The maximum
string length is 100 (at most 99 letters, plus ‘\0’). However, most strings are very short. That is, you should
dynamically allocate just enough memory to store each string. You should also dynamically allocate just
enough space for storing n+m string pointers.
Please sort these n+m strings based on their lengths in descending order. String lengths are unique. There
will be no ties. Output the sorted list of strings.
n and then followed by n strings. m and then followed by m strings.
Sorted list, one string per line
Title: 2 raised to the power of x
2^x stands for 2 raised to the power of x. 2^4=16 and the sum of its digits is 1+6=7. 2^5=32 and the sum of
its digits is 3+2=5. Input x. x is a nonnegative integer that is at most 10000. Output the sum of the digits of
Hint: Problem 1036 is a good start.
result (one integer)
COMP281 Assignment 2 – C Programming
COMP281 Assignment 2 –