Recess_extra_class.pptx

advertisement

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

CS1010 (AY2013/4 Semester 1) Recess Extra Class - 2

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) Recess Extra Class - 3

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) Recess Extra Class - 4

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) Recess Extra Class - 5

Week 6 Discussion Q5: Asterisks

 (b)

*

***

*****

*

***

*****

*******

*********

***********

 (c)

*

***

*****

*

***

*****

*******

*********

***********

CS1010 (AY2013/4 Semester 1) Recess Extra Class - 6

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) Recess Extra Class - 7

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) Recess Extra Class - 8

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) Recess Extra Class - 9

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) Recess Extra Class - 10

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) Recess Extra Class - 11

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) Recess Extra Class - 12

Check order of input data (Pract. Ex #20) (2/2)

 Time limit:

20-25 minutes

 Sample runs:

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: 0

Data 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.

Recess Extra Class - 13 CS1010 (AY2013/4 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 (AY2013/4 Semester 1) Recess Extra Class - 14

End of File

Download