1 / 15

CS1010: Programming Methodology comp.nus.sg/~cs1010/

CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/. Extra class: 25 September 2013. 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).

gabby
Download Presentation

CS1010: Programming Methodology comp.nus.sg/~cs1010/

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/

  2. Extra class: 25 September 2013 • 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 (AY2013/4 Semester 1)

  3. Summary (1/2) • Week 1: • Writing algorithms (pseudo-codes) • Week 2: • 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 • Using math functions • Discussion session: Writing algorithms, programming environment, using CodeCrunch CS1010 (AY2013/4 Semester 1)

  4. Summary (2/2) • Week 4: • Selection statements: ‘if’, ‘if-else’, ‘switch’ • Discussion session: Inaccuracy of real numbers, initialisation of variables, redundant assignment, writing functions. • Week 5: • Repetition statements: ‘while’, ‘do-while’, ‘for’ • Discussion session: Good programming practice, using selection statements, conditional operator ?:, using repetition statements. • Week 6: • Functions with address parameters • Discussion session: Repetition statements, functions with address parameters, design issues, exploration on random numbers. CS1010 (AY2013/4 Semester 1)

  5. 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 (AY2013/4 Semester 1)

  6. Week 6 Discussion Q5: Asterisks * *** ***** ******* ********* *********** * *** ***** • (b) • (c) * *** ***** * *** ***** ******* ********* *********** CS1010 (AY2013/4 Semester 1)

  7. Big Question • Have you been writing a lot of programs by yourself? • Target: Write 150 programs by the end of this semester • For students with no experience: 200 programs • Possible! Average about 17 programs per week over 12 weeks. Less than 3 programs a day! CS1010 (AY2013/4 Semester 1)

  8. 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 (AY2013/4 Semester 1)

  9. Count multiples of 5 or 7 (Practice Ex #18) • 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]. • Your program should have a function count_multiples(int) • 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 (AY2013/4 Semester 1)

  10. Perfect numbers (Practice Ex #19) (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. • It should have a function is_perfect(int) that returns 1 if the parameter is a perfect number, or 0 otherwise. • For each positive integer entered, your program is to check whether it is a perfect number or not. CS1010 (AY2013/4 Semester 1)

  11. Perfect numbers (Practice Ex #19) (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 (AY2013/4 Semester 1)

  12. Check order of input data (Pract. Ex #20) (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 (AY2013/4 Semester 1)

  13. Check order of input data (Pract. Ex #20) (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 (AY2013/4 Semester 1)

  14. 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 (AY2013/4 Semester 1)

  15. End of File

More Related