7/27/2019 Extra_class_with_notes.pptx
1/15
CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/
http://www.comp.nus.edu.sg/~cs1101/http://www.comp.nus.edu.sg/~cs1101/7/27/2019 Extra_class_with_notes.pptx
2/15
Extra class: 21 September 2011
Objectives:
To go over the basic concepts covered in the first
6 weeks
To give out some programming exercises to teststudents understanding of these basic concepts
CS1010 (AY2011/2 Semester 1) Extra Class - 2
7/27/2019 Extra_class_with_notes.pptx
3/15
Summary (1/2)
Week 2: Writing algorithms (pseudo-codes)
Input (scanf) and output (printf)
Types (int, float, double, char, etc.) and variables
Mathematical operations (*, /, %, +, -)
About integer division
Week 3:
Top-down design
Writing functions: The NOT acronym
Using math functions
Discussion: Writing algorithms, programming environment.
CS1010 (AY2011/2 Semester 1) Extra Class - 3
7/27/2019 Extra_class_with_notes.pptx
4/15
Summary (2/2)
Week 4: Selection statements: if, if-else, switch
Discussion: inaccuracy of real numbers, initialisation of variables,redundant assignment, using selection statements, conditional
operator ?:, writing functions.
Week 5: Repetition statements: while, do-while, for
Week 6:
Functions with address parameters
Modular programming and separate compilation
Discussion: tracing of loops, writing loops, functions with addressparameters, design issues, exploration on random numbers.
CS1010 (AY2011/2 Semester 1) Extra Class - 4
7/27/2019 Extra_class_with_notes.pptx
5/15
Skills
Using vim
Pseudo-code before code Clear logic
Can you convince yourself that your algorithm works before you
start typing the program?
Simplifying the problem If you get a complex problem, what would you do? (Reference:
Week 6 Discussion Q5 Asterisks)
Incremental coding Do you type in the whole program at one go? Or a bit at a time?
Understanding compilers messages Do you know where to zoom into your program to spot the error?
CS1010 (AY2011/2 Semester 1) Extra Class - 5
7/27/2019 Extra_class_with_notes.pptx
6/15
Week 6 Discussion Q5: Asterisks
(b)
CS1010 (AY2011/2 Semester 1) Extra Class - 6
*
***
*****
*
***
*****
*******
*********
***********
(c)
*
***
*****
*
***
************
*********
***********
7/27/2019 Extra_class_with_notes.pptx
7/15
Big Question
Have you been writing a lot of programs by yourself? Target: Write 100 programs by the
end of this semester
For students with no experience: 150programs
Possible!Average about 15 programs per week, or 2
programs per day.
CS1010 (AY2011/2 Semester 1) Extra Class - 7
7/27/2019 Extra_class_with_notes.pptx
8/15
Tracing
Another important skill: Tracing codes. Reinforce that program execution is done step by step
following the sequence, selection and repetition control
structures.
Every variable should be represented by a box, and its value
updated during the trace.
Manually trace the given programs in the hand-out,
and write out their outputs.
CS1010 (AY2011/2 Semester 1) Extra Class - 8
7/27/2019 Extra_class_with_notes.pptx
9/15
Count multiples of 5 or 7
Write a program multiples5or7.c that asks user for apositive numbernum, and count the number of
multiples of 5 or 7 in the range [1, num].
Time limit: 10-15 minutes
Sample runs:
CS1010 (AY2011/2 Semester 1) Extra Class - 9
Enter positive integer: 10
There are 3 multiples of 5 or 7 in [1,10].
Enter positive integer: 6
There is 1 multiple of 5 or 7 in [1,6].
Enter positive integer: 50
There are 16 multiples of 5 or 7 in [1,50].
7/27/2019 Extra_class_with_notes.pptx
10/15
Perfect numbers (1/2)
Definition: A perfect numberis a positive integer that is the sum of its
proper positive divisors.
Examples:
6 is a perfect number, because 6 = 1 + 2 + 3
8 is not a perfect number, because 8 1 + 2 + 4
100 is not a perfect number, because 100 1 + 2 + 4 + 5 +
10 + 20 + 25 + 50
Write a program check_perfect.c that asks user
repeatedly for a non-negative integer, and stops whenthe number is zero.
For each positive integer entered, your program is to
check whether it is a perfect number or not.
CS1010 (AY2011/2 Semester 1) Extra Class - 10
7/27/2019 Extra_class_with_notes.pptx
11/15
Perfect numbers (2/2)
Time limit: 20-25 minutes Sample run:
CS1010 (AY2011/2 Semester 1) Extra Class - 11
Enter number: 6
6 is a perfect number.
Enter number: 88 is a not perfect number.
Enter number: 100
100 is not a perfect number.
Enter number: 0
7/27/2019 Extra_class_with_notes.pptx
12/15
Check order of input data (1/2)
Write a program check_order.c to read in a list ofpositive integers.
The program is to continue asking for the next
positive integer as long as the integers entered so far
are in increasing order. The moment the input data are not in increasing
order, or the input value is zero, the input ends.
The program should then report whether the input
data are in increasing order or not. You may assume that at least one positive integer is
entered. If that is the case, we treat the list as in
increasing order.
CS1010 (AY2011/2 Semester 1) Extra Class - 12
7/27/2019 Extra_class_with_notes.pptx
13/15
Check order of input data (2/2)
Time limit:20-25
minutes
Sample
runs:
CS1010 (AY2011/2 Semester 1) Extra Class - 13
Enter positive integer:3
Enter positive integer: 0
Data are in increasing order.
Enter positive integer: 10
Enter positive integer: 12
Enter positive integer: 21
Enter positive integer: 0Data are in increasing order.
Enter positive integer: 100
Enter positive integer: 102
Enter positive integer: 100
Data are not in increasing order.
Enter positive integer: 7
Enter positive integer: 31
Enter positive integer: 56
Enter positive integer: 56
Data are not in increasing order.
7/27/2019 Extra_class_with_notes.pptx
14/15
Reminders
Revise all of the following Textbook and lecture notes
Discussion questions
Lab exercises
Write many programs! Post queries on IVLE forum and read
postings by others
You CAN succeed!
CS1010 (AY2011/2 Semester 1) Extra Class - 14
7/27/2019 Extra_class_with_notes.pptx
15/15
End of File
CS1010 (AY2011/2 Semester 1) Extra Class - 15