Docsity

CS1010 Programming Methodology Practical Exam 1: Exercises and Solutions, Exams of Computer Science

Toggle sidebar
bg1
Practical Exam 1 CS1010 AY24/25 Sem 1
NATIONAL UNIVERSITY OF SINGAPORE
SCHOOL OF COMPUTING
PRACTICAL EXAMINATION 1 FOR
Semester 1 AY2024/2025
CS1010 Programming Methodology
October 2024 Time Allowed: 2 Hours
INSTRUCTIONS TO CANDIDATES
1. This assessment paper contains 4 questions and comprises 8 printed pages, including this page.
2. The total mark for this assessment is 30. Answer ALL questions.
3. This is an OPEN BOOK assessment. You are only allowed to refer to any printed or handwritten
materials. No digital reference materials are allowed.
4. You can assume that all the given inputs are valid.
5. You can assume that the types long and double suffice for storing integer values and real
values respectively, for the purpose of this examination.
6. Login to the special account given to you. You should see the following in your home directory:
• The skeleton code symbol.c ,friend.c ,markup.c , and interval.c .
• A file named Makefile to automate compilation and testing.
• A file named test.sh to invoke the program with its test cases.
• Two directories, inputs and outputs , within which you can find some sample inputs
and outputs.
• Two directories, include and lib , containing the files for the CS1010 I/O library.
• The files .clang-tidy ,.clang-format , and compile_flags.txt for configuring the
tools clang-tidy ,clang-format and clang respectively.
• The file .vimrc and the directory .vim that contains the default configuration and plu-
gins for vim .
7. Solve the given programming tasks by editing the given skeleton code. You can leave the files in
your home directory and log off after the examination is over. There is no need to submit your
code to GitHub.
8. You can run the command make to automatically compile, run the tests (if compiled success-
fully), and run the command clang-tidy on your code.
9. Only the code written in symbol.c ,friend.c ,markup.c , and interval.c directly under
your home directory will be graded. Make sure that you write your solution in the correct file.
Failure to do so would result in 0 marks for the corresponding question.
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS1010 Programming Methodology Practical Exam 1: Exercises and Solutions and more Exams Computer Science in PDF only on Docsity!

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING

PRACTICAL EXAMINATION 1 FOR

Semester 1 AY2024/

CS1010 Programming Methodology

October 2024 Time Allowed: 2 Hours

INSTRUCTIONS TO CANDIDATES

  1. This assessment paper contains 4 questions and comprises 8 printed pages, including this page.
  2. The total mark for this assessment is 30. Answer ALL questions.
  3. This is an OPEN BOOK assessment. You are only allowed to refer to any printed or handwritten materials. No digital reference materials are allowed.
  4. You can assume that all the given inputs are valid.
  5. You can assume that the types long and double suffice for storing integer values and real values respectively, for the purpose of this examination.
  6. Login to the special account given to you. You should see the following in your home directory:
    • The skeleton code symbol.c , friend.c , markup.c , and interval.c.
    • A file named Makefile to automate compilation and testing.
    • A file named test.sh to invoke the program with its test cases.
    • Two directories, inputs and outputs , within which you can find some sample inputs and outputs.
    • Two directories, include and lib , containing the files for the CS1010 I/O library.
    • The files .clang-tidy , .clang-format , and compile_flags.txt for configuring the tools clang-tidy , clang-format and clang respectively.
    • The file .vimrc and the directory .vim that contains the default configuration and plu- gins for vim.
  7. Solve the given programming tasks by editing the given skeleton code. You can leave the files in your home directory and log off after the examination is over. There is no need to submit your code to GitHub.
  8. You can run the command make to automatically compile, run the tests (if compiled success- fully), and run the command clang-tidy on your code.
  9. Only the code written in symbol.c , friend.c , markup.c , and interval.c directly under your home directory will be graded. Make sure that you write your solution in the correct file. Failure to do so would result in 0 marks for the corresponding question.
  1. Grading criteria:
    • Passing all the given test cases does not guarantee that your code will receive full marks.
    • The main marking criteria is correctness and is defined in the broad sense of using the var- ious programming constructs in C (type, function, variable, loops, conditionals, arithmetic expressions, logical expressions, memory management) properly — not just producing the correct output.
    • Marks may be deducted for code that performs redundant or repetitive work. Question 4 interval allocates 2 marks separately for efficiency.
    • There is a 1 mark deduction for each warning (including repeated warnings) generated by clang and clang-tidy.
    • Programs that cannot be compiled would receive 0 marks.

2 Friend (7 marks)

We can measure the popularity of a person by how many friends a person has. We assume that ”friend” is a symmetric relation. If A is a friend of B, then B is a friend of A.

Given a list of k people, with id numbered from 0, 1,.. ., k− 1 , we can represent n pairs of friendship among these people using two lists of numbers F and G. Each list contains n integers ranging from 0 to k − 1 , with possible repetitions. Let F = ⟨f 0 , f 1 ,... , fn− 1 ⟩ and G = ⟨g 0 , g 1 ,... , gn− 1 ⟩, then fi is a friend of gi (and vice versa) for 0 ≤ i < n. A pair of friends is listed only once in F and G, and they can be listed in any order.

Write a program friend that reads the following from the standard input:

  • a positive integer k representing the number of people;
  • a non-negative integer n representing the pairs of friendships;
  • n integers representing the list F ; and
  • n integers representing the list G.

Print, to the standard output, the ID of the person who is the most popular (i.e., with the most number of friends). We break ties by preferring the person with a smaller id.

Example

0 1

3 2

4 0 2 1 3

Figure 1 Figure 2

Consider the input below that corresponds to Figure 1 above.

4 4 0 3 3 1 3 1 2 2

In this input, there are four people and four pairs of friends among them. The pairs of friends are (0, 3), (3, 1), (3, 2), and (1, 2). The most popular person is person 3.

Another example is shown in Figure 2 and corresponds to the input below:

5 8 4 4 4 0 0 0 2 1 1 2 0 1 3 2 1 3

Here, there are five people and eight pairs of friends among them. Person 0 and Person 1 have the most friends (four each). We break ties by choosing Person 0.

Sample Runs

echu21@pe101:~$./friend 4 4 0 3 3 1 3 1 2 2 3

echu21@pe101:~$./friend 5 8 4 4 4 0 0 0 2 1 1 2 0 1 3 2 1 3 0

4 Interval (9 marks)

Help Mr Nodle schedule his consultation hours by merging a given list of intervals.

An interval is a pair of integers [a, b] where a is the start time and b is the end time. An interval [a, b] is contained in another interval [c, d] if c ≥ a and b ≤ d. In this case, merging [a, b] and [c, d] gives the interval [c, d]. Two intervals [a, b] and [c, d] overlap if a ≤ c ≤ b or c ≤ a ≤ d. In this case, merging [a, b] and [c, d] gives the interval [min(a, c), max(b, d)].

For example, merging [1, 3] and [2, 4] gives [1, 4]. Merging [2, 3] and [1, 6] gives [1, 6]. Interval [1, 2] and [2, 3] are also overlapping, and merging them gives [1, 3]. On the other hand, the intervals [1, 3] and [4, 5] are disjoint. Merging [1, 3] and [4, 5] has no effect and gives [1, 3] and [4, 5].,

Write a program interval that reads, from the standard input, a positive integer n followed by 2 n non-negative integers representing n intervals. The numbers are start and end times. The i-th interval will be the 2 i-th number and (2i + 1)-th number in the sequence. You are guaranteed that the intervals are sorted in increasing order by their end time.

The program merges as many intervals in the input as it can, and prints to the standard output the merged intervals, one per line, in increasing order of the end time. When printing an interval, the start and end times should be separated by a comma and a space, and the intervals should be enclosed in square brackets. The function print_interval is provided to help you print an interval.

Examples

The input 3 0 5 3 6 4 6 represents intervals [0, 5], [3, 6], and [4, 6]. The three intervals can be merged as [0, 6]. So Mr. Nodle starts his consultation at time 0 and ends at 6.

The input 2 2 1000000 1000000 1000001 represents intervals [2, 1000000] and [1000000, 1000001]. The first interval ends at the same time the second interval starts. The two intervals are merged as [2, 1000001]. Mr. Nodle is gonna hold a really long consultation hour.

The input 4 2 4 5 8 2 8 9 11 represents intervals [2, 4], [5, 8], [2, 8], and [9, 11]. The first three intervals are merged into [2, 8]. The last interval [9, 11] is disjoint from [2, 8]. The intervals after merging are [2, 8] and [9, 11]. Mr. Nodel gets a break between 8 and 9.

Mark Allocation

2 marks are allocated to efficiency for this question.

Sample Runs

echu21@pe101:~$./interval 3 0 5 3 6 4 6 [0, 6]

echu21@pe101:~$./interval 2 2 1000000 1000000 1000001 [2, 1000001]

echu21@pe101:~$./interval 4 2 4 5 8 2 8 9 11 [2, 8] [9, 11]

This page is intentionally left blank.

END OF PAPER