CS1101: Programming Methodology
Download
Report
Transcript CS1101: Programming Methodology
CS1101:
Programming Methodology
Preparing for
Practical Exam (PE)
Important Notes (1/2)
Have you been practising? In a simulated test
environment?
Manage your time well.
Read the instructions carefully.
Read the questions carefully. When in doubts,
ask.
Do not start coding right away. THINK about
the algorithm first.
Check that your algorithm works before you
start coding.
CS1101: Preparing for PE
2
Important Notes (2/2)
If the problem seems hard, simplify it, break it
into smaller sub-problems.
In the worst case, solve a simplified version. A
partial program is better than no program.
Make sure that your programs can be compiled.
Code incrementally.
Test your programs thoroughly with your own
test data.
Your programs will be graded manually.
CourseMarker is just a tool.
CS1101: Preparing for PE
3
Other tips…
Two questions.
Spend 20 – 30 minutes on thinking, algorithm,
etc. before you code.
Do not forget to add appropriate comments and
proper indentation in your codes. These are
graded as well.
Ask your lecturer!
CS1101: Preparing for PE
4
Task 1: Plurals (1/5)
If a word ends in “y”, replace it with “ies”.
If a word ends in “s”, “ch” or “sh”, add
“es”.
All other cases, just add “s”.
Input consists of multiple lines. Each line
contains one word. Each word contains one or
more lowercase letters.
CS1101: Preparing for PE
5
Task 1: Plurals (2/5)
Enter a word: dairy
The plural form of "dairy" is "dairies".
Enter a word: boss
The plural form of "boss" is "bosses".
Enter a word: dish
The plural form of "dish" is "dishes".
Enter a word: bird
The plural form of "bird" is "birds".
Enter a word: <enter>
CS1101: Preparing for PE
6
Task 1: Plurals (3/5)
while ( there is still input ) {
read str;
n = str.length();
if (str ends with “y”)
print str.subString(0, n-1) + “ies”;
else if (str ends with “s” or “ch” or “sh”)
print str + “es”;
else
print str + “s”;
}
CS1101: Preparing for PE
7
Task 1: Plurals (4/5)
CS1101: Preparing for PE
8
Task 1: Plurals (5/5)
CS1101: Preparing for PE
9
Task 2: Factorisation (1/4)
Past PE question
Time limit: 30 minutes
Write a program to read in a non-zero integer
and display the factorisation.
Examples:
Enter n: 8
8 = 1 * 2 * 2 * 2
Enter n: -300
-300 = -1 * 2 * 2 * 3 * 5 * 5
Enter n: 77
77 = 1 * 7 * 11
CS1101: Preparing for PE
10
Task 2: Factorisation (2/4)
CS1101: Preparing for PE
11
Task 2: Factorisation (3/4)
CS1101: Preparing for PE
12
Task 2: Factorisation (4/4)
CS1101: Preparing for PE
13
Task 3: Candles (1/3)
Peter has n candles. He burns them one at a time
and carefully collects all unburnt residual wax. Out
of the residual wax of exactly k > 1 candles, he can
roll out a new candle.
How many candles can Peter burn?
The input contains two integers giving the values of
n and k.
The output should consist of just one integer giving
the maximum number of candles that Peter can
burn.
CS1101: Preparing for PE
14
Task 3: Candles (2/3)
Sample run:
Enter n: 5
Enter k: 3
Number of candles Peter will burn = 7
CS1101: Preparing for PE
15
Task 3: Candles (3/3)
CS1101: Preparing for PE
16
Task 4: Pascal’s Triangle (1/5)
In this problem, you are asked to generate Pascal’s Triangle.
Pascal’s Triangle is useful in many areas from probability to
polynomials to setting programming questions. It is a
triangle of integers with 1 on top and down the sides. Any
number in the interior equals the sum of the two numbers
above it. For example, here are the first 5 rows of the
triangle.
1
1 1
1
2
1
1
3
3 1
1
CS1101: Preparing for PE
4
6
4
1
17
Task 4: Pascal’s Triangle (2/5)
Write a program to generate a Pascal’s Triangle as
shown. It should be observed that the next row of
the Pascal’s triangle can be generated from the
previous row. Thus, using a single-dimensioned
array to store the values of the previous rows seems
appropriate.
1
1
1
1
1
1
CS1101: Preparing for PE
Output for n (number of rows) = 6.
1
2
3
4
5
1
3 1
6 4 1
10 10 5 1
18
Task 4: Pascal’s Triangle (3/5)
import java.util.*;
public class PascalTriangle {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
int[] pascal = new int[n];
printPascalTriangle(pascal);
}
CS1101: Preparing for PE
19
Task 4: Pascal’s Triangle (4/5)
n=6
1
CS1101: Preparing for PE
0
0
0
0
0
20
Task 4: Pascal’s Triangle (5/5)
CS1101: Preparing for PE
21
Task 5: Teams (1/3)
We need to split a group of n (an even number) players into
two teams. Here is what we do:
Line up the players in a straight line.
Stating from the first player in the line, count the players while
reciting a song that consists of m words. The mth player leaves
the line and joins “Team A”.
Repeat the song, now starting with the player who comes after
the player who just left.
The next player who leaves the line joins “Team B”.
Whenever the end of the line is reached, the counting resumes
at the beginning of the line.
Repeat until all the players are assigned to one of the two teams.
CS1101: Preparing for PE
22
Task 5: Teams (2/3)
The first 2 lines of the input are the numbers n and m.
The next n lines of the input are the names of the players.
The input name sequence determines the order in which the
players are lined up.
Your output should list all the players in “Team A” followed by
“Team B” in the order which they joined the respective teams.
CS1101: Preparing for PE
23
Task 5: Teams (3/3)
Sample input:
6
3
Emily
Hannah
Emma
Ashley
Sarah
Victoria
Sample output:
Emma
Ashley
Sarah
Victoria
Hannah
Emily
CS1101: Preparing for PE
24
More tasks at…
Visit
http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html
CS1101: Preparing for PE
25
End of File
CS1101: Preparing for PE
26