150 likes | 401 Views
CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/. 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 test students’ understanding of these basic concepts. Summary (1/2). Week 2:
E N D
CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/
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 test students’ understanding of these basic concepts CS1010 (AY2011/2 Semester 1)
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)
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 address parameters, design issues, exploration on random numbers. CS1010 (AY2011/2 Semester 1)
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 compiler’s messages • Do you know where to zoom into your program to spot the error? CS1010 (AY2011/2 Semester 1)
Week 6 Discussion Q5: Asterisks * *** ***** ******* ********* *********** * *** ***** • (b) • (c) * *** ***** * *** ***** ******* ********* *********** CS1010 (AY2011/2 Semester 1)
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: 150 programs • Possible! Average about 15 programs per week, or 2 programs per day. CS1010 (AY2011/2 Semester 1)
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)
Count multiples of 5 or 7 • Write a program multiples5or7.c that asks user for a positive number num, and count the number of multiples of 5 or 7 in the range [1, num]. • Time limit: 10-15 minutes • Sample runs: 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]. CS1010 (AY2011/2 Semester 1)
Perfect numbers (1/2) • Definition: • A perfect number is 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 when the 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)
Perfect numbers (2/2) • Time limit: 20-25 minutes • Sample run: Enter number: 6 6 is a perfect number. Enter number: 8 8 is a not perfect number. Enter number: 100 100 is not a perfect number. Enter number: 0 CS1010 (AY2011/2 Semester 1)
Check order of input data (1/2) • Write a program check_order.c to read in a list of positive 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)
Check order of input data (2/2) Enter positive integer: 3 Enter positive integer: 0 Data are in increasing order. • Time limit: 20-25 minutes • Sample runs: Enter positive integer: 10 Enter positive integer: 12 Enter positive integer: 21 Enter positive integer: 0 Data are in increasing order. Enter positive integer: 100Enter positive integer: 102 Enter positive integer: 100 Data are not in increasing order. Enter positive integer: 7Enter positive integer: 31Enter positive integer: 56 Enter positive integer: 56 Data are not in increasing order. CS1010 (AY2011/2 Semester 1)
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)
End of File CS1010 (AY2011/2 Semester 1)