NATIONAL UNIVERSITY OF SINGAPORE
SCHOOL OF COMPUTING
Practical Examination 1 (PE1) for Semester 1, AY2013/4
CS1010 Programming Methodology
21 September 2013
Time Allowed: 2 hours
__________________________________________________________________________________________________________
INSTRUCTION TO CANDIDATES
1.
You are only allowed to read this cover page and the last page. Do not read the questions
until you are told to do so.
2.
This paper consists of 2 exercises on 6 pages. Each exercise constitutes 50%.
3.
This is an open-book exam. You may bring in any printed material, but not electronic
devices, including but not limited to laptop, thumb-drive, electronic dictionary and
calculator. You are to switch off/silence your mobile phone and keep it out of view.
4.
You may turn to the last page (page 6) to read some advice.
5.
You will be logged into a special Windows account at the beginning of the PE. Do not log
off until the end of the PE. Do not use your own NUSNET account.
6.
A plab account slip will be issued to you at the beginning of the PE. Bring your
matriculation card for identification when you collect it. Please leave your matriculation
card on the desk in front of you throughout the PE.
7.
You are to write your program in the given plab account. The host name is plab0 (not
sunfire!). No activity should be done outside this plab account.
8.
You do not need to submit your programs to CodeCrunch. We will retrieve your programs
and submit them to CodeCrunch after the PE.
9.
Skeleton programs and some test data files are already residing in your plab account.
Please leave the programs in the home directory, and use the same program names as
specified in the paper. Do not create subdirectory to put your programs there or we will
not be able to find them!
10. Only your source codes (.c programs) from your plab account will be collected after the
PE. Hence, how you name your executable files is not important.
11. Please read carefully and follow all instructions in the question. If in doubt, please ask.
Raise your hand and the invigilator will attend to you.
12. Any form of communication with other students or the use of unauthorised materials is
considered cheating and you are liable to disciplinary action.
13. Please save your programs regularly during the PE.
14. When you are told to stop, please do so immediately, or you will be penalised.
15. At the end of the PE, please log out from your plab account.
16. Please check and take your belongings (especially matriculation card) before you leave.
17. We will make arrangement for you to retrieve your programs after we have finished
grading. Grading may take a week or more.
ALL THE BEST!
CS1010 AY2013/4 Semester 1
- 1 of 6 -
Practical Exam 1
Exercise 1: Tray and slabs (50 marks)
There are 2 sub-tasks in this exercise.
First subtask: As in lab #1, you are given a rectangular tray and an unlimited supply of
slabs. An example of a 12×20 tray and an 8×3 slab is shown below. Note that it is
possible for the slab to be larger than the tray.
A 12×20 tray
An 8×3 slab
You are to find the minimum unused area of the tray after the slabs are packed onto
the tray. The slabs may be packed in either one of the two orientations, as shown
below, but not in a mix of orientations.
Unused area of tray = 48
Unused area of tray = 96
The minimum unused area of the tray is 48 in this example.
Second subtask: The tray is made of foldable material. Supposed you are to fold the
tray in half three times, each time either horizontally or vertically. Compute the
minimum perimeter of the tray after the folding.
Below is one way of folding the tray. The tray has been resized in the drawing to
conserve space. In the example below, the perimeter of the tray after folding is 26, but
this is not the minimum perimeter possible. The minimum perimeter is 22.
12×20
CS1010 AY2013/4 Semester 1
6×20
- 2 of 6 -
6×10
3×10
Practical Exam 1
Write a program tray.c to read in the sizes of the tray and slab, which must be of int
type. Your program then computes the following:
1. The minimum unused area of the tray after it is filled with slabs. The slabs can be
filled in one of the two orientations. Your program should have a function
min_unused_area() for this. You are to determine its return type and parameters.
2. The minimum perimeter of the tray after it is folded in half 3 times. Your program
should have a function min_perimeter() for this. You are to determine its return
type and parameters.
If you do not have the functions min_unused_area() and min_perimeter() in your
program, marks will be deducted on design. You may write additional function(s) if you
think it is necessary.
Two sample runs are shown below, with user input shown in bold. Note that the
perimeter is printed in 2 decimal places.
Enter size of tray: 12 20
Enter size of slab: 8 3
Minimum unused area = 48
Minimum perimeter after folding = 22.00
Enter size of tray: 12345 139
Enter size of slab: 27 33
Minimum unused area = 49785
Minimum perimeter after folding = 3364.25
Skeleton Program:
A skeleton program tray.c is available in your plab account and is shown below.
//
//
//
//
//
//
//
CS1010 AY2013/4 Semester 1
PE1 Ex1: tray.c
Name:
Matriculation number:
plab-id:
Discussion group:
Description:
int main(void) {
int trayHeight, trayWidth, slabHeight, slabWidth;
printf("Enter size of tray: ");
printf("Enter size of slab: ");
printf("Minimum unused area = "); // incomplete
printf("Minimum perimeter after folding = "); // incomplete
return 0;
}
CS1010 AY2013/4 Semester 1
- 3 of 6 -
Practical Exam 1
Exercise 2: Square-free Integer (50 marks)
In mathematics, a square number is an integer that is the square of a positive integer.
Examples are 9 (= 3×3), 4 (= 2×2), and 1 (=1×1). 1 is the smallest square number.
On the other hand, a square-free integer is a positive integer divisible by NO square
number, except 1. For instance,
•
•
•
10 is a square-free number
18 is not square-free as it is divisible by a square number 9
4 is also not square-free as it is divisible by the square number 4
The first 10 square-free integers are:
1, 2, 3, 5, 6, 7, 10, 11, 13, 14
Write a program square_free.c to read four positive integers (in that sequence):
lower1, upper1, lower2, upper2, compute the number of square-free integers in two
ranges [lower1, upper1] (both inclusive) and [lower2, upper2] (both inclusive), compare
and report which range has more square-free integers.
You may assume that:
1 ≤ lower1 ≤ upper1, and 1 ≤ lower2 ≤ upper2
No input validation is needed.
For example, in the sample run #1 below, range [1, 5] contains 4 square-free integers
while range [5, 9] contains 3 square-free integers. Therefore your program should print
out:
Range [1, 5] has more square-free numbers: 4
Modular design makes your coding easier. Besides the main() function, your program
must contain at least another function (of your own choice) to compute some results.
Use the skeleton program given to you. Check sample runs for input and output
format.
Two sample runs are shown below, with user input shown in bold. If both ranges have
the same number of square-free integers, refer to the second sample run below.
Enter four positive integers: 1 5 5 9
Range [1, 5] has more square-free numbers: 4
Enter four positive integers: 1 8 2 10
Both ranges have the same number of square-free numbers: 6
CS1010 AY2013/4 Semester 1
- 4 of 6 -
Practical Exam 1
Skeleton Program:
A skeleton program square_free.c is available in your plab account and is shown below.
//
//
//
//
//
//
//
CS1010 AY2013/4 Semester 1
PE1 Ex2: square_free.c
Name:
Matriculation number:
plab-id:
Discussion group:
Description:
#include <stdio.h>
#include <math.h> // optional, remove if not used
int main(void) {
int lower1, upper1, lower2, upper2;
printf("Enter four positive integers: ");
// the following printf is incomplete
printf("Range [%d, %d] has more square-free numbers:
%d\n");
// the following printf is incomplete
printf("Both ranges have the same number of square-free
numbers: %d\n");
return 0;
}
// Write at least another function (of your own choice)
// to compute some results
CS1010 AY2013/4 Semester 1
- 5 of 6 -
Practical Exam 1
CS1010 AY2013/4 Semester 1
Practical Exam 1 (PE1)
Advice – Please read!
You are advised to spend the first 10 minutes for each exercise thinking and
designing your algorithm, instead of writing the programs right away.
You are not allowed to use arrays, recursion or string functions from string.h. If in
doubt, please check with the lecturer.
If you write a function, you must have a function prototype, and you must put the
function definition after the main() function.
You may write additional function(s) not mentioned in the question, if you think it is
necessary.
Any variable you use must be declared in some function. You are not allowed to use
global variables (variables that are declared outside all the functions).
You may assume that all inputs are valid, that is, you do not need to perform input
validity check.
Manage your time well! Do not spend excessive time on any exercise.
Be careful in naming your executable code. Do not overwrite your source code with
your executable code, especially if you are using the –o option in gcc!
The rough marking scheme for both exercises is given below.
Rough Marking Scheme For Each Exercise [50 marks]
1. Style: 10 marks
Are name, matriculation number, plab-id, DG and description filled at the top
of the program?
Is there a description written at the top of every function (apart from the
main() function)?
Are there proper indentation and naming of variables?
Are there appropriate comments wherever necessary?
2. Design: 10 marks
Are there correct definition and use of functions?
Are function prototypes present?
Is the right construct used?
Is algorithm not unnecessarily complicated?
3. Correctness: 30 marks
4. Deductions (not restricted to the following):
Program cannot be compiled: Deduct 10 marks
Compiler issues warning with –Wall: Deduct 5 marks.
Use of arrays, recursion, built-in string functions from string.h: Deduct 10 marks
Use of global variables: Deduct 10 marks
--- END OF PAPER --CS1010 AY2013/4 Semester 1
- 6 of 6 -
Practical Exam 1
© Copyright 2025 Paperzz