Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS

Similar presentations


Presentation on theme: "Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS"— Presentation transcript:

1 Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS http://www.renren.com/profile.do?id=313870900

2 Gist of the Problem Anything written in any languages involve two components: – Grammar( 语法) – Content (内容) Many are learning the grammar of programming languages without knowing how to come out with the content. That’s like trying to write poetry without knowing what you want to write about. So what if you know all the grammatical rules? The slides here aim to give some explanation on how the “content” are generated or more correctly designed in computing

3 Components of Programming Languages Operators/Input/Output: These are single statements that either modify the values stored in the variables or output them Memory allocation: These are statements that allocate spaces for storing variables and declare how these variables should be interpreted (eg int, char[], malloc) Control Statements: Control the flow of programs. Two main ones: if-then-else, loop Functions: We can do without functions but things will be much more tidy and manageable with them. We talk about functions later.

4 Plan Your Program ! To program effectively, it make sense to have a feel or overview of your program. Typically, this involve the following three steps i) Plan your control flow statements and their scope – How many loops? Are they nested within each other? Are they within or outside the scope of if-then-else statements? – How many if-then-else should there be? What scope should they be within ? What should be within their scopes? ii) What variables should there be? This includes – (a) control variables for control statements, – (b) variables for storing input/output/intermediate result iii) What operators should be used to manipulate the variables and more importantly where should they be within the scopes of the control statements

5 Scope(I) The scope of a control statement refers to the regions which are control by the statement: if () then { } else { } for () { } while () { } Scope_if Scope_else Scope_forScope_while Disclaimer: We won’t be faithfully following the syntax of C here. Most programming languages apply here anyway.

6 Scope(II) By arranging the scopes of these control statements, you form the skeleton (initial design) of your program. main() { if () then { } else { } for () { } if () { } Scope_for Scope_if main() { for () { } for () { } if () { } Scope_for Scope_if Example 1Example 2

7 Scope(III) Once the skeleton is correct, it is a matter of filling up the “meat” i.e. the variables declaration, operators, input, output statements at the correct place within the skeleton main() { for () { } for () { } if () { } Scope_for Scope_if variables declaration and initialization usually here can be here and there as well (variables declared within a scope won’t be recognized outside it) input statements output statements usually here but can be at any place as well condition tests

8 Scope(IV) 1.Test: Since the skeleton of the program is so important, it make to test the skeleton thoroughly (using printf statement )BEFORE filling in the actual “meat” 2.Indent: It is a good habit to indent the brackets properly so that the scopes (and thus the skeleton) of your control statements are very clear to you 3.Comments: If need be, first add in comments near the control statements to remind yourself why you put these control statements there! 4.Self-contain: Try to make the scopes as self-contained as possible i.e. statements within a scope should NOT affect anything outside the scope unless necessary (eg. for sending out result to outside the scope)

9 Example Write a program that determine whether an input number N is a prime Step 1: write/think in human/mathematical language how you intend to do it – I will take N and divide it by all those integer starting from 2 that is smaller than it. If I found a number that can divide N exactly, then N is not a prime, else N is a prime Step 2: from the above, try to guess how many loops and if-then-else statements are needed. Here, assume we guess: 1 loop and 1 if-then-else based on previous step

10 Example(II) Step 3: Try to arrange the 1 loop and 1 if-then- else statement to form the skeleton main() { for () { } if () { } else { } main() { if () { } else { } for () { } Choice 1 Which choice is correct? Choice 2

11 Example(III) Step 4: Assuming you know choice 1 is correct either through testing or by common sense! Then we need to fill in the “meat” main() { scanf(“%i”,N); for (int i=2;i<N;i++) { } if (N mod i==0) { IsPrime==0 } else { % actually nothing need to be here! } Choice 1 Input N here, need to declare int N input statements output statements condition tests Need to output result, probably need to get result from the if-then-else scope through a variable. Oh, maybe need an additional if-then-else! this loop is to go through 2 to N- 1, need to declare int i for the counter in the loop this “if” is to test whether N is divisible by i, must pass result to output statement, need to declare a boolean variable IsPrime

12 Example (IV) Step 5: Finally, declare the variables that are needed and add in the if-then-else statement that we never foresee variables declaration and initialization main() { int N; int IsPrime=1; scanf(“%i”,N); for (int i=2;i<N;i++) { } if (IsPrime==0) then printf(“Not prime”) else printf(“prime”) } if (N mod i==0) { IsPrime==0 } Notice how the variable i exists only within the scope of the for-loop since it is not needed outside the scope. Notice how we easily figure out that we need an additional if-then-else statement and place it correctly due to the fact that we are very clear about the skeleton of the program and know where the output logic are.

13 Equivalent Statements There are many equivalent statements in C that are useful to have but not necessary Example: do-while(), while(), for() can replaced each other Example: if-then-else can replace the switch- case statements Example: break can be avoided altogether Feel free to use them but do not get confuse with the program logic because of them!

14 Due to lack of time, we will stop here, when I have the time, I will add in slides for Functions and how they can help you to design you program better. Good luck with your checkpoint!


Download ppt "Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS"

Similar presentations


Ads by Google